summaryrefslogtreecommitdiff
path: root/archaeological_warehouse
diff options
context:
space:
mode:
authorÉtienne Loks <etienne.loks@iggdrasil.net>2020-06-24 17:37:59 +0200
committerÉtienne Loks <etienne.loks@iggdrasil.net>2021-02-28 12:15:20 +0100
commit2f6802085f89cf947a60da17f6e2b47a580abc4b (patch)
tree769b24f68103e05ce23850733f6925c8dd7bf62d /archaeological_warehouse
parente4523a6d05ecfa1c1e3da3fa29f1169cc42228ca (diff)
downloadIshtar-2f6802085f89cf947a60da17f6e2b47a580abc4b.tar.bz2
Ishtar-2f6802085f89cf947a60da17f6e2b47a580abc4b.zip
Container - batch modification: check db constraint uniqueness on validation (refs #4951)
Diffstat (limited to 'archaeological_warehouse')
-rw-r--r--archaeological_warehouse/forms.py41
-rw-r--r--archaeological_warehouse/views.py6
2 files changed, 47 insertions, 0 deletions
diff --git a/archaeological_warehouse/forms.py b/archaeological_warehouse/forms.py
index 9c39c61c5..6ea1ae4f6 100644
--- a/archaeological_warehouse/forms.py
+++ b/archaeological_warehouse/forms.py
@@ -593,6 +593,7 @@ class QAContainerFormMulti(QAForm):
]
def __init__(self, *args, **kwargs):
+ self.items = kwargs["items"]
super(QAContainerFormMulti, self).__init__(*args, **kwargs)
locations = set([item.location_id for item in self.items])
if len(locations) == 1:
@@ -609,3 +610,43 @@ class QAContainerFormMulti(QAForm):
return models.Container.objects.get(pk=value).cached_label
except models.Container.DoesNotExist:
return ""
+
+ def clean(self):
+ new_values = {}
+ if self.cleaned_data.get("qacontainer_type", None):
+ new_values["container_type_id"] = self.cleaned_data[
+ "qacontainer_type"]
+ if self.cleaned_data.get("qalocation", None):
+ new_values["location_id"] = self.cleaned_data[
+ "qalocation"]
+ if self.cleaned_data.get("qaparent", None):
+ new_values["parent_id"] = self.cleaned_data[
+ "qaparent"]
+ new_tuples = []
+ for item in self.items:
+ vals = {
+ "container_type_id": item.container_type_id,
+ "location_id": item.location_id,
+ "parent_id": item.parent_id,
+ "reference": item.reference.strip()
+ }
+ vals.update(new_values)
+ c_tuple = (vals["location_id"], vals["container_type_id"],
+ vals["parent_id"], vals["reference"])
+ q = models.Container.objects.filter(**vals).exclude(id=item.id)
+ if c_tuple in new_tuples or q.count():
+ parent = models.Container.objects.get(pk=vals["parent_id"])
+ raise forms.ValidationError(
+ str(
+ _("Cannot do this changes because it would generate "
+ "many containers with location: {}, container type: "
+ "{}, parent: {} {} and reference: {}. "
+ "Merge theses containers first?")).format(
+ models.Warehouse.objects.get(pk=vals["location_id"]),
+ models.ContainerType.objects.get(
+ pk=vals["container_type_id"]),
+ parent.container_type, parent.reference,
+ vals["reference"]
+ )
+ )
+ return self.cleaned_data
diff --git a/archaeological_warehouse/views.py b/archaeological_warehouse/views.py
index d02b1d51e..6675addca 100644
--- a/archaeological_warehouse/views.py
+++ b/archaeological_warehouse/views.py
@@ -299,3 +299,9 @@ def reset_wizards(request):
class QAContainerForm(QAItemEditForm):
model = models.Container
form_class = forms.QAContainerFormMulti
+
+ def get_form_kwargs(self):
+ kwargs = super(QAContainerForm, self).get_form_kwargs()
+ # item list is necessary to verify unicity rules
+ kwargs['items'] = self.items
+ return kwargs