Exemple #1
0
def menage(request):

    scenario = request.session.get('scenario', default=None)
    if scenario == None:
        print 'scenario is None'
        scenario = Scenario()

    if request.method == 'POST':

        if 'reset' in request.POST:
            del request.session['scenario']
            scenario = Scenario()
            formset = scenario2formset(scenario)
            request.session['scenario'] = scenario

        else:
            ScenarioFormSet = formset_factory(IndividualForm,
                                              formset=BaseScenarioFormSet,
                                              extra=0)
            formset = ScenarioFormSet(request.POST)

            #            for form in formset.cleaned_data:
            #                print form
            if formset.is_valid():
                scenario = formset2scenario(formset)

                if 'add' in request.POST:
                    scenario.addIndiv(scenario.nbIndiv(),
                                      datetime(1975, 1, 1).date(), 'vous',
                                      'chef')
                if 'remove' in request.POST:
                    scenario.rmvIndiv(scenario.nbIndiv() - 1)


#                print scenario
                formset = scenario2formset(scenario)
                request.session['scenario'] = scenario

                if 'submit' in request.POST:
                    scenario.genNbEnf()
                    ok = True
                    ok = build_simu(scenario)
                    print 'is it ok ? :', ok
                    #return (request, 'simulation/menage.html', {'formset' : formset})

    else:

        formset = scenario2formset(scenario)
        request.session['scenario'] = scenario

    return render(request, 'simulation/menage.html', {'formset': formset})
Exemple #2
0
class Compo(object):
    def __init__(self):
        super(Compo, self).__init__()
        self.scenario = Scenario()

    def nbRow(self):
        return self.scenario.nbIndiv()

    def addPerson(self):

        noi = self.nbRow()
        print noi
        if noi == 1:
            self.scenario.addIndiv(noi,
                                   birth=date(1975, 1, 1),
                                   quifoy='conj',
                                   quifam='part')
        else:
            self.scenario.addIndiv(noi,
                                   birth=date(2000, 1, 1),
                                   quifoy='pac',
                                   quifam='enf')
        print 'scenario at the end of addPerson'
        print self.scenario

    def rmvPerson(self, noi=None):
        pass

    def gen_formset(self):

        scenario = self.scenario
        print 'scenario at the beginning of gen_formset'
        print scenario
        scenario_var_list = [
            'noi', 'birth', 'idfoy', 'quifoy', 'idfam', 'quifam'
        ]

        convert = dict(idfoy="noidec", idfam="noichef")
        zero_start = ["idfoy", "idfam", "noi"]
        initial = []

        for noi, indiv in scenario.indiv.iteritems():
            new_form = {}
            for var in scenario_var_list:
                if var == "noi":
                    new_form[var] = noi
                elif var in convert.keys():
                    new_form[var] = indiv[convert[var]]
                else:
                    new_form[var] = indiv[var]
                if var in zero_start:
                    new_form[var] += 1

            print 'new_form for noi: ' + str(noi)
            print new_form
            initial.append(new_form)

        ScenarioFormSet = formset_factory(IndividualForm,
                                          formset=BaseScenarioFormSet,
                                          extra=0)

        formset = ScenarioFormSet(initial=initial)

        print formset.is_valid()
        if True:  #formset.is_valid():
            for form in formset.forms:
                print form.is_valid()
                if form.is_valid():
                    print form.cleaned_data
        return formset

    def set_logement(self, values):
        '''
        Sets logement values in scenario
        '''
        loyer = values['loyer']
        so = values['so']
        #zone_apl = values['zone_apl']
        code_postal = values['code_postal']
        self.scenario.menage[0].update({
            'loyer': int(loyer),
            'so': int(so),
            #'zone_apl': int(zone_apl),
            'code_postal': int(code_postal)
        })
