diff options
author | Étienne Loks <etienne.loks@iggdrasil.net> | 2019-06-27 17:20:43 +0200 |
---|---|---|
committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2019-06-27 17:20:43 +0200 |
commit | 3d766fae8dd27b097eadd66993a091aa32af1aec (patch) | |
tree | 9201a1616398d4145fe3836821c3ccca788d817d /archaeological_warehouse/forms.py | |
parent | 7186a3adae39105729e31d0c7b594fcbcbdfd091 (diff) | |
download | Ishtar-3d766fae8dd27b097eadd66993a091aa32af1aec.tar.bz2 Ishtar-3d766fae8dd27b097eadd66993a091aa32af1aec.zip |
Warehouse: link warehouse to an organization - manage address dependencies
Diffstat (limited to 'archaeological_warehouse/forms.py')
-rw-r--r-- | archaeological_warehouse/forms.py | 56 |
1 files changed, 49 insertions, 7 deletions
diff --git a/archaeological_warehouse/forms.py b/archaeological_warehouse/forms.py index 1679c9d0b..58d856844 100644 --- a/archaeological_warehouse/forms.py +++ b/archaeological_warehouse/forms.py @@ -26,7 +26,8 @@ from django.conf import settings from django.forms.formsets import formset_factory from django.utils.translation import ugettext_lazy as _ -from ishtar_common.models import Person, valid_id, Town, SpatialReferenceSystem +from ishtar_common.models import Person, valid_id, Town, \ + SpatialReferenceSystem, Organization, OrganizationType from archaeological_operations.models import ArchaeologicalSite from archaeological_context_records.models import ContextRecord from archaeological_finds.models import TreatmentType, FindBasket, \ @@ -129,6 +130,7 @@ class WarehouseForm(CustomForm, ManageOldType, forms.Form): associated_models = { 'warehouse_type': models.WarehouseType, 'person_in_charge': Person, + 'organization': Organization, 'precise_town': Town, 'spatial_reference_system': SpatialReferenceSystem } @@ -137,15 +139,33 @@ class WarehouseForm(CustomForm, ManageOldType, forms.Form): validators=[name_validator]) warehouse_type = forms.ChoiceField(label=_(u"Warehouse type"), choices=[]) + organization = forms.IntegerField( + label=_("Organization"), + widget=widgets.JQueryAutoComplete( + reverse_lazy('autocomplete-organization'), + associated_model=Organization, new=True), + validators=[valid_id(Organization)], + required=False) person_in_charge = forms.IntegerField( - label=_(u"Person in charge"), + label=_("Person in charge"), widget=widgets.JQueryAutoComplete( reverse_lazy('autocomplete-person'), associated_model=Person, new=True), validators=[valid_id(Person)], required=False) + create_organization = forms.BooleanField( + label=_("Create a new organization from this warehouse"), + required=False + ) comment = forms.CharField(label=_(u"Comment"), widget=forms.Textarea, required=False) + HEADERS['address'] = FormHeader( + _(u"Address"), collapse=True, + help_message=_( + "Only fill the following fields if no organization is provided or " + "if the address of the warehouse is different from the one of the " + "organization. If a new organization is created from this " + "warehouse, the following fields are used for the organization.")) address = forms.CharField(label=_(u"Address"), widget=forms.Textarea, required=False) address_complement = forms.CharField(label=_(u"Address complement"), @@ -160,11 +180,11 @@ class WarehouseForm(CustomForm, ManageOldType, forms.Form): phone = forms.CharField(label=_(u"Phone"), max_length=18, required=False) mobile_phone = forms.CharField(label=_(u"Mobile phone"), max_length=18, required=False) - HEADERS['x'] = FormHeader(_(u"Coordinates")) - x = forms.FloatField(label=_(u"X"), required=False) - y = forms.FloatField(label=_(u"Y"), required=False) + HEADERS['x'] = FormHeader(_("Coordinates")) + x = forms.FloatField(label=_("X"), required=False) + y = forms.FloatField(label=_("Y"), required=False) spatial_reference_system = forms.ChoiceField( - label=_(u"Spatial Reference System"), required=False, choices=[]) + label=_("Spatial Reference System"), required=False, choices=[]) TYPES = [ FieldType('warehouse_type', models.WarehouseType), @@ -176,21 +196,43 @@ class WarehouseForm(CustomForm, ManageOldType, forms.Form): kwargs.pop('limits') super(WarehouseForm, self).__init__(*args, **kwargs) + def clean(self): + if self.cleaned_data.get("organization", None) and \ + self.cleaned_data.get("create_organization", None): + raise forms.ValidationError( + _("A new organization is not created if an organization is " + "selected.")) + return self.cleaned_data + def save(self, user): dct = self.cleaned_data dct['history_modifier'] = user dct['warehouse_type'] = models.WarehouseType.objects.get( pk=dct['warehouse_type']) if 'person_in_charge' in dct and dct['person_in_charge']: - dct['person_in_charge'] = models.Person.objects.get( + dct['person_in_charge'] = Person.objects.get( pk=dct['person_in_charge']) + if 'organization' in dct and dct['organization']: + dct['organization'] = Organization.objects.get( + pk=dct['organization']) if not dct.get("spatial_reference_system", None): dct.pop("spatial_reference_system") + create_orga = dct.pop("create_organization") new_item = models.Warehouse(**dct) new_item.save() + if not create_orga: + return new_item + + new_item.create_attached_organization() return new_item +class WarehouseModifyForm(WarehouseForm): + def __init__(self, *args, **kwargs): + super(WarehouseModifyForm, self).__init__(*args, **kwargs) + self.fields.pop("create_organization") + + class WarehouseDeletionForm(FinalForm): confirm_msg = _(u"Would you like to delete this warehouse?") confirm_end_msg = _(u"Would you like to delete this warehouse?") |