Example #1
0
 def handle(self, *args, **options):
     print "clean old downloaded files"
     os.system("rm %s %s" % (JSON_DUMP_ARCHIVE_LOCALIZATION, JSON_DUMP_LOCALIZATION))
     print "download lastest data dump of meps from parltrack"
     os.system("wget http://parltrack.euwiki.org/dumps/ep_meps_current.json.xz -O %s" % JSON_DUMP_ARCHIVE_LOCALIZATION)
     print "unxz dump"
     os.system("unxz %s" % JSON_DUMP_ARCHIVE_LOCALIZATION)
     print "load json"
     meps = json.load(open(JSON_DUMP_LOCALIZATION, "r"))
     print "Set all current active mep to unactive before importing"
     with transaction.commit_on_success():
         MEP.objects.filter(active=True).update(active=False)
         a = 0
         for mep_json in meps:
             a += 1
             print a, "-", mep_json["Name"]["full"].encode("Utf-8")
             in_db_mep = MEP.objects.filter(ep_id=int(mep_json["UserID"]))
             if in_db_mep:
                 mep = in_db_mep[0]
                 mep.active = mep_json['active']
                 manage_mep(mep, mep_json)
             else:
                 mep = create_mep(mep_json)
         clean()
     print
     update_meps_positions(verbose=True)
     update_search_index()
 def handle(self, *args, **options):
     if len(args) not in (3, 4):
         print >>sys.stderr, "Usage: %s <recommendationdata id> <{for,against}> <recommendation weight> <proposal ponderation=1 by default>" % __file__
         sys.exit(1)
     if len(args) == 4:
         recommendationdata_id, recommendation, weight, proposal_ponderation = args
     else:
         recommendationdata_id, recommendation, weight = args
         proposal_ponderation = 1
     if recommendation not in ("for", "against"):
         print recommendation
         print >>sys.stderr, "Recommendation should be either 'for' or 'against'"
         sys.exit(1)
     with transaction.commit_on_success():
         create_recommendation(*args)
     sys.stdout.write("Update total score of all meps now\n")
     update_total_score_of_all_meps(verbose=True)
     update_meps_positions(verbose=True)
     sys.stdout.write("Clean all deprecated trends\n")
     clean_all_trends()
     update_search_index()
def clean():
    Delegation.objects.annotate(mep_count=Count('mep')).filter(mep_count=0).delete()
    Committee.objects.annotate(mep_count=Count('mep')).filter(mep_count=0).delete()
    Organization.objects.annotate(mep_count=Count('mep')).filter(mep_count=0).delete()

if __name__ == "__main__":
    print "load json"
    meps = json.load(open(current_meps, "r"))
    print "Set all current active mep to unactive before importing"
    with transaction.commit_on_success():
        MEP.objects.filter(active=True).update(active=False)
        a = 0
        #for mep_json in meps["meps"]:
        for mep_json in meps:
            a += 1
            print a, "-", mep_json["Name"]["full"]
            in_db_mep = MEP.objects.filter(ep_id=int(mep_json["UserID"]))
            if in_db_mep:
                mep = in_db_mep[0]
                mep.active = mep_json['active']
                manage_mep(mep, mep_json)
            else:
                mep = create_mep(mep_json)
        clean()
    print
    update_meps_positions(verbose=True)
    update_search_index()

# vim:set shiftwidth=4 tabstop=4 expandtab: