def get_data_gvt_compo(row): """ FUNCTION get a string (row from csv file) and put its content into an instance of GvtCompo PARAMETERS row: row from the csv file [row object] RETURN instance: instance of the model with the extracted data [GvtCompo model instance] msg: id of the row, used to display an error message [string] exist: True if the instance already exists, False otherwise [boolean] """ #used to identify the row ids_row={} ids_row["start_date"]=date_string_to_iso(row[0].strip()) ids_row["end_date"]=date_string_to_iso(row[1].strip()) gvt_compos=row[2].split(":") ids_row["country"]=Country.objects.get(country_code=gvt_compos[0].strip()) #get instance or create instance if does not already exist instance, created = GvtCompo.objects.get_or_create(**ids_row) exist=not created #if the row didn't exist, save the related models #2009-avril-09 2010-juin-27 CZ: none if not exist and gvt_compos[1].strip()!="none": #split party and party_family partys_temp=gvt_compos[1].split(",") partys=[] partys_family=[] for party in partys_temp: string=re.findall("[^()]+", party) partys.append(string[0].strip()) partys_family.append(string[1].strip()) #save party and party_family data={} data["country"]=ids_row["country"] for index in xrange(len(partys)): #save party data["party"]=save_get_object(Party, {"party": partys[index]}) instance.party.add(data["party"]) data["party_family"]=partys_family[index] if data["party_family"]=="Conservative/ Christian Democracy": data["party_family"]="Conservative/Christian Democracy" #save party_family try: save_get_object(PartyFamily, data) except Exception, e: #inconsistency in data -> the source file must be fixed before saving the data again print "get_data_gvt_compo exception", e #~ msg="Cant save the row" msg=str("Can't save the row " + str(ids_row) + ". '" + data["country"].country_code + "'+'" + data["party"].party + "' can't be associated to '" + str(data["party_family"]) + "' because they are already associated to '" + PartyFamily.objects.get(country=data["country"], party=data["party"]).party_family + "' !") print msg exist=True instance.delete() return instance, msg, exist
def get_sign_pecs(soup, no_unique_type): """ FUNCTION get the sign_pecs variable from the oeil url PARAMETERS soup: oeil url content [BeautifulSoup object] RETURN sign_pecs [date] """ if no_unique_type=="COD": try: sign_pecs=soup.find("td", text="Final act signed").find_previous("td").get_text() return date_string_to_iso(sign_pecs) except: return None return None