Exemple #3
0
class Compo(object):
    def __init__(self):
        super(Compo, self).__init__()
        self.scenario = Scenario()

    def nbRow(self):
        return self.scenario.nbIndiv()

    def addPerson(self):
        noi = self.nbRow()
        if noi == 1:
            self.scenario.addIndiv(noi,
                                   birth=date(1975, 1, 1),
                                   quifoy='conj',
                                   quifam='part')
        else:
            self.scenario.addIndiv(noi,
                                   birth=date(2000, 1, 1),
                                   quifoy='pac',
                                   quifam='enf')

    def rmvPerson(self, noi=None):
        pass

    def gen_formset(self):

        scenario = self.scenario
        print self.scenario
        scenario_var_list = [
            'noi', 'birth', 'idfoy', 'quifoy', 'idfam', 'quifam', 'statmarit',
            'activite'
        ]

        convert = dict(idfoy="noidec", idfam="noichef")
        zero_start = ["idfoy", "idfam", "noi"]

        ScenarioFormSet = formset_factory(IndividualForm,
                                          formset=BaseScenarioFormSet)

        data = {
            'form-TOTAL_FORMS': scenario.nbIndiv(),
            'form-INITIAL_FORMS': scenario.nbIndiv(),
            'form-MAX_NUM_FORMS': u'',
        }

        for noi, indiv in scenario.indiv.iteritems():
            for var in scenario_var_list:
                if var == "noi":
                    data['form-' + str(noi) + '-' + str(var)] = noi
                elif var == "quifoy" and indiv[var][:3] == "pac":
                    data['form-' + str(noi) + '-' + str(var)] = "pac"
                elif var == "quifam" and indiv[var][:3] == "enf":
                    data['form-' + str(noi) + '-' + str(var)] = "enf"
                elif var in convert.keys():
                    data['form-' + str(noi) + '-' +
                         str(var)] = indiv[convert[var]]
                else:
                    data['form-' + str(noi) + '-' + str(var)] = indiv[var]
                if var in zero_start:
                    data['form-' + str(noi) + '-' + str(var)] += 1

        formset = ScenarioFormSet(data=data)

        return formset

    def create_declar1(self, idfoy=None):
        '''
        Creates a Declar1Form from compo.scenario data
        '''
        if idfoy is None:
            idfoy = 0

        data = dict(statmarit=2)
        for indiv in self.scenario.indiv.itervalues():
            if indiv['noidec'] == idfoy:
                if indiv['quifoy'][:3] == 'pac':
                    data['pac'] = indiv['birth']
                else:
                    data[indiv['quifoy']] = indiv['birth']

                if indiv['quifoy'] == 'vous':
                    data['statmarit'] = int(1)

        form = Declar1Form(data)

        for name, field in form.fields.iteritems():
            if name in data.keys():
                field.required = True
            else:
                field.required = False
        return form

    def create_declar3(self, idfoy=None, description=None):
        '''
        Creates a Declar3Form from compo.scenario data
        '''
        if idfoy is None:
            idfoy = 0

        if description is None:
            print 'a description should be provided'  # TODO convert this to exception

        declar = self.scenario.declar[idfoy]
        cleaned_indiv = extract_foy_indiv(self.scenario, idfoy=idfoy)
        data = declar

        def indiv2foy(varname, quifoy):
            '''
            Yields the field of the foy corresponding to the varname 
            '''
            convert = foy2of_dict()

            value = None
            for field in convert.keys():
                if field2quifoy(field) == quifoy:
                    if convert[field] == varname:
                        value = field
            return value

        for person in cleaned_indiv.itervalues():
            for varname, value in person.iteritems():
                quifoy = person['quifoy']
                field = indiv2foy(varname, quifoy)
                data[field] = value

        form = Declar3Form(data=declar, description=description)
        return form

    def create_declar(self, formClass, idfoy=None):
        '''
        Creates a Declar?Forms from compo.scenario data
        Works for Declar2Form, Declar4Form, Declar5Form 
        
        '''
        if idfoy is None:
            idfoy = 0

        declar = self.scenario.declar[idfoy]
        form = formClass(data=declar)
        return form

    def set_logement(self, values):
        '''
        Sets logement values in scenario
        '''
        code_postal = values['code_postal']
        commune, zone_apl = get_zone(code_postal)
        loyer = values['loyer']
        so = values['so']
        self.scenario.menage[0].update({
            'loyer': int(loyer),
            'so': int(so),
            'zone_apl': int(zone_apl),
            'code_postal': int(code_postal)
        })

        return commune, code_postal

    def get_declar1(self, data=None, idfoy=None):
        '''
        Sets declar1 values in compo.scenario  from a Delcar1Form
        '''
        if idfoy is None:
            idfoy = 0

        statmarit = data['statmarit']
        print self.scenario.indiv
        for indiv in self.scenario.indiv.itervalues():
            if indiv['noidec'] == idfoy:
                if indiv['quifoy'] in ['vous', 'conj']:
                    indiv['statmarit'] = statmarit


