Skip to content

Commit

Permalink
✅(demo) improve demo site page tree
Browse files Browse the repository at this point in the history
Page tree is just a flat list of pages, all enabled in navigation.
But we don't want every pages to be in menus and need a more advanced
architecture to be able to create an additional menu for annexe page
like About, Help, Legal mentions, etc..

This commit hides some pages from menus and add a new page "Annexe"
where "About" page is moved in.
  • Loading branch information
sveetch committed Sep 4, 2018
1 parent 2556feb commit 5461ee2
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 177 deletions.
2 changes: 2 additions & 0 deletions src/richie/apps/core/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class PageExtensionDjangoModelFactory(factory.django.DjangoModelFactory):
parent = None
template = None
title = None
in_navigation = False

@factory.lazy_attribute
def extended_object(self):
Expand All @@ -62,6 +63,7 @@ def extended_object(self):
title=self.title,
languages=self.languages,
template=self.template,
in_navigation=self.in_navigation,
parent=self.parent,
)

Expand Down
44 changes: 43 additions & 1 deletion src/richie/apps/core/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def create_text_plugin(
a placeholder filled with some random text using Faker.
Arguments:
page (Page model instance): Instance of a Page used to search for
page (cms.models.pagemodel.Page): Instance of a Page used to search for
given slot (aka a placeholder name).
slot (string): A placeholder name available from page template.
Expand Down Expand Up @@ -148,3 +148,45 @@ def create_text_plugin(
plugin_type=plugin_type,
body="".join(body),
)


def recursive_page_creation(site, pages, parent=None):
"""
Recursively create page following tree structure with parent/children.
Arguments:
site (django.contrib.sites.models.Site): Site object which page will
be linked to.
pages (dict): Page items to create recursively such as 'children' key
value can be a dict to create child pages. The current page is
given to children for parent relation.
Keyword Arguments:
parent (cms.models.pagemodel.Page): Page used as a parent to create
page item from `pages` argument.
Returns:
dict: Created page items.
"""
pages_created = {}

for name, info in pages.items():
page = create_i18n_page(
info["content"],
is_homepage=(name == "home"),
in_navigation=info["in_navigation"],
published=True,
site=site,
parent=parent,
**info["kwargs"]
)

pages_created[name] = page

# Create children
if info.get("children", None):
pages_created[name].created_children = recursive_page_creation(
site, info["children"], parent=page
)

return pages_created
43 changes: 28 additions & 15 deletions src/richie/apps/core/management/commands/create_demo_site.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@
from richie.apps.persons.factories import PersonFactory
from richie.apps.persons.models import Person

from ...helpers import create_i18n_page
from ...helpers import recursive_page_creation

logger = logging.getLogger("richie.commands.core.create_demo_site")

DEMO_ANNEX_PAGE_ID = "annex"
NB_COURSES = 3
NB_COURSES_ORGANIZATION_RELATIONS = 3
NB_COURSES_SUBJECT_RELATIONS = 4
Expand All @@ -37,48 +38,66 @@
PAGE_INFOS = {
"home": {
"content": {"en": "Home", "fr": "Accueil"},
"in_navigation": False,
"kwargs": {"template": "richie/fullwidth.html"},
},
"news": {
"content": {"en": "News", "fr": "Actualités"},
"in_navigation": True,
"kwargs": {"template": "richie/fullwidth.html"},
},
"courses": {
"content": {"en": "Courses", "fr": "Cours"},
"in_navigation": True,
"kwargs": {
"reverse_id": Course.ROOT_REVERSE_ID,
"template": "search/search.html",
},
},
"subjects": {
"content": {"en": "Subjects", "fr": "Sujets"},
"in_navigation": False,
"kwargs": {
"reverse_id": Subject.ROOT_REVERSE_ID,
"template": "richie/child_pages_list.html",
},
},
"organizations": {
"content": {"en": "Organizations", "fr": "Etablissements"},
"in_navigation": True,
"kwargs": {
"reverse_id": Organization.ROOT_REVERSE_ID,
"template": "richie/child_pages_list.html",
},
},
"persons": {
"content": {"en": "Persons", "fr": "Personnes"},
"in_navigation": False,
"kwargs": {
"reverse_id": Person.ROOT_REVERSE_ID,
"template": "richie/child_pages_list.html",
},
},
"dashboard": {
"content": {"en": "Dashboard", "fr": "Tableau de bord"},
"in_navigation": False,
"cms": False,
"kwargs": {"template": "richie/fullwidth.html"},
},
"about": {
"content": {"en": "About", "fr": "A propos"},
"kwargs": {"template": "richie/fullwidth.html"},
"annex": {
"content": {"en": "Annex", "fr": "Annexe"},
"in_navigation": False,
"kwargs": {
"template": "richie/fullwidth.html",
"reverse_id": DEMO_ANNEX_PAGE_ID,
},
"children": {
"about": {
"content": {"en": "About", "fr": "A propos"},
"in_navigation": True,
"kwargs": {"template": "richie/fullwidth.html"},
}
},
},
}

Expand Down Expand Up @@ -121,17 +140,7 @@ def create_demo_site():
site = Site.objects.get(id=1)

# Create pages as described in PAGES_INFOS
pages_created = {}
for name, info in PAGE_INFOS.items():
page = create_i18n_page(
info["content"],
is_homepage=(name == "home"),
in_navigation=True,
published=True,
site=site,
**info["kwargs"]
)
pages_created[name] = page
pages_created = recursive_page_creation(site, PAGE_INFOS)

# Create some licences
licences = LicenceFactory.create_batch(NB_LICENCES)
Expand All @@ -145,6 +154,7 @@ def create_demo_site():
fill_description=True,
fill_logo=True,
should_publish=True,
in_navigation=True,
)

# Create subjects under the `Subjects` page
Expand All @@ -156,6 +166,7 @@ def create_demo_site():
fill_description=True,
fill_logo=True,
should_publish=True,
in_navigation=True,
)

