Skip to content

Commit

Permalink
Importe l'information 'ready_to_publish' depuis les archives (#6203)
Browse files Browse the repository at this point in the history
* Prend en compte le champ "ready_to_publish" à l'import

* Ajoute un test pour l'import de 'ready_to_publish'

* Corrige une coquille dans un test des tutos
  • Loading branch information
Arnaud-D authored Dec 27, 2021
1 parent 5ea1809 commit 44a96f4
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
55 changes: 54 additions & 1 deletion zds/tutorialv2/tests/tests_views/tests_content.py
Original file line number Diff line number Diff line change
Expand Up @@ -1634,7 +1634,7 @@ def test_import_in_existing_content(self):

# ensure the content
self.assertEqual(versioned.get_introduction(), some_text)
self.assertEqual(versioned.get_introduction(), some_text)
self.assertEqual(versioned.get_conclusion(), some_text)
self.assertEqual(len(versioned.children), 1)

new_chapter = versioned.children[-1]
Expand Down Expand Up @@ -1774,6 +1774,59 @@ def test_import_image_with_archive(self):
os.remove(draft_zip_path)
os.remove(image_zip_path)

def test_import_ready_to_publish(self):
"""Test whether the 'ready_to_publish' info from the archive is correctly imported."""

# General principle of this test:
# * create an archive by creating a content and exporting it
# * change the 'ready_to_publish' toggles on the content
# * import the archive and check whether we get back to the initial state (i.e. correct import)

self.client.force_login(self.user_author)

# Create a content with parts
content = PublishableContentFactory(author_list=[self.user_author])
versioned = content.load_version()
part1 = ContainerFactory(db_object=content, parent=versioned)
chapter1 = ContainerFactory(db_object=content, parent=part1)
chapter1.ready_to_publish = False
ContainerFactory(db_object=content, parent=part1) # chapter 2
part2 = ContainerFactory(db_object=content, parent=versioned)
part2.ready_to_publish = False
sha = versioned.repo_update(content.title, content.slug, "introduction", "conclusion")
content.sha_draft = sha
content.save()

# Download archive of initial state for content
result = self.client.get(reverse("content:download-zip", args=[content.pk, content.slug]), follow=False)
self.assertEqual(result.status_code, 200)
draft_zip_path = os.path.join(tempfile.gettempdir(), "__draft1.zip")
with open(draft_zip_path, "wb") as f:
f.write(result.content)

# Update readiness of part 2 and part1/chapter1
# Failure to import this information defaults also to True, this is to make sure.
versioned.children[0].children[0].ready_to_publish = True
versioned.children[1].ready_to_publish = True
sha = versioned.repo_update_top_container(content.title, content.slug, "introduction", "conclusion")
content.sha_draft = sha
content.save()

# Import archive
result = self.client.post(
reverse("content:import", args=[content.pk, content.slug]),
{"archive": open(draft_zip_path, "rb")},
)
self.assertEqual(result.status_code, 302)

# Check override of previous modifications through the import
content = PublishableContent.objects.get(pk=content.pk) # reload from database
versioned = content.load_version()
self.assertTrue(versioned.children[0].ready_to_publish)
self.assertTrue(versioned.children[0].children[1].ready_to_publish)
self.assertFalse(versioned.children[0].children[0].ready_to_publish)
self.assertFalse(versioned.children[1].ready_to_publish)

def test_display_history(self):
"""Test DisplayHistory view"""

Expand Down
3 changes: 3 additions & 0 deletions zds/tutorialv2/views/archives.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,8 @@ def update_from_new_version_in_zip(copy_to, copy_from, zip_file):
raise BadArchiveError(_(f"Le fichier « {child.conclusion} » n'est pas encodé en UTF-8"))

copy_to.repo_add_container(child.title, introduction, conclusion, do_commit=False, slug=child.slug)
copy_to.children[-1].ready_to_publish = child.ready_to_publish
copy_to.repo_update(copy_to.title, introduction, conclusion, do_commit=False)
UpdateContentWithArchive.update_from_new_version_in_zip(copy_to.children[-1], child, zip_file)

elif isinstance(child, Extract):
Expand Down Expand Up @@ -381,6 +383,7 @@ def form_valid(self, form):
if new_version.conclusion:
conclusion = str(zfile.read(new_version.conclusion), "utf-8")

versioned.ready_to_publish = new_version.ready_to_publish
versioned.repo_update_top_container(
new_version.title, new_version.slug, introduction, conclusion, do_commit=False
)
Expand Down

0 comments on commit 44a96f4

Please sign in to comment.