#                    indiv['birth'] = data[indiv['quifoy']]
#                else:
#                    indiv['birth'] =

    def get_declar(self, form=None, idfoy=0):
        '''
        Gets declar values in compo.scenario from DeclarForms
        Works for Declar2Form, Declar4Form, Declar5Form 
        
        '''
        declar = self.scenario.declar[idfoy]
        data = form.cleaned_data

        for field, value in data.iteritems():
            declar[field] = value

    def get_declar3(self, form=None, idfoy=0):
        '''
        Gets declar values in compo.scenario from Declar3Form
        '''
        declar = self.scenario.declar[idfoy]

        # Build a dict of individulas present on the declar
        cleaned_indiv = extract_foy_indiv(self.scenario, idfoy=idfoy)

        convert = foy2of_dict()

        data = form.cleaned_data

        for field, value in data.iteritems():
            if field in convert.keys():
                quifoy = field2quifoy(field)
                varname = convert[field]
                for person in cleaned_indiv.itervalues():
                    if person['quifoy'] == quifoy:
                        person[varname] = value
            else:
                declar[field] = value
Exemple #4
0
class Compo(object):
    def __init__(self):
        super(Compo, self).__init__()
        self.scenario = Scenario()
        

    def nbRow(self):
        return self.scenario.nbIndiv()

    def addPerson(self):
        
        noi = self.nbRow()
        print noi
        if noi == 1: self.scenario.addIndiv(noi, birth = date(1975,1,1), quifoy = 'conj', quifam = 'part')
        else:        self.scenario.addIndiv(noi, birth = date(2000,1,1), quifoy = 'pac' , quifam = 'enf')
        print 'scenario at the end of addPerson'
        print self.scenario

    def rmvPerson(self, noi = None):
        pass

    def gen_formset(self):
        
        scenario = self.scenario
        print 'scenario at the beginning of gen_formset'
        print scenario
        scenario_var_list = ['noi', 'birth', 'idfoy', 'quifoy', 'idfam', 'quifam']
        
        convert = dict(idfoy = "noidec", idfam ="noichef")
        zero_start = [ "idfoy", "idfam", "noi"]
        initial = []    
    
        for noi, indiv in scenario.indiv.iteritems():
            new_form = {}
            for var in scenario_var_list:
                if var == "noi":
                    new_form[var] = noi
                elif var in convert.keys():
                    new_form[var] = indiv[convert[var]]
                else:
                    new_form[var] = indiv[var]       
                if var in zero_start:
                    new_form[var] += 1
            
            print 'new_form for noi: ' + str(noi)        
            print new_form        
            initial.append(new_form)
            
            
        ScenarioFormSet = formset_factory(IndividualForm, formset = BaseScenarioFormSet, extra=0)
        
        formset = ScenarioFormSet(initial=initial)
        
        print formset.is_valid()
        if True: #formset.is_valid():
            for form in formset.forms:
                print form.is_valid()
                if form.is_valid():
                    print form.cleaned_data
        return formset


    def set_logement(self, values):
        '''
        Sets logement values in scenario
        '''
        loyer = values['loyer']
        so = values['so']
        #zone_apl = values['zone_apl']
        code_postal = values['code_postal']
        self.scenario.menage[0].update({'loyer': int(loyer),
                                        'so': int(so),
                                        #'zone_apl': int(zone_apl),
                                        'code_postal': int(code_postal)})