# Django parler require a language to be manually set when working out of
Expand All @@ -170,6 +181,7 @@ def create_demo_site():
fill_portrait=True,
fill_resume=True,
should_publish=True,
in_navigation=True,
)

# Create courses under the `Course` page with subjects and organizations
Expand Down Expand Up @@ -199,6 +211,7 @@ def create_demo_site():
with_organizations=course_organizations,
with_subjects=random.sample(subjects, NB_COURSES_SUBJECT_RELATIONS),
should_publish=True,
in_navigation=True,
)
# Add a random number of course runs to the course
nb_course_runs = get_number_of_course_runs()
Expand Down
6 changes: 3 additions & 3 deletions src/richie/apps/courses/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ class OrganizationFactory(BLDPageExtensionDjangoModelFactory):

class Meta:
model = Organization
exclude = ["languages", "parent", "template", "title"]
exclude = ["languages", "parent", "template", "in_navigation", "title"]

template = Organization.TEMPLATE_DETAIL

Expand Down Expand Up @@ -131,7 +131,7 @@ class CourseFactory(PageExtensionDjangoModelFactory):

class Meta:
model = Course
exclude = ["languages", "parent", "template", "title"]
exclude = ["languages", "parent", "template", "in_navigation", "title"]

template = Course.TEMPLATE_DETAIL

Expand Down Expand Up @@ -324,7 +324,7 @@ class SubjectFactory(BLDPageExtensionDjangoModelFactory):

class Meta:
model = Subject
exclude = ["languages", "template", "title", "parent"]
exclude = ["languages", "template", "in_navigation", "title", "parent"]

template = Subject.TEMPLATE_DETAIL

Expand Down
2 changes: 1 addition & 1 deletion src/richie/apps/persons/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class PersonFactory(PageExtensionDjangoModelFactory):

class Meta:
model = Person
exclude = ["languages", "parent", "template", "title"]
exclude = ["languages", "parent", "template", "in_navigation", "title"]

template = Person.TEMPLATE_DETAIL

Expand Down

0 comments on commit 5461ee2

Please sign in to comment.