コード例 #1
0
ファイル: get_save_act_data.py プロジェクト: EPX/epx2013
    def handle(self, **options):
        
        #~ acts_to_validate=[]
        #~ acts_to_validate.append([2003, 10, 1])
        #~ acts_to_validate.append([2003, 10, 9])
        #~ acts_to_validate.append([2003, 10, 10])
        #~ 
        #~ for act_ids in acts_to_validate:
            
            #~ act=Act.objects.get(releve_annee=act_ids[0], releve_mois=act_ids[1], no_ordre=act_ids[2])

        #import and "validate" acts not yet validated by experts
        for act in Act.objects.filter(validated=1):
            
            #the act has not been validated yet
            if act.validated==1:
                
                #retrieve the act ids for each source
                act_ids=get_act_ids(act)

                #"compute" the url of the eurlex and oeil page
                urls=get_urls(act_ids["index"])

                #get data on oeil
                fields, dg_names_oeil, resp_names_oeil=get_data("oeil", act_ids, urls["url_oeil"])
                act.__dict__.update(fields)
    
                #get data on eurlex
                fields, dg_names_eurlex, resp_names_eurlex=get_data("eurlex", act_ids, urls["url_eurlex"])
                act.__dict__.update(fields)
                

                #pb empty dgs (empty list instead of None)
                fields["dg_1_id"]=set_empty_dg(fields["dg_1_id"])
                fields["dg_2_id"]=set_empty_dg(fields["dg_2_id"])
                fields["dg_3_id"]=set_empty_dg(fields["dg_3_id"])
                act.__dict__.update(fields)
                
                #~ #store dg/resp from eurlex and oeil to be displayed as text in the template
                act=store_dg_resp(act, dg_names_eurlex, dg_names_oeil, "dg")[0]
                act=store_dg_resp(act, resp_names_eurlex, resp_names_oeil, "resp")[0]

                #~ #check multiple values for dgs with numbers
                act=check_multiple_dgs(act)[1]

                #set value for releve_mois_init
                act.releve_mois_init=act.releve_mois
                   
                #update opal, min_attend
                get_data_others(act_ids["index"], act)
               
                #validate the act
                act.validated=2
                
                #set false to empty booleans
                if act.modif_propos==None:
                    act.modif_propos=False
                
                print "act"
                fields= act.__dict__
                for field, value in fields.items():
                    print field, value
                
                #save the act
                print act.__dict__
                act.save()
                
                print "the act", act, "has been validated :)."
                print ""
                
                #add entry in history
                History.objects.create(action="add", form="data", act=act, user=User.objects.get(username="******"))
            
            else:
                print "the act", act, "has already been validated!!"
                print ""