Exemple #5
0
class Compo(object):
    def __init__(self):
        super(Compo, self).__init__()
        self.scenario = Scenario()
        

    def nbRow(self):
        return self.scenario.nbIndiv()

    def addPerson(self):        
        noi = self.nbRow()
        if noi == 1: self.scenario.addIndiv(noi, birth = date(1975,1,1), quifoy = 'conj', quifam = 'part')
        else:        self.scenario.addIndiv(noi, birth = date(2000,1,1), quifoy = 'pac' , quifam = 'enf')

    def rmvPerson(self, noi = None):
        pass

    def gen_formset(self):
        
        scenario = self.scenario
        print self.scenario
        scenario_var_list = ['noi', 'birth', 'idfoy', 'quifoy', 'idfam', 'quifam', 'statmarit', 'activite']
        
        convert = dict(idfoy = "noidec", idfam ="noichef")
        zero_start = [ "idfoy", "idfam", "noi"]

        ScenarioFormSet = formset_factory(IndividualForm, formset = BaseScenarioFormSet)
        
        data = {'form-TOTAL_FORMS': scenario.nbIndiv(),
                'form-INITIAL_FORMS': scenario.nbIndiv(),
                'form-MAX_NUM_FORMS': u'',}
        
        for noi, indiv in scenario.indiv.iteritems():
            for var in scenario_var_list:
                if var == "noi":
                    data['form-' + str(noi)+'-' + str(var)] = noi
                elif var == "quifoy" and indiv[var][:3] == "pac":
                    data['form-' + str(noi)+'-' + str(var)] = "pac"
                elif var == "quifam" and indiv[var][:3] == "enf":
                    data['form-' + str(noi)+'-' + str(var)] = "enf"
                elif var in convert.keys():
                    data['form-' + str(noi)+'-' + str(var)] = indiv[convert[var]]
                else:
                    data['form-' + str(noi)+'-' + str(var)] = indiv[var]       
                if var in zero_start:
                    data['form-' + str(noi)+'-' + str(var)] += 1
                

        formset = ScenarioFormSet(data=data)
        

        return formset



    def create_declar1(self, idfoy = None):
        '''
        Creates a Declar1Form from compo.scenario data
        '''
        if idfoy is None:
            idfoy = 0
        
        data = dict(statmarit=2)
        for indiv in self.scenario.indiv.itervalues():
            if indiv['noidec'] == idfoy:            
                if indiv['quifoy'][:3] == 'pac':
                    data['pac'] = indiv['birth']
                else:
                    data[indiv['quifoy']] = indiv['birth']
                
                if indiv['quifoy'] == 'vous':
                    data['statmarit'] = int(1)
                
        
        
        
        form = Declar1Form(data)

        for name, field in form.fields.iteritems():
            if name in data.keys():
                field.required = True
            else:
                field.required = False
        return form
    
    def create_declar3(self, idfoy = None, description=None):
        '''
        Creates a Declar3Form from compo.scenario data
        '''
        if idfoy is None:
            idfoy = 0
            
        if description is None:
            print 'a description should be provided' # TODO convert this to exception
            
        declar = self.scenario.declar[idfoy]
        cleaned_indiv = extract_foy_indiv(self.scenario, idfoy=idfoy)
        data = declar
        
        def indiv2foy(varname, quifoy):
            '''
            Yields the field of the foy corresponding to the varname 
            '''
            convert = foy2of_dict()
            
            value = None
            for field in convert.keys():
                if field2quifoy(field) == quifoy:    
                    if convert[field] == varname:
                        value = field
            return value
            
        for person in cleaned_indiv.itervalues():
            for varname, value in person.iteritems():
                quifoy = person['quifoy']
                field = indiv2foy(varname, quifoy)
                data[field]  = value
            
        form = Declar3Form(data=declar, description=description)
        return form        

            
    def create_declar(self, formClass, idfoy = None):
        '''
        Creates a Declar?Forms from compo.scenario data
        Works for Declar2Form, Declar4Form, Declar5Form 
        
        '''
        if idfoy is None:
            idfoy = 0
            
        declar = self.scenario.declar[idfoy]
        form = formClass(data=declar)
        return form
    


    def set_logement(self, values):
        '''
        Sets logement values in scenario
        '''
        code_postal = values['code_postal']
        commune, zone_apl = get_zone(code_postal)
        loyer = values['loyer']
        so = values['so']
        self.scenario.menage[0].update({'loyer': int(loyer),
                                        'so': int(so),
                                        'zone_apl': int(zone_apl),
                                        'code_postal': int(code_postal)})

        return commune, code_postal

    def get_declar1(self, data = None, idfoy = None):
        '''
        Sets declar1 values in compo.scenario  from a Delcar1Form
        '''
        if idfoy is None:
            idfoy = 0

        statmarit = data['statmarit']
        print self.scenario.indiv
        for indiv in self.scenario.indiv.itervalues(): 
            if indiv['noidec'] == idfoy:
                if indiv['quifoy'] in ['vous', 'conj']:
                    indiv['statmarit'] = statmarit
#                    indiv['birth'] = data[indiv['quifoy']]
#                else:
#                    indiv['birth'] = 

    def get_declar(self, form = None, idfoy = 0):
        '''
        Gets declar values in compo.scenario from DeclarForms
        Works for Declar2Form, Declar4Form, Declar5Form 
        
        '''
        declar = self.scenario.declar[idfoy]
        data = form.cleaned_data
        
        for field, value in data.iteritems(): 
            declar[field] = value

    def get_declar3(self, form = None, idfoy = 0):
        '''
        Gets declar values in compo.scenario from Declar3Form
        '''
        declar = self.scenario.declar[idfoy]

        # Build a dict of individulas present on the declar
        cleaned_indiv = extract_foy_indiv(self.scenario, idfoy= idfoy)

        convert = foy2of_dict()
                        
        data = form.cleaned_data
        
        for field, value in data.iteritems(): 
            if field in convert.keys():
                quifoy  = field2quifoy(field)
                varname = convert[field]
                for person in cleaned_indiv.itervalues():
                    if person['quifoy'] == quifoy:
                        person[varname] = value
            else:
                declar[field] = value