From 121382c0e1c5202c744b7ea8dcfe900ad95b4fa0 Mon Sep 17 00:00:00 2001 From: Étienne Loks Date: Fri, 3 Dec 2021 11:27:56 +0100 Subject: Documents - add a sub-document from sheet: refactoring, use GET parameters instead of url parameters (refs #5172) --- ishtar_common/models.py | 6 +++--- ishtar_common/urls.py | 7 ------- ishtar_common/views.py | 18 +++++++++--------- 3 files changed, 12 insertions(+), 19 deletions(-) diff --git a/ishtar_common/models.py b/ishtar_common/models.py index 197ac5dcc..c8056a4ab 100644 --- a/ishtar_common/models.py +++ b/ishtar_common/models.py @@ -4214,10 +4214,10 @@ class Document( if can_create_document: actions += [ ( - reverse("create-document-source", args=[self.pk]), - _("Add document with source parent"), + reverse("create-document") + f"?source_pk={self.pk}", + _("Add sub-document"), "fa fa-plus", - _("Doc./src"), + _("sub-document"), "", False, ) diff --git a/ishtar_common/urls.py b/ishtar_common/urls.py index de881e1ba..7bbdc189f 100644 --- a/ishtar_common/urls.py +++ b/ishtar_common/urls.py @@ -485,13 +485,6 @@ urlpatterns += [ ), name="create-document", ), - url( - r"document/create/(?P[0-9-]+)?/$", - check_rights(["add_document", "add_own_document"])( - views.DocumentCreateView.as_view() - ), - name="create-document-source", - ), url( r"document/edit/$", check_rights(["change_document", "change_own_document"])( diff --git a/ishtar_common/views.py b/ishtar_common/views.py index 19fc9f848..6f5953841 100644 --- a/ishtar_common/views.py +++ b/ishtar_common/views.py @@ -2100,14 +2100,14 @@ class DocumentCreateView(DocumentFormMixin, CreateView): def get_form_kwargs(self): kwargs = super(DocumentCreateView, self).get_form_kwargs() initial = kwargs.get("initial", {}) - document_parent = None - value = None + source = None - if self.kwargs.get("pk"): + if self.request.GET.get("source_pk", None): try: - document_parent = models.Document.objects.get(pk=self.kwargs.get("pk")) - assert check_permission(self.request, "document/create", document_parent.pk) - except (AssertionError, models.Document.DoesNotExist): + source = models.Document.objects.get( + pk=self.request.GET.get("source_pk") + ) + except models.Document.DoesNotExist: raise Http404() for related_key in models.Document.RELATED_MODELS_ALT: @@ -2119,15 +2119,15 @@ class DocumentCreateView(DocumentFormMixin, CreateView): continue initial[related_key] = str(item.pk) - if document_parent: + if source: for k in models.Document.RELATED_MODELS: - value = getattr(document_parent, k) + value = getattr(source, k) if hasattr(value, "all"): value = ",".join([str(v.pk) for v in value.all()]) if hasattr(value, "pk"): value = value.pk initial[k] = value - initial["source"] = document_parent.pk + initial["source"] = source.pk if initial: kwargs["initial"] = initial -- cgit v1.2.3