fix_country_0808.py 2.41 KB
Newer Older
1 2
# -*- coding: utf-8 -*-
""" NAME
3 4
        fix-country-0808 -- fix invalid country names

5 6
    SYNOPSIS
        fix-country [options]
7

8
    DESCRIPTION
9
        Before the limbra 0.8.8, the name of the country
10 11 12
        for a conference is defined by the user or by harvesters.
        As the result, the database contains a mixture of French and
        English name for country. In addition, some value are wrong.
13

14 15 16 17
        Starting with version 0.8.8, the database is populated with
        a list of countries, in English, coming from a geographical
        database. A standard user or an harvesters can no longer add
        a country in the database.
18

19 20
        The aim of this script, is to replace invalid name by the
        correct one and to remove bad ones.
21

22 23
        At the end of this process, the database should only contains
        official country names.
24

25
    OPTIONS
26

27
    EXAMPLE
28

29
        > cd ...limbra/scripts
30
        > ./run script fix-country-0808.py
31
        > ./run script -S limbra_cppm fix-country-0808.py
32

33
    AUTHOR
34
        R. Le Gac -- Nov 2014
35

36
"""
37
if __name__ == "__main__":
38

39
    from callbacks import INHIBIT_PUBLICATION_UPDATE_ON_OK
40

41 42
    from countries import COUNTRIES
    from plugin_dbui import get_id, UNDEF_ID
43

44 45
    # unlock the publications update when the status is OK
    db.publications._before_update.remove(INHIBIT_PUBLICATION_UPDATE_ON_OK)
46

47 48
    # scan the database to find invalid countries
    for row in db(db.countries).select():
49

50 51
        if row.country in COUNTRIES or row.id == UNDEF_ID:
            continue
52

53 54 55
        # replacement value for the country
        old_country = row.country
        new_country = raw_input("\nReplacement for '%s' [to skip CR]: " % old_country)
56

57 58
        if not new_country:
            continue
59

60 61 62
        # is the new value valid ?
        id_old = get_id(db.countries, country=old_country)
        id_new = get_id(db.countries, country=new_country)
63

64 65
        if not id_new:
            continue
66

67 68
        print "%s will be replaced by %s" % (old_country, new_country)
        rep = raw_input("Ok to continue [y/N]: ")
69

70 71
        if rep != "y":
            continue
72

73 74 75
        # modify publications
        for row in db(db.publications.id_countries==id_old).select():
            print "  - ", row.id, row.title
76
            db(db.publications.id==row.id).update(id_countries=id_new)
77
            db.commit()
78

79 80 81
        # delete the old value
        db(db.countries.id==id_old).delete()
        db.commit()