Skip to content

Commit

Permalink
♻️(person) use new fill_* pattern on person factory and improve i18n
Browse files Browse the repository at this point in the history
We were using a "with_content" post_generation hook to create content
on pages but all the other factories have moved to one hook per
placeholder named with a "fill_*" pattern. The content was also not
created in all languages so this is also fixed in this commit.
  • Loading branch information
sampaccoud committed Aug 5, 2018
1 parent 1ea7451 commit d035375
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 57 deletions.
3 changes: 2 additions & 1 deletion src/richie/apps/core/management/commands/create_demo_site.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,8 @@ def create_demo_site():
NB_PERSONS,
languages=[l[0] for l in settings.LANGUAGES],
parent=pages_created["persons"],
with_content=True,
fill_portrait=True,
fill_resume=True,
should_publish=True,
)

Expand Down
56 changes: 23 additions & 33 deletions src/richie/apps/persons/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@
Persons factories
"""
import os
import random

from django.conf import settings
from django.core.files import File

import factory
from cms.api import add_plugin
from filer.models.imagemodels import Image

from ..core.factories import PageExtensionDjangoModelFactory
from ..core.helpers import create_text_plugin
from ..core.tests.utils import file_getter
from .models import Person, PersonTitle

Expand Down Expand Up @@ -58,46 +57,37 @@ def title(self):

@factory.post_generation
# pylint: disable=unused-argument
def with_content(self, create, extracted, **kwargs):
def fill_portrait(self, create, extracted, **kwargs):
"""
Add content plugins displayed in the "portrait" and "resume" placeholder
of the person page:
- Picture plugin featuring a random portrait image,
- Text plugin featuring the person resume with a random long text.
Add a portrait with a random image
"""
if create and extracted:
language = settings.LANGUAGE_CODE
portrait_placeholder = self.extended_object.placeholders.get(
slot="portrait"
)

resume_placeholder = self.extended_object.placeholders.get(slot="resume")

# Add a portrait with a random image
portrait_file = file_getter(os.path.dirname(__file__), "portrait")()
wrapped_portrait = File(portrait_file, portrait_file.name)
portrait = Image.objects.create(file=wrapped_portrait)
add_plugin(
language=language,
placeholder=portrait_placeholder,
plugin_type="PicturePlugin",
picture=portrait,
attributes={"alt": "portrait image"},
)

# Add a text plugin for resume with a long random text
nb_paragraphs = random.randint(2, 4)
paragraphs = [
factory.Faker("text", max_nb_chars=random.randint(200, 1000)).generate(
{}
for language in self.extended_object.get_languages():
add_plugin(
language=language,
placeholder=portrait_placeholder,
plugin_type="PicturePlugin",
picture=portrait,
attributes={"alt": "portrait image"},
)
for i in range(nb_paragraphs)
]
body = ["<p>{:s}</p>".format(p) for p in paragraphs]
add_plugin(
language=language,
placeholder=resume_placeholder,
plugin_type="CKEditorPlugin",
body="".join(body),

@factory.post_generation
# pylint: disable=unused-argument
def fill_resume(self, create, extracted, **kwargs):
"""
Add a text plugin for resume with a long random text
"""
if create and extracted:
create_text_plugin(
self.extended_object,
"resume",
nb_paragraphs=1,
languages=self.extended_object.get_languages(),
)
61 changes: 38 additions & 23 deletions tests/apps/persons/test_factories_person.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,43 @@ class PersonFactoryTestCase(TestCase):
Unit test suite to validate the behavior of the Person factory
"""

def test_person_factory(self):
def test_person_factory_portrait(self):
"""
PersonFactoryTestCase should be able to generate plugins with realistic fake
data: portrait and resume.
PersonFactory should be able to generate plugins with a realistic portrait for several
languages.
"""
person = PersonFactory(with_content=True)

# The portrait plugin should point to one of our fixtures images
portrait_placeholder = person.extended_object.placeholders.get(slot="portrait")
portrait_plugin = portrait_placeholder.cmsplugin_set.get(
plugin_type="PicturePlugin"
)
self.assertIn(
"portrait",
os.path.basename(
portrait_plugin.djangocms_picture_picture.picture.file.name
),
)

# The resume plugin should contain paragraphs
resume_placeholder = person.extended_object.placeholders.get(slot="resume")
resume_plugin = resume_placeholder.cmsplugin_set.get(
plugin_type="CKEditorPlugin"
)
self.assertIn("<p>", resume_plugin.simple_text_ckeditor_simpletext.body)
person = PersonFactory(fill_portrait=True, languages=["fr", "en"])

# Check that the portrait plugins were created as expected
portrait = person.extended_object.placeholders.get(slot="portrait")
self.assertEqual(portrait.cmsplugin_set.count(), 2)

# The portrait plugins should point to one of our fixtures images
for language in ["fr", "en"]:
portrait_plugin = portrait.cmsplugin_set.get(
plugin_type="PicturePlugin", language=language
)
self.assertIn(
"portrait",
os.path.basename(
portrait_plugin.djangocms_picture_picture.picture.file.name
),
)

def test_person_factory_resume(self):
"""
PersonFactory should be able to generate plugins with a realistic resume for
several languages.
"""
person = PersonFactory(fill_resume=True, languages=["fr", "en"])

# Check that the resume plugins were created as expected
resume = person.extended_object.placeholders.get(slot="resume")
self.assertEqual(resume.cmsplugin_set.count(), 2)

# The resume plugins should contain paragraphs
for language in ["fr", "en"]:
resume_plugin = resume.cmsplugin_set.get(
plugin_type="CKEditorPlugin", language=language
)
self.assertIn("<p>", resume_plugin.simple_text_ckeditor_simpletext.body)

0 comments on commit d035375

Please sign in to comment.