diff options
author | Étienne Loks <etienne.loks@iggdrasil.net> | 2019-06-12 12:24:29 +0200 |
---|---|---|
committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2019-06-17 13:22:54 +0200 |
commit | 72f9aea6da09a91e0fca6d6b5b28d214207eb00a (patch) | |
tree | 08bd01af358f529605479acf64b74b3c747157db /archaeological_warehouse/forms.py | |
parent | 8285022df29732ef16ff50f53501b75ff337a30c (diff) | |
download | Ishtar-72f9aea6da09a91e0fca6d6b5b28d214207eb00a.tar.bz2 Ishtar-72f9aea6da09a91e0fca6d6b5b28d214207eb00a.zip |
Container form: allow to remove index - try to keep localisation when changing the location
Diffstat (limited to 'archaeological_warehouse/forms.py')
-rw-r--r-- | archaeological_warehouse/forms.py | 30 |
1 files changed, 20 insertions, 10 deletions
diff --git a/archaeological_warehouse/forms.py b/archaeological_warehouse/forms.py index 00d14425b..1679c9d0b 100644 --- a/archaeological_warehouse/forms.py +++ b/archaeological_warehouse/forms.py @@ -21,6 +21,7 @@ from collections import OrderedDict import datetime from django import forms +from django.db.models import Max from django.conf import settings from django.forms.formsets import formset_factory from django.utils.translation import ugettext_lazy as _ @@ -259,7 +260,7 @@ class ContainerForm(CustomForm, ManageOldType, forms.Form): class ContainerModifyForm(ContainerForm): pk = forms.IntegerField(required=False, widget=forms.HiddenInput) - index = forms.IntegerField(label=_(u"ID")) + index = forms.IntegerField(label=_(u"ID"), required=False) def __init__(self, *args, **kwargs): super(ContainerModifyForm, self).__init__(*args, **kwargs) @@ -274,15 +275,23 @@ class ContainerModifyForm(ContainerForm): def clean(self): # manage unique ID cleaned_data = super(ContainerModifyForm, self).clean() - index = cleaned_data.get("index") + index = cleaned_data.get("index", None) warehouse = cleaned_data.get("location") - q = models.Container.objects.filter( - index=index, location__pk=warehouse) - if 'pk' in cleaned_data and cleaned_data['pk']: - q = q.exclude(pk=int(cleaned_data['pk'])) - if q.count(): - raise forms.ValidationError(_(u"This ID already exists for " - u"this warehouse.")) + if not index: + q = models.Container.objects.filter(location__pk=warehouse) + if not q.count(): + cleaned_data["index"] = 1 + else: + cleaned_data["index"] = q.all().aggregate( + Max("index"))["index__max"] + 1 + else: + q = models.Container.objects.filter( + index=index, location__pk=warehouse) + if 'pk' in cleaned_data and cleaned_data['pk']: + q = q.exclude(pk=int(cleaned_data['pk'])) + if q.count(): + raise forms.ValidationError(_(u"This ID already exists for " + u"this warehouse.")) return cleaned_data @@ -431,7 +440,8 @@ class LocalisationForm(CustomForm, forms.Form): initial = u"-" if self.container: q = models.ContainerLocalisation.objects.filter( - division=divlink, container=self.container) + division__division=divlink.division, + container=self.container) if q.count(): initial = q.all()[0].reference self.fields['division_{}'.format(divlink.pk)] = forms.CharField( |