Coverage for apis_core/apis_entities/forms.py: 36%
22 statements
« prev ^ index » next coverage.py v7.6.4, created at 2024-11-22 07:51 +0000
« prev ^ index » next coverage.py v7.6.4, created at 2024-11-22 07:51 +0000
1from crispy_forms.helper import FormHelper
2from crispy_forms.layout import Submit
3from dal import autocomplete
4from django import forms
5from django.contrib.contenttypes.models import ContentType
6from django.urls import reverse
9class EntitiesMergeForm(forms.Form):
10 def __init__(self, *args, **kwargs):
11 if "instance" in kwargs:
12 instance = kwargs.pop("instance")
13 super().__init__(*args, **kwargs)
14 ct = ContentType.objects.get_for_model(instance)
15 self.fields["uri"] = forms.ModelChoiceField(
16 queryset=ct.model_class().objects.all()
17 )
18 uri = reverse("apis_core:generic:autocomplete", args=[ct])
19 attrs = {
20 "data-placeholder": "Search ...",
21 "data-minimum-input-length": 3,
22 "data-html": True,
23 "data-tags": 1,
24 }
25 self.fields["uri"].widget = autocomplete.ModelSelect2(uri, attrs=attrs)
26 self.fields["uri"].widget.choices = self.fields["uri"].choices
27 self.fields["uri"].label = "Merge with..."
28 self.helper = FormHelper()
29 self.helper.form_method = "GET"
30 self.helper.add_input(Submit("submit", "Submit"))
31 self.helper.form_action = instance.get_enrich_url()