Exemplo n.º 1
0
def get_rapps(rapps_html):
    """
    FUNCTION
    get rapporteurs data from the html content about rapporteurs
    PARAMETERS
    rapps_html: rapporteurs data (html format) [BeautifulSoup object]
    RETURN
    data: rapporteurs data object [dictionary of Person model objects]
    """
    data={}
    #for each rapporteur, get name, party and country
    for index in xrange(len(rapps_html)):
        num=str(index+1)
        #django adds "_id" to foreign keys field names
        data['rapp_'+num+"_id"]=None
        if rapps_html[index]!=None:
            rapp=" ".join(get_rapp(rapps_html[index]).split())
            field=[Person, "name", rapp]
            fks=[]
            
            try:
                fks.append([Country, "country", get_country_instance(get_country(rapps_html[index])).pk])
            except Exception, e:
                print "exception get_rapps", e
                
            fks.append([Party, "party", get_party(rapps_html[index])])
            src="rapp"
            data['rapp_'+num+"_id"]=save_get_field_and_fk(field, fks, src)[0]
Exemplo n.º 2
0
Arquivo: views.py Projeto: EPX/epx2013
def save_2_tables(row, fields, fields_fk, party_family=""):
    """
    FUNCTION
    open a csv file and save its variables variables in 2 joined tables:
    code_sect_rep/config_cons
    code_sect_rep/code_agenda
    name/country
    name/party
    dg/dg_sigle
    PARAMETERS
    row: row to be processed (from the csv file) [list]
    fields: model name, field name and position in file (num colomn) [list]
    fields_fk: model name, field name and position in file (num colomn) for each foreign key [list of lists]
    party_family: model name, field name and position in file (num colomn) for the party_family variable
    RETURN
    msg: row main variable, used to display a success or error [string]
    exist: True if the instance already exists, False otherwise [boolean]
    """
    #~ field=["Person", "name", <position>]
    #~ fields_fk=[["Country", "country", <position>], ["Party", "party", <position>]]
    src=""
    #original lists are modified a few lines after in the program and should not be (the position is updated with the value of the field) -> use a copy of the list instead with deepcopy
    field=copy.deepcopy(fields)
    field_fk=copy.deepcopy(fields_fk)
    #the position becomes the value (in order to call the save_get_field_and_fk function)
    field[2]=row[field[2]].strip()
    #if import of responsibles (eurlex) -> change name format of person: "LASTNAME, Firstname" -> "LASTNAME Firstname"
    if field[1]=="name":
        field[2]=field[2].replace(",","")
        src="resp"
    #delete the last dot for the code_sect if any
    elif field[1]=="code_sect":
        field[2]=field[2].rstrip(".")

    msg=var_name_data.var_name[field[1]]+"="+field[2]
    #for each foreign key
    for fk in field_fk:
        fk[2]=row[fk[2]].strip()
        msg+=", "+fk[1]+"="+fk[2]

    #save the field and its foreign keys
    instance, exist=save_get_field_and_fk(field, field_fk, src)

    #if dg import: we have to save dg_nb too
    if field[1]=="dg":
        dg_nb=row[2].strip()
        #if the dg has an associate number
        if dg_nb!="":
            #save or get (if already exists) the dg_nb instance
            dg_nb_instance=save_get_object(DGNb, {"dg_nb": dg_nb})
            #link it to the current dg
            instance.dg_nb.add(dg_nb_instance)
    #import party_family
    elif party_family!="":
        party_family=row[party_family[2]].strip()
        #save the party family
        try:
            PartyFamily.objects.create(party=instance.party, country=instance.country, party_family=party_family)
        except Exception, e:
            print "party_family already exists for this party and country"
        msg+=", party_family="+party_family