Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(Export PDF) Enlève les conteneurs non validés #6267

Merged
merged 3 commits into from
Mar 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion zds/tutorialv2/publication_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ def publish(self, md_file_path, base_name, **kwargs):
replaced_media_url = settings.MEDIA_URL
if replaced_media_url.startswith("/"):
replaced_media_url = replaced_media_url[1:]
exported = export_content(public_versionned_source, with_text=True)
exported = export_content(public_versionned_source, with_text=True, ready_to_publish_only=True)
# no title to avoid zmd to put it on the final latex
del exported["title"]
content, metadata, messages = render_markdown(
Expand Down
29 changes: 28 additions & 1 deletion zds/tutorialv2/tests/tests_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
from zds.tutorialv2.publication_utils import publish_content, unpublish_content
from zds.tutorialv2.models.database import PublishableContent, PublishedContent, ContentReaction, ContentRead
from django.core.management import call_command
from zds.tutorialv2.publication_utils import Publicator, PublicatorRegistry
from zds.tutorialv2.publication_utils import Publicator, PublicatorRegistry, ZMarkdownRebberLatexPublicator
from zds.tutorialv2.tests import TutorialTestMixin, override_for_contents
from zds import json_handler
from zds.utils.tests.factories import LicenceFactory
Expand Down Expand Up @@ -263,6 +263,14 @@ def test_export_only_ready_to_publish(self):
"""
Test exported contents contain only ready_to_publish==True parts.
"""

# We save the current settings for the PDF publicator:
previous_pdf_publicator = PublicatorRegistry.get("pdf")
previous_build_pdf_when_published = self.overridden_zds_app["content"]["build_pdf_when_published"]
# We need to produce at least the LaTeX file, so we use the real PDF publicator:
PublicatorRegistry.registry["pdf"] = ZMarkdownRebberLatexPublicator(".pdf")
self.overridden_zds_app["content"]["build_pdf_when_published"] = True

# Medium-size tutorial
midsize_tuto = PublishableContentFactory(type="TUTORIAL")

Expand Down Expand Up @@ -299,13 +307,32 @@ def test_export_only_ready_to_publish(self):
self.assertIsNone(child.introduction)
self.assertIsNone(child.conclusion)

# Test Markdown content:
self.assertTrue(published.has_md())
with Path(published.get_extra_contents_directory(), published.content_public_slug + ".md").open("r") as md:
content = md.read()
self.assertIn(chapter1.title, content)
self.assertIn(chapter2.title, content)
self.assertNotIn(chapter3.title, content)

# TODO: factorize getting the LaTeX file path with what is done in zds.tutorialv2.publication_utils.publish_content()
tmp_path = os.path.join(
settings.ZDS_APP["content"]["repo_public_path"], published.content_public_slug + "__building"
)
build_extra_contents_path = os.path.join(tmp_path, settings.ZDS_APP["content"]["extra_contents_dirname"])
base_name = os.path.join(build_extra_contents_path, published.content_public_slug)
tex_file = base_name + ".tex"
# PDF generation may fail, we only test the LaTeX content:
with open(tex_file) as tex:
content = tex.read()
self.assertIn(chapter1.title, content)
self.assertIn(chapter2.title, content)
self.assertNotIn(chapter3.title, content)

# We set back the previous settings:
PublicatorRegistry.registry["pdf"] = previous_pdf_publicator
self.overridden_zds_app["content"]["build_pdf_when_published"] = previous_build_pdf_when_published
philippemilink marked this conversation as resolved.
Show resolved Hide resolved

def test_tagged_tree_extract(self):
midsize = PublishableContentFactory(author_list=[self.user_author])
midsize_draft = midsize.load_version()
Expand Down
14 changes: 10 additions & 4 deletions zds/tutorialv2/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -696,11 +696,13 @@ def export_extract(extract, with_text):
return dct


def export_container(container, with_text=False):
def export_container(container, with_text=False, ready_to_publish_only=False):
"""Export a container to a dictionary

:param container: the container
:type container: zds.tutorialv2.models.models_versioned.Container
:param ready_to_publish_only: if True, returns only ready-to-publish containers
:type ready_to_publish_only: boolean
:return: dictionary containing the information
:rtype: dict
"""
Expand All @@ -727,22 +729,26 @@ def export_container(container, with_text=False):
dct["ready_to_publish"] = container.ready_to_publish
if container.has_sub_containers():
for child in container.children:
dct["children"].append(export_container(child, with_text))
if ready_to_publish_only and not child.ready_to_publish:
continue
dct["children"].append(export_container(child, with_text, ready_to_publish_only))
elif container.has_extracts():
for child in container.children:
dct["children"].append(export_extract(child, with_text))

return dct


def export_content(content, with_text=False):
def export_content(content, with_text=False, ready_to_publish_only=False):
"""Export a content to dictionary in order to store them in a JSON file

:param content: content to be exported
:param ready_to_publish_only: if True, returns only ready-to-publish containers
:type ready_to_publish_only: boolean
:return: dictionary containing the information
:rtype: dict
"""
dct = export_container(content, with_text)
dct = export_container(content, with_text, ready_to_publish_only)

# append metadata :
dct["version"] = 2.1
Expand Down