From 4162b84d564cbf4749224d6107cddaea26033d73 Mon Sep 17 00:00:00 2001 From: Étienne Loks Date: Tue, 21 Jul 2015 17:47:46 +0200 Subject: Manage lan planner organizations (refs #2671) --- archaeological_files_pdl/forms.py | 96 ++++++++++++++++++++++++++------------- 1 file changed, 65 insertions(+), 31 deletions(-) (limited to 'archaeological_files_pdl/forms.py') diff --git a/archaeological_files_pdl/forms.py b/archaeological_files_pdl/forms.py index 102275d64..1f2dfd708 100644 --- a/archaeological_files_pdl/forms.py +++ b/archaeological_files_pdl/forms.py @@ -30,7 +30,8 @@ from archaeological_files import models from ishtar_common.forms import get_now, reverse_lazy from ishtar_common.forms_common import get_advanced_town_field from archaeological_files.forms import GENERAL_CONTRACTOR, \ - RESPONSIBLE_PLANNING_SERVICE + GENERAL_CONTRACTOR_ORGA, RESPONSIBLE_PLANNING_SERVICE, \ + RESPONSIBLE_PLANNING_SERVICE_ORGA from ishtar_common import widgets @@ -109,8 +110,11 @@ class FileFormResearchAddress(forms.Form): class PersonOrgaForm(forms.Form): PERSON_FIELD = 'TO BE DEFINED' - ORGA_FIELD = 'TO BE DEFINED' PERSON_TYPE = GENERAL_CONTRACTOR + PERSON_LABEL = "" + ORGA_FIELD = 'TO BE DEFINED' + ORGA_TYPE = GENERAL_CONTRACTOR_ORGA + ORGA_LABEL = "" def _media(self): if self.status == 'corporation': @@ -118,24 +122,34 @@ class PersonOrgaForm(forms.Form): media = property(_media) def __init__(self, *args, **kwargs): - # get the status: natural person or corporation DEFAULT_STATUS = 'natural' current_status = '' if 'data' in kwargs: - current_item_key = ((kwargs['prefix'] + '-') if kwargs.get('prefix')\ - else '') + self.PERSON_FIELD - if kwargs['data'] and kwargs['data'].get(current_item_key): - model = self.associated_models[self.PERSON_FIELD] + # the order is important: PERSON can have an ORGA + for field in [self.ORGA_FIELD, self.PERSON_FIELD]: + current_item_key = ((kwargs['prefix'] + '-') \ + if kwargs.get('prefix') else '') + field + if kwargs['data'] and kwargs['data'].get(current_item_key): + model = self.associated_models[field] + try: + item = model.objects.get(pk=kwargs['data'][current_item_key]) + current_status = 'natural' if field == self.PERSON_FIELD \ + else 'corporation' + except (model.DoesNotExist, ValueError): + pass + initial = kwargs.get("initial", {}) + if not current_status: + # the order is important: PERSON can have an ORGA + for field in [self.ORGA_FIELD, self.PERSON_FIELD]: + value = initial.get(field) + model = self.associated_models[field] try: - item = model.objects.get(pk=kwargs['data'][current_item_key]) - current_status = 'natural' + item = model.objects.get(pk=value) + current_status = 'natural' if field == self.PERSON_FIELD \ + else 'corporation' except (model.DoesNotExist, ValueError): pass - current_item_key = ((kwargs['prefix'] + '-') if kwargs.get('prefix')\ - else '') + self.ORGA_FIELD - if kwargs['data'] and kwargs['data'].get(current_item_key): - current_status = 'corporation' status = '' if 'status' in kwargs: @@ -153,13 +167,15 @@ class PersonOrgaForm(forms.Form): if status not in ('natural', 'corporation'): status = DEFAULT_STATUS + super(PersonOrgaForm, self).__init__(*args, **kwargs) # distinct widget for natural and corporation if status == 'natural': self.fields[self.PERSON_FIELD] = forms.IntegerField( - label=self.fields[self.ORGA_FIELD].label, + label=self.PERSON_LABEL, required=False, + initial= initial.get(self.PERSON_FIELD, None), widget=widgets.JQueryPersonOrganization( reverse_lazy('autocomplete-person', args=[self.PERSON_TYPE.pk]), @@ -170,37 +186,55 @@ class PersonOrgaForm(forms.Form): js_template='ishtar/blocks/JQueryNaturalPerson.js', new=True), validators=[valid_id(Person)]) + else: + self.fields[self.ORGA_FIELD] = forms.IntegerField( + label=self.ORGA_LABEL, + required=False, + initial= initial.get(self.ORGA_FIELD, None), + widget=widgets.JQueryAutoComplete( + reverse_lazy('autocomplete-organization', + args=[self.ORGA_TYPE.pk]), + limit={'organization_type':[self.ORGA_TYPE.pk]}, + associated_model=models.Organization, + new=True), + validators=[valid_id(models.Organization)]) class FileFormGeneralContractor(PersonOrgaForm): form_label = _(u"General contractor") - associated_models = {'general_contractor':models.Person} - raw_general_contractor = forms.CharField(label=_(u"General contractor"), - required=False, max_length=200) + associated_models = {'general_contractor':models.Person, + 'corporation_general_contractor':models.Organization} PERSON_FIELD = 'general_contractor' - ORGA_FIELD = 'raw_general_contractor' PERSON_TYPE = GENERAL_CONTRACTOR + PERSON_LABEL = _(u"General contractor") + ORGA_FIELD = 'corporation_general_contractor' + ORGA_TYPE = GENERAL_CONTRACTOR_ORGA + ORGA_LABEL = _(u"General contractor") + + # default initialisation before dynamic + general_contractor = forms.IntegerField(label=" ", widget=forms.HiddenInput, + required=False) + corporation_general_contractor = forms.IntegerField(label=" ", + widget=forms.HiddenInput, required=False) class FileFormPlanningService(PersonOrgaForm): form_label = _(u"Town planning service") - associated_models = {'responsible_town_planning_service':models.Person} + associated_models = {'responsible_town_planning_service':models.Person, + 'planning_service':models.Organization} reference_number = forms.IntegerField(label=_(u"File reference"), required=False) - raw_town_planning_service = forms.CharField( - label=_(u"Responsible town planning service"), required=False, - max_length=200) PERSON_FIELD = 'responsible_town_planning_service' - ORGA_FIELD = 'raw_town_planning_service' PERSON_TYPE = RESPONSIBLE_PLANNING_SERVICE + PERSON_LABEL = _(u"Responsible town planning service") + ORGA_FIELD = 'planning_service' + ORGA_TYPE = RESPONSIBLE_PLANNING_SERVICE_ORGA + ORGA_LABEL = _(u"Planning service") - def __init__(self, *args, **kwargs): - super(FileFormPlanningService, self).__init__(*args, **kwargs) - self.fields.keyOrder = [] - if self.ORGA_FIELD in self.fields: - self.fields.keyOrder.append(self.ORGA_FIELD) - elif self.PERSON_FIELD in self.fields: - self.fields.keyOrder.append(self.PERSON_FIELD) - self.fields.keyOrder.append('reference_number') + # default initialisation before dynamic + responsible_town_planning_service = forms.IntegerField(label=" ", + widget=forms.HiddenInput, required=False) + planning_service = forms.IntegerField(label=" ", widget=forms.HiddenInput, + required=False) class FileFormInstruction(forms.Form): form_label = u"Instruction SRA" -- cgit v1.2.3