コード例 #2
0
ファイル: views.py プロジェクト: EPX/epx2013
def get_data_all(context, add_modif, act, POST):
    """
    FUNCTION
    get all data of an act (from eurlex and oeil)
    PARAMETERS
    context: variables to be displayed in the html form [dictionary]
    add_modif: "add" if the form is in add mode, "modif" otherwise [string]
    act: instance of the data of the act [Act model instance]
    POST: request.POST object [dictionary]
    RETURN
    context: update of the variables to be displayed in the html form [dictionary]
    """
    logger.debug("get_data_all")
    # retrieve the act ids for each source
    act_ids = get_act_ids(act)

    # "compute" the url of the eurlex and oeil page
    urls = get_urls(act_ids["index"])

    # an act has been selected in the drop down list -> the related data is displayed
    # if state different of modif, save and ongoing and if the act is not being modified
    # not check_update(POST): not updating a code_sect, rapp or resp
    if context["state"] == "display" and add_modif == "add" and not check_update(POST):
        print "data retrieval"
        logger.debug("data retrieval")

        # retrieve all the data from all the sources
        # COMMENT FOR TESTS ONLY

        logger.debug("oeil to be processed")
        dg_names_oeil = [None] * nb_dgs
        resp_names_oeil = [None] * nb_resps
        print "oeil to be processed"
        fields, dg_names_oeil, resp_names_oeil = get_data("oeil", act_ids, urls["url_oeil"])
        act.__dict__.update(fields)

        # adopt_conseil from eurlex needs nb_lectures from oeil -> eurlex after oeil
        logger.debug("eurlex to be processed")
        print "eurlex to be processed"
        fields, dg_names_eurlex, resp_names_eurlex = get_data("eurlex", act_ids, urls["url_eurlex"])
        act.__dict__.update(fields)

        # ~ #store dg/resp from eurlex and oeil to be displayed as text in the template
        logger.debug("dg and resp to be processed")
        act, dg_names_eurlex, dg_names_oeil = store_dg_resp(act, dg_names_eurlex, dg_names_oeil, "dg")
        act, resp_names_eurlex, resp_names_oeil = store_dg_resp(act, resp_names_eurlex, resp_names_oeil, "resp")
        # ~
        # ~ #check multiple values for dgs with numbers
        dgs, act = check_multiple_dgs(act)

    # COMMENT FOR TESTS ONLY
    temp = get_data_others(act_ids["index"], act)
    context["opals"] = temp["opal"]
    context["gvt_compos"] = temp["gvt_compo"]
    context["min_attends"] = temp["min_attend"]
    context["group_votes"] = temp["group_votes"]
    print "group_votes", context["group_votes"]
    group_names = act.group_vote_names_as_dict()
    context["group_vote_names"] = group_names

    # we have selected an act in the drop down list or clicked on the modification button
    if "add_act" in POST or "modif_act" in POST:
        # display adopt variables (countries in the drop down lists)
        initial_dic = get_adopt_variables(act)
        # display cons variables
        initial_dic.update(get_cons_vars("b", act.date_cons_b, act.cons_b))
        initial_dic.update(get_cons_vars("a", act.date_cons_a, act.cons_a))
        initial_dic.update(context["group_votes"])

        if "add_act" in POST:
            print "add_act", POST["add_act"]
            # ~ print "modif_act", POST["modif_act"]
            initial_dic["releve_mois_init"] = act.releve_mois
            initial_dic["hidden_dg_eurlex_dic"] = dg_names_eurlex
            initial_dic["hidden_dg_oeil_dic"] = dg_names_oeil
            initial_dic["hidden_resp_eurlex_dic"] = resp_names_eurlex
            initial_dic["hidden_resp_oeil_dic"] = resp_names_oeil
            initial_dic["hidden_dg_dic"] = dgs

        form_data = ActForm(instance=act, initial=initial_dic)
    else:
        form_data = ActForm(POST, instance=act)

    context["urls"] = urls
    context["act"] = act
    context["party_family"] = get_party_family({"1": act.resp_1_id, "2": act.resp_2_id, "3": act.resp_3_id})
    context["act_ids"] = act_ids
    context["form_data"] = form_data

    return context
