summaryrefslogtreecommitdiff
path: root/archaeological_warehouse/forms.py
diff options
context:
space:
mode:
authorÉtienne Loks <etienne.loks@iggdrasil.net>2024-01-10 15:58:30 +0100
committerÉtienne Loks <etienne.loks@iggdrasil.net>2024-04-16 16:40:54 +0200
commitd2783f68a60eff33535448e07484bbaf92312ff8 (patch)
tree56cb5f25e3e2a38a583ff0bb53e6e3a7cde09461 /archaeological_warehouse/forms.py
parent1e0ba5c083005ecbfde7b6218e5f46a6a06012d2 (diff)
downloadIshtar-d2783f68a60eff33535448e07484bbaf92312ff8.tar.bz2
Ishtar-d2783f68a60eff33535448e07484bbaf92312ff8.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.py48
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"))