diff options
| -rw-r--r-- | archaeological_operations/forms.py | 28 | ||||
| -rw-r--r-- | archaeological_operations/wizards.py | 11 | ||||
| -rw-r--r-- | ishtar_common/wizards.py | 2 | 
3 files changed, 38 insertions, 3 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") diff --git a/archaeological_operations/wizards.py b/archaeological_operations/wizards.py index 5410b37f8..c132c24be 100644 --- a/archaeological_operations/wizards.py +++ b/archaeological_operations/wizards.py @@ -1,6 +1,6 @@  #!/usr/bin/env python  # -*- coding: utf-8 -*- -# Copyright (C) 2012-2016  Étienne Loks  <etienne.loks_AT_peacefrogsDOTnet> +# Copyright (C) 2012-2017  Étienne Loks  <etienne.loks_AT_peacefrogsDOTnet>  # This program is free software: you can redistribute it and/or modify  # it under the terms of the GNU Affero General Public License as @@ -275,6 +275,15 @@ class OperationModificationWizard(OperationWizard):      modification = True      filter_owns = {'selec-operation_modification': ['pk']} +    def get_form_kwargs(self, step, **kwargs): +        kwargs = super(OperationModificationWizard, self).get_form_kwargs( +            step, **kwargs) +        print(step) +        if step != "relations-operation_modification": +            return kwargs +        kwargs["left_record"] = self.get_current_object() +        return kwargs +  class OperationClosingWizard(ClosingWizard):      model = models.Operation diff --git a/ishtar_common/wizards.py b/ishtar_common/wizards.py index ddb2bc2ac..61923d920 100644 --- a/ishtar_common/wizards.py +++ b/ishtar_common/wizards.py @@ -819,7 +819,7 @@ class Wizard(NamedUrlWizardView):                  # get a form key                  frm = form.form                  if callable(frm): -                    frm = frm() +                    frm = frm(self.get_form_kwargs(step))                  total_field = 0                  if hasattr(frm, 'count_valid_fields'): | 
