summaryrefslogtreecommitdiff
path: root/archaeological_operations/forms.py
diff options
context:
space:
mode:
authorÉtienne Loks <etienne.loks@iggdrasil.net>2017-04-01 19:06:35 +0200
committerÉtienne Loks <etienne.loks@iggdrasil.net>2017-04-01 19:06:35 +0200
commit71672e712570bf33b55cb534e2776ef8a1251755 (patch)
tree9c277f0f6fdd5ae6a42b0f7201c96c75ca0f7bdf /archaeological_operations/forms.py
parent08ad687cf6ca9f4aa9aff625370df24a6a94dea6 (diff)
downloadIshtar-71672e712570bf33b55cb534e2776ef8a1251755.tar.bz2
Ishtar-71672e712570bf33b55cb534e2776ef8a1251755.zip
Operation wizard: an operation cannot be related to herself (refs #3578)
Diffstat (limited to 'archaeological_operations/forms.py')
-rw-r--r--archaeological_operations/forms.py28
1 files changed, 27 insertions, 1 deletions
diff --git a/archaeological_operations/forms.py b/archaeological_operations/forms.py
index 6423962d8..4796ef68c 100644
--- a/archaeological_operations/forms.py
+++ b/archaeological_operations/forms.py
@@ -387,6 +387,9 @@ class RecordRelationsForm(ManageOldType, forms.Form):
validators=[valid_id(models.Operation)], required=False)
def __init__(self, *args, **kwargs):
+ self.left_record = None
+ if 'left_record' in kwargs:
+ self.left_record = kwargs.pop('left_record')
super(RecordRelationsForm, self).__init__(*args, **kwargs)
self.fields['relation_type'].choices = \
models.RelationType.get_types(
@@ -413,6 +416,9 @@ class RecordRelationsForm(ManageOldType, forms.Form):
cleaned_data.get('right_record', None)):
raise forms.ValidationError(
_(u"You should select a relation type."))
+ if str(cleaned_data.get('right_record')) == str(self.left_record.pk):
+ raise forms.ValidationError(
+ _(u"An operation cannot be related to herself."))
return cleaned_data
@classmethod
@@ -447,7 +453,27 @@ class RecordRelationsForm(ManageOldType, forms.Form):
result.append((_("Deleted relations"), u" ; ".join(deleted)))
return result
-RecordRelationsFormSet = formset_factory(RecordRelationsForm, can_delete=True)
+
+class RecordRelationsFormSetBase(FormSet):
+ # passing left_record should be nicely done with form_kwargs with Django 1.9
+ # with no need of all these complications
+
+ def __init__(self, *args, **kwargs):
+ self.left_record = None
+ if 'left_record' in kwargs:
+ self.left_record = kwargs.pop('left_record')
+ super(RecordRelationsFormSetBase, self).__init__(*args, **kwargs)
+
+ def _construct_forms(self):
+ # instantiate all the forms and put them in self.forms
+ self.forms = []
+ for i in xrange(self.total_form_count()):
+ self.forms.append(self._construct_form(
+ i, left_record=self.left_record))
+
+
+RecordRelationsFormSet = formset_factory(
+ RecordRelationsForm, can_delete=True, formset=RecordRelationsFormSetBase)
RecordRelationsFormSet.form_label = _(u"Relations")