diff options
author | Étienne Loks <etienne.loks@iggdrasil.net> | 2024-01-10 15:58:30 +0100 |
---|---|---|
committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2024-02-05 10:56:44 +0100 |
commit | 8b23fe52c07114ca43a0d0c18182d60ad03aa78d (patch) | |
tree | c0f1e33f76005c2372b86da8bf9d764ded71cdbf /archaeological_warehouse/forms.py | |
parent | 1070a8a4f0232b2d3fda1e21a143f63ef72a1a9d (diff) | |
download | Ishtar-8b23fe52c07114ca43a0d0c18182d60ad03aa78d.tar.bz2 Ishtar-8b23fe52c07114ca43a0d0c18182d60ad03aa78d.zip |
🗃️ Container: new field Code (forms, sheet, search index and autocomplete) (refs #5704)
Diffstat (limited to 'archaeological_warehouse/forms.py')
-rw-r--r-- | archaeological_warehouse/forms.py | 48 |
1 files changed, 27 insertions, 21 deletions
diff --git a/archaeological_warehouse/forms.py b/archaeological_warehouse/forms.py index acb2a2e50..f083785b5 100644 --- a/archaeological_warehouse/forms.py +++ b/archaeological_warehouse/forms.py @@ -337,6 +337,7 @@ class ContainerForm(CustomForm, ManageOldType, forms.Form): "parent": models.Container, } reference = forms.CharField(label=_("Ref."), max_length=200) + code = forms.CharField(label=_("Code"), max_length=200, required=False) old_reference = forms.CharField( label=_("Old reference"), required=False, max_length=200 ) @@ -407,28 +408,32 @@ class ContainerForm(CustomForm, ManageOldType, forms.Form): def clean(self): cleaned_data = self.cleaned_data warehouse = cleaned_data.get("location") - q = models.Container.objects.filter( - reference=cleaned_data.get("reference"), - location__pk=warehouse, - container_type_id=cleaned_data.get("container_type"), - parent_id=cleaned_data.get("parent"), - ) - pk = None - if "pk" in cleaned_data and cleaned_data["pk"]: - pk = int(cleaned_data["pk"]) - q = q.exclude(pk=pk) - if q.count(): - raise forms.ValidationError( - _("This reference already exists for " "this warehouse.") - ) - if ( - pk - and cleaned_data.get("parent", None) - and pk == int(cleaned_data.get("parent")) - ): - raise forms.ValidationError( - _("A container cannot be a parent of " "himself.") + for ref_attr in ("reference", "code"): + ref = cleaned_data.get(ref_attr, None) + if not ref: + continue + q = models.Container.objects.filter( + location__pk=warehouse, + container_type_id=cleaned_data.get("container_type"), + parent_id=cleaned_data.get("parent"), ) + q = q.filter(**{ref_attr: ref}) + pk = None + if "pk" in cleaned_data and cleaned_data["pk"]: + pk = int(cleaned_data["pk"]) + q = q.exclude(pk=pk) + if q.count(): + raise forms.ValidationError( + _("This reference/code already exists for this warehouse.") + ) + if (ref_attr == "reference" # only check parent for reference + and pk + and cleaned_data.get("parent", None) + and pk == int(cleaned_data.get("parent")) + ): + raise forms.ValidationError( + _("A container cannot be a parent of himself.") + ) return cleaned_data def save(self, user): @@ -509,6 +514,7 @@ class ContainerSelect(DocumentItemSelect): responsibility_name = get_warehouse_field(label=_("Warehouse (responsibility)")) container_type = forms.ChoiceField(label=_("Container type"), choices=[]) reference = forms.CharField(label=_("Ref.")) + code = forms.CharField(label=_("Code")) old_reference = forms.CharField(label=_("Old reference")) comment = forms.CharField(label=_("Comment")) contain_containers = forms.NullBooleanField(label=_("Contain containers")) |