Coverage for apis_core/apis_entities/forms.py: 36%
25 statements
« prev ^ index » next coverage.py v7.6.1, created at 2024-09-16 07:42 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2024-09-16 07:42 +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
8from apis_core.generic.forms.fields import ModelImportChoiceField
11class EntitiesMergeForm(forms.Form):
12 def __init__(self, *args, **kwargs):
13 if "instance" in kwargs:
14 instance = kwargs.pop("instance")
15 super().__init__(*args, **kwargs)
16 ct = ContentType.objects.get_for_model(instance)
17 self.fields["uri"] = ModelImportChoiceField(
18 queryset=ct.model_class().objects.all()
19 )
20 uri = reverse("apis_core:generic:autocomplete", args=[ct])
21 attrs = {
22 "data-placeholder": "Search ...",
23 "data-minimum-input-length": 3,
24 "data-html": True,
25 }
26 self.fields["uri"].widget = autocomplete.ModelSelect2(uri, attrs=attrs)
27 self.fields["uri"].widget.choices = self.fields["uri"].choices
28 entitytype = instance._meta.verbose_name
29 help_text = f"""The attributes of the source {entitytype} you
30 choose will be copied/moved to this one and the source
31 {entitytype} will then be deleted."""
32 self.fields["uri"].help_text = help_text
33 self.fields["uri"].label = "Merge with..."
34 self.helper = FormHelper()
35 self.helper.add_input(Submit("submit", "Submit"))
36 self.helper.form_action = reverse(
37 "apis_core:apis_entities:generic_entities_merge_view",
38 args=[instance.__class__.__name__.lower(), instance.pk],
39 )