summaryrefslogtreecommitdiff
path: root/archaeological_warehouse/forms.py
diff options
context:
space:
mode:
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
commita700ba64f2badd50a006dfd1db37c834ae6b9031 (patch)
tree08bd01af358f529605479acf64b74b3c747157db /archaeological_warehouse/forms.py
parentda914f4cf22d656c1b24e5ceeadd48785c81e9ff (diff)
downloadIshtar-a700ba64f2badd50a006dfd1db37c834ae6b9031.tar.bz2
Ishtar-a700ba64f2badd50a006dfd1db37c834ae6b9031.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.py30
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(