diff options
Diffstat (limited to 'archaeological_context_records/forms.py')
-rw-r--r-- | archaeological_context_records/forms.py | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/archaeological_context_records/forms.py b/archaeological_context_records/forms.py index 6d48aba4f..594ed1346 100644 --- a/archaeological_context_records/forms.py +++ b/archaeological_context_records/forms.py @@ -53,6 +53,7 @@ from ishtar_common.forms import ( MultiSearchForm, LockForm, DocumentItemSelect, + QAForm, ) from ishtar_common.forms_common import get_town_field from archaeological_operations.forms import ( @@ -688,3 +689,57 @@ class QAContextRecordDuplicateForm(IshtarForm): except Parcel.DoesNotExist: pass return self.context_record.duplicate(self.user, data=data) + + +class QAContextRecordFormMulti(QAForm): + form_admin_name = _("Context record - Quick action - Modify") + form_slug = "contextrecord-quickaction-modify" + + base_models = ["qa_relation_type"] + associated_models = { + "qa_relation_type": models.RelationType, + } + + MULTI = True + qa_relation_type = forms.ChoiceField( + label=_("Relation type"), + required=False, + ) + qa_related_to = forms.IntegerField( + label=_("Related to"), + widget=widgets.JQueryAutoComplete( + reverse_lazy("autocomplete-contextrecord"), + associated_model=models.ContextRecord, + ), + validators=[valid_id(models.ContextRecord)], + required=False, + ) + + TYPES = [ + FieldType("qa_relation_type", models.RelationType), + ] + + def clean(self): + cleaned_data = super().clean() + r_type = cleaned_data.get("qa_relation_type") + r_related = cleaned_data.get("qa_related_to") + if (r_type and not r_related) or (not r_type and r_related): + raise forms.ValidationError( + _( + "To add relations, you must fill relation type and related to fields." + ) + ) + + def _set_qa_related_to(self, item, __): + r_type = self.cleaned_data.get("qa_relation_type", None) + r_related = self.cleaned_data.get("qa_related_to", None) + if not r_type or not r_related: + return + models.RecordRelations.objects.get_or_create( + left_record=item, + right_record_id=r_related, + relation_type_id=r_type + ) + + def _set_qa_relation_type(self, item, __): + pass |