diff options
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 |
commit | 25bfdf136bd2bd7658878d25e337b02b8150915d (patch) | |
tree | 9c277f0f6fdd5ae6a42b0f7201c96c75ca0f7bdf /archaeological_operations/forms.py | |
parent | 59847d040c37d77ebd9c731f9a4587665e906feb (diff) | |
download | Ishtar-25bfdf136bd2bd7658878d25e337b02b8150915d.tar.bz2 Ishtar-25bfdf136bd2bd7658878d25e337b02b8150915d.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.py | 28 |
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") |