diff options
| -rw-r--r-- | archaeological_context_records/forms.py | 26 | ||||
| -rw-r--r-- | archaeological_operations/forms.py | 6 | 
2 files changed, 20 insertions, 12 deletions
diff --git a/archaeological_context_records/forms.py b/archaeological_context_records/forms.py index 162c22f40..698484f50 100644 --- a/archaeological_context_records/forms.py +++ b/archaeological_context_records/forms.py @@ -27,7 +27,7 @@ from django import forms  from django.conf import settings  from django.core import validators  from django.forms.formsets import formset_factory -from ishtar_common.utils import ugettext_lazy as _ +from ishtar_common.utils import get_current_profile, ugettext_lazy as _  from ishtar_common.models import (      valid_id, @@ -188,7 +188,9 @@ class RecordSelect(GeoItemSelect, PeriodSelect):      SITE_KEYS = {"archaeological_site": None}      def __init__(self, *args, **kwargs): -        super(RecordSelect, self).__init__(*args, **kwargs) +        super().__init__(*args, **kwargs) +        if not get_current_profile().archaeological_site: +            self._remove_fields(("archaeological_site",))          self._reorder_period_fields("unit")      def get_input_ids(self): @@ -399,25 +401,27 @@ class RecordFormGeneral(CustomForm, ManageOldType):                  kwargs["data"] = None                  if "files" in kwargs:                      kwargs.pop("files") -        super(RecordFormGeneral, self).__init__(*args, **kwargs) -        profile = IshtarSiteProfile.get_current_profile() +        super().__init__(*args, **kwargs) +        profile = get_current_profile()          self.fields["parcel"].choices = [("", "--")]          if not profile.parcel_mandatory:              self.fields["parcel"].required = False              self.fields["town"].choices = [("", "--")]          else:              self._remove_fields(("town",)) - -        site_label = IshtarSiteProfile.get_default_site_label() -        self.fields["archaeological_site"].label = site_label -        self.fields["archaeological_site"].choices = [("", "--")] +        sites = [] +        if operation: +            sites = [(site.pk, str(site)) for site in operation.archaeological_sites.all()] +        if not sites or not profile.archaeological_site: +            self._remove_fields(("archaeological_site",)) +        else: +            site_label = IshtarSiteProfile.get_default_site_label() +            self.fields["archaeological_site"].label = site_label +            self.fields["archaeological_site"].choices = [("", "--")] + sites          if operation:              self.fields["operation_id"].initial = operation.pk              get_init_parcel(self, operation) -            self.fields["archaeological_site"].choices += [ -                (site.pk, str(site)) for site in operation.archaeological_sites.all() -            ]      def clean(self):          # manage unique context record ID diff --git a/archaeological_operations/forms.py b/archaeological_operations/forms.py index 6b0959a91..af3c4bb3d 100644 --- a/archaeological_operations/forms.py +++ b/archaeological_operations/forms.py @@ -872,7 +872,7 @@ class OperationFormGeneral(CustomForm, ManageOldType):          towns = None          if kwargs.get("data", None) and kwargs['data'].get("TOWNS", None):              towns = kwargs['data']['TOWNS'] -        super(OperationFormGeneral, self).__init__(*args, **kwargs) +        super().__init__(*args, **kwargs)          profile = get_current_profile()          if not profile.files:              for key in self.FILE_FIELDS: @@ -882,6 +882,10 @@ class OperationFormGeneral(CustomForm, ManageOldType):                  self.remove_field(key)          if not profile.underwater:              self._remove_fields(("drassm_code",)) +        data = kwargs.get("data", {}) or kwargs.get("initial", {}) +        has_site = any(1 for k, v in data.items() if v and k.split("-")[-1] == "archaeological_site") +        if not has_site and not profile.archaeological_site: +            self._remove_fields(("archaeological_site",))          if 'collaborator' in self.fields:              self.fields['collaborator'].widget.attrs['full-width'] = True          if towns and towns != -1:  | 
