Skip to content

Commit

Permalink
feat(generic): Introduce SelectMergeOrEnrich view and route
Browse files Browse the repository at this point in the history
This view provides a simple form to select an internal or external
entity to merge its values in the entity that is modified.
  • Loading branch information
b1rger committed Nov 28, 2024
1 parent 7b0fa1c commit 0bef171
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 0 deletions.
4 changes: 4 additions & 0 deletions apis_core/generic/abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ def get_merge_url(self, other_id):
ct = ContentType.objects.get_for_model(self)
return reverse("apis_core:generic:merge", args=[ct, self.id, other_id])

def get_select_merge_or_enrich_url(self):
ct = ContentType.objects.get_for_model(self)
return reverse("apis_core:generic:selectmergeorenrich", args=[ct, self.id])

def get_create_success_url(self):
return self.get_absolute_url()

Expand Down
20 changes: 20 additions & 0 deletions apis_core/generic/forms/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,26 @@ def __init__(self, *args, **kwargs):
self.fields[field].widget.choices = self.fields[field].choices


class GenericSelectMergeOrEnrichForm(forms.Form):
def __init__(self, *args, **kwargs):
if "instance" in kwargs:
instance = kwargs.pop("instance")
super().__init__(*args, **kwargs)
ct = ContentType.objects.get_for_model(instance)
self.fields["uri"] = forms.ModelChoiceField(
queryset=ct.model_class().objects.all()
)
uri = reverse("apis_core:generic:autocomplete", args=[ct])
attrs = {"data-html": True, "data-tags": 1}
self.fields["uri"].widget = autocomplete.ModelSelect2(uri, attrs=attrs)
self.fields["uri"].widget.choices = self.fields["uri"].choices
self.fields["uri"].label = "Select or paste URI"
self.helper = FormHelper()
self.helper.form_method = "GET"
self.helper.add_input(Submit("submit", "Submit"))
self.helper.form_action = instance.get_enrich_url()


class GenericMergeWithForm(forms.Form):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{% extends basetemplate|default:"base.html" %}
{% load crispy_forms_tags %}

{% block content %}
<div class="container">
<div class="card">
<div class="card-header">
Select to merge into <a href="{{ object.get_absolute_url }}">{{ object }}</a>
</div>
<div class="card-body">{% crispy form form.helper %}</div>
</div>
</div>
{% endblock content %}
5 changes: 5 additions & 0 deletions apis_core/generic/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ def to_url(self, value):
path("create", views.Create.as_view(), name="create"),
path("delete/<int:pk>", views.Delete.as_view(), name="delete"),
path("update/<int:pk>", views.Update.as_view(), name="update"),
path(
"selectmergeorenrich/<int:pk>",
views.SelectMergeOrEnrich.as_view(),
name="selectmergeorenrich",
),
path(
"merge/<int:pk>/<int:otherpk>",
views.MergeWith.as_view(),
Expand Down
26 changes: 26 additions & 0 deletions apis_core/generic/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
GenericImportForm,
GenericMergeWithForm,
GenericModelForm,
GenericSelectMergeOrEnrichForm,
)
from .helpers import (
first_member_match,
Expand Down Expand Up @@ -359,6 +360,31 @@ def get_success_url(self):
return self.object.get_absolute_url()


class SelectMergeOrEnrich(GenericModelMixin, PermissionRequiredMixin, FormView):
"""
This view provides a simple form that allows to select other entities (also from
external sources, if set up) and on form submit redirects to the Enrich view.
"""

template_name = "generic/generic_selectmergeorenrich.html"
permission_action_required = "create"
form_class = GenericSelectMergeOrEnrichForm
template_name_suffix = "_selectmergeorenrich.html"

def get_object(self, *args, **kwargs):
return get_object_or_404(self.model, pk=self.kwargs.get("pk"))

def get_context_data(self, *args, **kwargs):
context = super().get_context_data(*args, **kwargs)
context["object"] = self.get_object()
return context

def get_form_kwargs(self, *args, **kwargs):
kwargs = super().get_form_kwargs(*args, **kwargs)
kwargs["instance"] = self.get_object()
return kwargs


class MergeWith(GenericModelMixin, PermissionRequiredMixin, FormView):
"""
Generic merge view.
Expand Down

0 comments on commit 0bef171

Please sign in to comment.