From fda77743c85ccfeeec7c53bf16af4c432326605a Mon Sep 17 00:00:00 2001 From: Cefin Date: Mon, 29 Nov 2021 12:07:47 +0000 Subject: rapid action add document with parent source #5172 --- ishtar_common/models.py | 12 ++++++++++++ ishtar_common/urls.py | 7 +++++++ ishtar_common/views.py | 17 +++++++++++++++++ 3 files changed, 36 insertions(+) (limited to 'ishtar_common') diff --git a/ishtar_common/models.py b/ishtar_common/models.py index e87e6c5a2..197ac5dcc 100644 --- a/ishtar_common/models.py +++ b/ishtar_common/models.py @@ -4210,6 +4210,18 @@ class Document( True, ) ) + can_create_document = self.can_do(request, "add_document") + if can_create_document: + actions += [ + ( + reverse("create-document-source", args=[self.pk]), + _("Add document with source parent"), + "fa fa-plus", + _("Doc./src"), + "", + False, + ) + ] return actions @property diff --git a/ishtar_common/urls.py b/ishtar_common/urls.py index 7bbdc189f..de881e1ba 100644 --- a/ishtar_common/urls.py +++ b/ishtar_common/urls.py @@ -485,6 +485,13 @@ 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 1363684a7..485facd75 100644 --- a/ishtar_common/views.py +++ b/ishtar_common/views.py @@ -2100,6 +2100,13 @@ class DocumentCreateView(DocumentFormMixin, CreateView): def get_form_kwargs(self): kwargs = super(DocumentCreateView, self).get_form_kwargs() initial = kwargs.get("initial", {}) + + try: + document = models.Document.objects.get(pk=self.kwargs.get("pk")) + assert check_permission(self.request, "document/create", document.pk) + except (AssertionError, models.Document.DoesNotExist): + raise Http404() + for related_key in models.Document.RELATED_MODELS_ALT: model = models.Document._meta.get_field(related_key).related_model if model.SLUG in self.request.GET: @@ -2108,9 +2115,19 @@ class DocumentCreateView(DocumentFormMixin, CreateView): except model.DoesNotExist: continue initial[related_key] = str(item.pk) + + value = getattr(document, related_key) + if hasattr(value, "all"): + value = ",".join([str(v.pk) for v in value.all()]) + if hasattr(value, "pk"): + value = value.pk + initial[related_key] = value + initial["source"] = document.pk + if initial: kwargs["initial"] = initial kwargs["user"] = self.request.user + return kwargs -- cgit v1.2.3