summaryrefslogtreecommitdiff
path: root/archaeological_operations/forms.py
diff options
context:
space:
mode:
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")