summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ishtar_common/tests.py38
-rw-r--r--ishtar_common/views.py19
2 files changed, 47 insertions, 10 deletions
diff --git a/ishtar_common/tests.py b/ishtar_common/tests.py
index a8e5eac6c..418b8a24b 100644
--- a/ishtar_common/tests.py
+++ b/ishtar_common/tests.py
@@ -3463,6 +3463,8 @@ class DocumentTest(TestCase):
self.st1 = models.SourceType.objects.create(label="Report", code="REP")
self.st2 = models.SourceType.objects.create(label="Illustration", code="ILL")
+ self.username, self.password, self.user = create_superuser()
+
def test_custom_index(self):
profile, created = models.IshtarSiteProfile.objects.get_or_create(
slug="default", active=True
@@ -3576,6 +3578,42 @@ class DocumentTest(TestCase):
doc.operations.add(self.ope1)
self.assertEqual(doc.operations.count(), 0)
+ def test_create_with_parent(self):
+ doc = models.Document.objects.create(title="Parent document")
+ doc.operations.add(self.ope1)
+ c = Client()
+ url = reverse("create-document-source", args=[doc.pk])
+ nb_doc = models.Document.objects.count()
+
+ response = c.get(url)
+ self.assertEqual(response.status_code, 302)
+
+ c.login(username=self.username, password=self.password)
+ response = c.get(url)
+ self.assertEqual(response.status_code, 200)
+ content = response.content.decode()
+ self.assertIn(
+ 'option value="{}" selected'.format(self.ope1.pk),
+ content
+ )
+ self.assertIn(doc.title, content)
+
+ posted = {"authors": [], "title": "A child document"}
+ posted["operations"] = [str(self.ope1.pk)]
+ posted["source"] = [doc.pk]
+ response = c.post(url, posted)
+ new_child_document_pk = self.ope1.documents.order_by("-pk").all()[0].pk
+ self.assertEqual(nb_doc + 1, models.Document.objects.count())
+ self.assertRedirects(
+ response,
+ "/document/edit/?open_item={}".format(
+ new_child_document_pk
+ ),
+ )
+ response = c.get("/show-document/{}/".format(doc.pk))
+ self.assertIn(posted["title"], content)
+ self.assertIn(doc.title, content)
+
class JinjaFilterTest(TestCase):
def test_splitpart(self):
diff --git a/ishtar_common/views.py b/ishtar_common/views.py
index 7368f3fab..19fc9f848 100644
--- a/ishtar_common/views.py
+++ b/ishtar_common/views.py
@@ -2100,13 +2100,13 @@ class DocumentCreateView(DocumentFormMixin, CreateView):
def get_form_kwargs(self):
kwargs = super(DocumentCreateView, self).get_form_kwargs()
initial = kwargs.get("initial", {})
- document = ''
- value = ''
+ document_parent = None
+ value = None
if self.kwargs.get("pk"):
try:
- document = models.Document.objects.get(pk=self.kwargs.get("pk"))
- assert check_permission(self.request, "document/create", document.pk)
+ 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):
raise Http404()
@@ -2119,16 +2119,15 @@ class DocumentCreateView(DocumentFormMixin, CreateView):
continue
initial[related_key] = str(item.pk)
- if document:
- value = getattr(document, related_key)
+ if document_parent:
+ for k in models.Document.RELATED_MODELS:
+ value = getattr(document_parent, k)
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
-
- if document:
- initial["source"] = document.pk
+ initial[k] = value
+ initial["source"] = document_parent.pk
if initial:
kwargs["initial"] = initial