def handle(self, **options): #make uniform the list of dg: #"Administration", "DG Administration", "Administration DG", "Directorate-General for the Administration": same DG! #only one DG needed: "DG Administration" (all the DGs must start with "DG") #store old and new dg names dgs={} print "get old and new values" for dg in DG.objects.all(): old_dg=dg.dg #format dg name: "DG ..." new_dg=format_dg_name(old_dg) #add to the dictionary of dgs to update if the name has changed if dg.dg != new_dg: dgs[dg.dg.encode("utf-8")]=new_dg.encode("utf-8") print dg.dg.encode("utf-8") + " -> " + new_dg.encode("utf-8") #~ #update database (dg in Act model, DG model and dg_nb) print "update DG model" for old_value, new_value in dgs.iteritems(): old_dg=DG.objects.get(dg=old_value) #~ #update DG model: add new DGs (DG model with dg_sigle) new_dg=DG.objects.get_or_create(dg=new_value, defaults={"dg_sigle": old_dg.dg_sigle}) #~ #NO NEED because all dgs with numbers are linked to dg with a correct name already #update dg_nb association: get dgs with nb linked to the current dg and associate them with the new dg for dg_nb in old_dg.dg_nb.all(): print dg_nb.dg new_dg.dg_nb.add(dg_nb) #remove old links old_dg.dg_nb.clear() #~ #update Act model and all their dgs print "update Act model" for act in Act.objects.all(): for index in range(1, nb_dgs+1): #if the act has a reference to an old dg, update it field_name="dg_"+str(index) dg=getattr(act, field_name) if dg is not None and dg.dg in dgs: setattr(act, field_name, DG.objects.get(dg=dgs[dg.dg])) print str(act)+": "+ dg.dg+" -> "+ dgs[dg.dg] #~ #~ #save the modifications if any act.save() #~ #~ #remove old DGs print "delete old dgs from DG model" for dg in DG.objects.all(): if dg.dg[:2] != "DG": dg.delete()
def compare_dgs(dg_1, dg_2): """ FUNCTION compare the dgs passed in parameter: return True if they are the same and False otherwise PARAMETERS dg_1: name of the first dg to compare [string] dg_2: name of the second dg to compare [string] RETURN True if the dgs are the same and False otherwise [boolean] """ if dg_1 is None and dg_2 is None: return True if dg_1 is not None and dg_2 is not None: dg_1=format_dg_name(dg_1) dg_2=format_dg_name(dg_2) #2014-2-1: Value found on eurlex: European Anti-Fraud Office; Value found on oeil: European Anti-Fraud Office (OLAF). if dg_1 == dg_2 or dg_1 in dg_2 or dg_2 in dg_1: return True return False
def store_dg_resp(act, eurlex_list, oeil_list, var_name): """ FUNCTION get all the dgs and resps on eurlex / oeil and save oeil dgs / resps in Act model if no dg or a dg with numbers only was found on eurlex PARAMETERS act: instance of the data of the act [Act model instance] eurlex_list: list of dg or resp names from eurlex [list of strings] oeil_list: list of dg or resp names from oeil [list of strings] var_name: name of the field ("dg" or "resp") [string] RETURN act: instance of the data of the act (with updated dgs or resps if any) [Act model instance] eurlex_dic: list of dg or resp names from eurlex [dictionary of strings] oeil_dic: list of dg or resp names from oeil [dictionary of strings] """ oeil_dic = {} for index, field in enumerate(oeil_list, start=1): num = str(index) oeil_dic[num] = field eurlex_dic = {} for index, field in enumerate(eurlex_list, start=1): num = str(index) eurlex_dic[num] = field # if no dg on eurlex, we use the dg on oeil # 2014-5-1 (dg=101059): if we found a dg with only numbers on eurlex, without a table of correspondance it's impossible to tell what dg it is -> we use the dg on oeil if (field is None or field.isdigit()) and oeil_dic[num] is not None: try: # update the act instance with the oeil resp if var_name == "resp": setattr(act, "resp_" + num, Person.objects.get(name=oeil_dic[num])) else: # format dg name: "DG ..." new_dg = format_dg_name(oeil_dic[num]) # update the act instance with the oeil dg setattr(act, "dg_" + num, DG.objects.get(dg=new_dg)) except Exception, e: print "except store_dg_resp", e