コード例 #3
0
ファイル: views.py プロジェクト: EPX/epx2013
def act_ids(request):
    """
    VIEW
    displays and processes the Act ids validation page
    TEMPLATES
    act_ids/index.html: display the act ids page which itself calls the template of the act_ids form
    act_ids/form.html: display the act_ids form
    """
    response={}
    #display "real" name of variables (not the ones stored in db)
    response['display_name']=var_name_ids.var_name
    response['display_name'].update(var_name_data.var_name)
    #state=display (display the ids of an act), saved (the act is being saved) or ongoing (validation errors while saving)
    state="display"
    #html page of the form
    form_template='act_ids/form.html'

    if request.method=='POST':
        #mode: "add" if selection of an act to add from the drop down list, "modif" if click on the modif_act button and None otherwise
        #add_modif: same than mode but return None if the add or modif form is not valid
        #act=act to validate / modify or None if no act is found (modifcation)
        #response: add add or modif to the forms being displayed / to be displayed
        mode, add_modif, act, response=check_add_modif_forms(request, response, Add, Modif, "act_ids")

        #if any of this key is present in the response dictionary -> no act display and return the errors with a json object
        #otherwise display act and return the html form of the act to validate or update in a string format
        keys=["msg", "add_act_errors", "modif_act_errors", "update_act_errors"]

        #if selection of an act in the drop down list or click on the modif_act button
        if mode!=None:
            #~ #if we are about to add or modif an act (the add or modif form is valid)
            if add_modif!=None:

                act_ids=ActIds.objects.get(act=act, src="index")
                form_ids=ActIdsForm(request.POST, instance=act_ids)
                #just for the notes field
                form_data=ActForm(request.POST, instance=act)

                #saves the act
                if 'save_act' in request.POST:
                    print "save"
                    if form_ids.is_valid():
                        print "form_ids valid"
                        #save the ids of the act in ActIds
                        form_ids.save()
                        #save notes and validate the act
                        act.notes=request.POST['notes']
                        if act.validated==0:
                            act.validated=1
                        act.save()
                        state="saved"
                        #success message (calls unicode method)
                        response["msg"]="The act " + str(act) + " has been validated!"
                        response["msg_class"]="success_msg"

                        #save in history
                        History.objects.create(action=add_modif, form="ids", act=act, user=request.user)
                    else:
                        print "form_ids not valid", form_ids.errors
                        if request.is_ajax():
                            response['save_act_errors']= get_ajax_errors(form_ids)
                        else:
                            response['form_ids']=form_ids
                        response["msg"]="The form contains errors! Please correct them before submitting again."
                        response["msg_class"]="error_msg"
                        state="ongoing"

                #if click on the actualisation button
                elif 'update_act' in request.POST:
                    print "update"
                    state="update"
                    #news ids must be saved in the database
                    if form_ids.is_valid():
                        print "update: form_ids valid"
                        form_ids.save()
                        #we retrieve and save the new ids (from the new urls)
                        ids_row={}
                        ids_row["releve_annee"]=act.releve_annee
                        ids_row["releve_mois"]=act.releve_mois
                        ids_row["no_ordre"]=act.no_ordre
                        #actualisation button -> use acts ids retrieval from the import module
                        get_save_act_ids([ids_row])
                        #get the updated instance of the act
                        act=Act.objects.get(id=act.id)
                    else:
                        print "form_ids not valid", form_ids.errors
                        if request.is_ajax():
                            response['update_act_errors']= get_ajax_errors(form_ids)
                        else:
                            response['form_ids']=form_ids

                #displays the ids of an act to validate
                #selection of an act in the add / modif form or update of an act with no form error
                #if javasxript deactivated, also display act ids if click on save button and errors in form_ids
                if not any(key in response for key in keys) or not request.is_ajax() and state!="saved":
                    print 'act_to_validate display'
                    
                    #an act has been selected in the drop down list -> the related data are displayed
                    if state=="display":
                        #the default value of the dos_id drop down list is the validated dos_id if any
                        init=None
                        if act_ids.dos_id!=None:
                            init=act_ids.dos_id
                        form_ids=ActIdsForm(instance=act_ids, initial ={'dos_id_choices': init})
                        form_data=ActForm(instance=act)
                    #otherwise use POST too (done before)

                    data={}
                    #retrieve the act ids instance for each source
                    act_ids=get_act_ids(act)

                    fields=["no_celex", "propos_annee", "propos_chrono", "propos_origine", "no_unique_annee", "no_unique_type", "no_unique_chrono"]

                    #check if the corresponding data are equal -> they will appear in red if not
                    for field in fields:
                        param=[]
                        for src in ["index", "eurlex", "oeil"]:
                            param.append(getattr(act_ids[src], field))
                        data[field]=check_equality_fields(param)

                    #check dos_id
                    data["dos_id"]=False
                    if act_ids["eurlex"].dos_id!=None:
                        #if there is a validated dos_id, do the comparison with that one
                        if act_ids["index"].dos_id!=None:
                            data["dos_id"]=check_equality_fields([act_ids["index"].dos_id, act_ids["eurlex"].dos_id])
                        else:
                            #otherwise search for the dos_id from the ImportDosId model matching the no_celex
                            try:
                                ImportDosId.objects.get(dos_id=act_ids["eurlex"].dos_id, no_celex=act_ids["index"].no_celex)
                                data["dos_id"]=True
                            except Exception, e:
                                print "no matching dos_id", e

                    #get urls
                    data["url_eurlex"]=eurlex.get_url_eurlex(act_ids["index"].no_celex, "HIS")
                    data["url_oeil"]=oeil.get_url_oeil(str(act_ids["index"].no_unique_type), str(act_ids["index"].no_unique_annee), str(act_ids["index"].no_unique_chrono))

                    response['form_ids']=form_ids
                    response['form_data']=form_data
                    response['act']=act
                    response['act_ids']=act_ids
                    response['data']=data
                    response['add_modif']=add_modif

                response['mode']=mode

            if request.is_ajax():
                #save act (with or without errors) or act display, modif and update (with errors)
                if any(key in response for key in keys):
                    return HttpResponse(simplejson.dumps(response), mimetype="application/json")
                else:
                    #act display, modif or update (without errors)
                    return HttpResponse(render_to_string(form_template, response, RequestContext(request)))

        if request.is_ajax():
            #no act has been selected-> do nothing
            return HttpResponse(simplejson.dumps(""), mimetype="application/json")