diff options
author | Étienne Loks <etienne.loks@iggdrasil.net> | 2023-10-18 12:48:05 +0200 |
---|---|---|
committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2023-10-18 12:50:55 +0200 |
commit | 20a8009562b4d03b334469fbe2902386be3371f4 (patch) | |
tree | 339fb76b82e542887d21a123a53c23631c42c9f3 | |
parent | 249b6c73e645674d8ca699be25a138bceacd477d (diff) | |
download | Ishtar-20a8009562b4d03b334469fbe2902386be3371f4.tar.bz2 Ishtar-20a8009562b4d03b334469fbe2902386be3371f4.zip |
🐛 Custom forms: fix crash when removing fields already filtered
-rw-r--r-- | archaeological_context_records/forms.py | 2 | ||||
-rw-r--r-- | archaeological_files/forms.py | 4 | ||||
-rw-r--r-- | archaeological_finds/forms.py | 9 | ||||
-rw-r--r-- | archaeological_operations/forms.py | 40 | ||||
-rw-r--r-- | ishtar_common/forms.py | 10 |
5 files changed, 40 insertions, 25 deletions
diff --git a/archaeological_context_records/forms.py b/archaeological_context_records/forms.py index e22bb73cc..24a94f2f5 100644 --- a/archaeological_context_records/forms.py +++ b/archaeological_context_records/forms.py @@ -403,7 +403,7 @@ class RecordFormGeneral(CustomForm, ManageOldType): self.fields["parcel"].required = False self.fields["town"].choices = [("", "--")] else: - self.fields.pop("town") + self._remove_fields(("town",)) site_label = IshtarSiteProfile.get_default_site_label() self.fields["archaeological_site"].label = site_label diff --git a/archaeological_files/forms.py b/archaeological_files/forms.py index 221004a66..2548d5f71 100644 --- a/archaeological_files/forms.py +++ b/archaeological_files/forms.py @@ -837,9 +837,9 @@ class FileFormPreventiveDetail(forms.ModelForm, CustomForm, ManageOldType): self.user = kwargs.pop("user") super(FileFormPreventiveDetail, self).__init__(*args, **kwargs) if not models.AgreementType.objects.count(): - self.fields.pop("type_of_agreement") + self._remove_fields(("type_of_agreement",)) if not models.OperationTypeForRoyalties.objects.count(): - self.fields.pop("operation_type_for_royalties") + self._remove_fields(("operation_type_for_royalties",)) class FileBaseFormset(forms.BaseModelFormSet): diff --git a/archaeological_finds/forms.py b/archaeological_finds/forms.py index 9bc3f56ff..691383cc6 100644 --- a/archaeological_finds/forms.py +++ b/archaeological_finds/forms.py @@ -415,9 +415,9 @@ class BasicFindForm(CustomForm, ManageOldType): def __init__(self, *args, **kwargs): context_record = kwargs.pop("context_record") - super(BasicFindForm, self).__init__(*args, **kwargs) + super().__init__(*args, **kwargs) if not context_record or not context_record.operation.operation_type.judiciary: - self.fields.pop("seal_number") + self._remove_fields(("seal_number",)) def clean(self): clutter_long_side = self.cleaned_data.get("clutter_long_side", None) @@ -875,19 +875,20 @@ class QAFindFormMulti(QAForm): class QAFindFormSingle(QAFindFormMulti): + # TODO: not used? MULTI = False form_admin_name = _("Find - Quick action - Modify single") form_slug = "find-quickaction-modifysingle" def __init__(self, *args, **kwargs): - super(QAFindFormSingle, self).__init__(*args, **kwargs) + super().__init__(*args, **kwargs) if ( not self.items or not self.items[0] .get_first_base_find() .context_record.operation.operation_type.judiciary ): - self.fields.pop("qa_seal_number") + self._remove_fields(("qa_seal_number",)) class QAFindBasketForm(IshtarForm): diff --git a/archaeological_operations/forms.py b/archaeological_operations/forms.py index 2104938c2..97a479505 100644 --- a/archaeological_operations/forms.py +++ b/archaeological_operations/forms.py @@ -594,17 +594,19 @@ class OperationSelect(GeoItemSelect): SITE_KEYS = {"archaeological_sites": None} def __init__(self, *args, **kwargs): - super(OperationSelect, self).__init__(*args, **kwargs) + super().__init__(*args, **kwargs) profile = get_current_profile() if not profile.warehouse: - self.fields.pop('documentation_deadline_before') - self.fields.pop('documentation_deadline_after') - self.fields.pop('documentation_received') - self.fields.pop('finds_deadline_before') - self.fields.pop('finds_deadline_after') - self.fields.pop('finds_received') + self._remove_fields(( + 'documentation_deadline_before', + 'documentation_deadline_after', + 'documentation_received', + 'finds_deadline_before', + 'finds_deadline_after', + 'finds_received', + )) if not profile.underwater: - self.fields.pop('drassm_code') + self._remove_fields(("drassm_code",)) if settings.ISHTAR_DPTS: k = 'towns__numero_insee__startswith' self.fields[k].choices = [ @@ -865,7 +867,7 @@ class OperationFormGeneral(CustomForm, ManageOldType): for key in self.WAREHOUSE_FIELDS: self.remove_field(key) if not profile.underwater: - self.fields.pop('drassm_code') + self._remove_fields(("drassm_code",)) if 'collaborator' in self.fields: self.fields['collaborator'].widget.attrs['full-width'] = True if towns and towns != -1: @@ -939,7 +941,7 @@ class OperationFormModifGeneral(OperationFormGeneral): def __init__(self, *args, **kwargs): super(OperationFormModifGeneral, self).__init__(*args, **kwargs) if not get_current_profile().files: - self.fields.pop('associated_file') + self._remove_fields(("associated_file",)) fields = OrderedDict() for idx, field in enumerate(list(self.fields.items())): @@ -1269,15 +1271,17 @@ class SiteSelect(GeoItemSelect): ] + GeoItemSelect.TYPES def __init__(self, *args, **kwargs): - super(SiteSelect, self).__init__(*args, **kwargs) + super().__init__(*args, **kwargs) if not get_current_profile().underwater: - self.fields.pop('shipwreck_name') - self.fields.pop('oceanographic_service_localisation') - self.fields.pop('shipwreck_code') - self.fields.pop('sinking_date') - self.fields.pop('discovery_area') - self.fields.pop('affmar_number') - self.fields.pop('drassm_number') + self._remove_fields(( + 'shipwreck_name', + 'oceanographic_service_localisation', + 'shipwreck_code', + 'sinking_date', + 'discovery_area', + 'affmar_number', + 'drassm_number', + )) class SiteFormSelection(LockForm, CustomFormSearch): diff --git a/ishtar_common/forms.py b/ishtar_common/forms.py index b0bf64b46..0f1263bef 100644 --- a/ishtar_common/forms.py +++ b/ishtar_common/forms.py @@ -195,6 +195,16 @@ class CustomForm(BSForm): if not self._explicit_ordering: self.custom_form_ordering() + def _remove_fields(self, field_names): + """ + Remove field (if already present) + """ + if not getattr(self, "fields", None): + return + for name in field_names: + if name in self.fields: + self.fields.pop(name) + def custom_form_ordering(self): available, header, excluded, json_fields = self.check_custom_form( self.current_user) |