Skip to content

Commit

Permalink
Mise à jour automatique des modèles de page (#228)
Browse files Browse the repository at this point in the history
* Auto update the page templates

* add

* Fix the template pages update script
  • Loading branch information
Ash-Crow authored Oct 24, 2024
1 parent f01a4e8 commit 5b6bf6e
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 15 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ init-dev:
update:
$(EXEC_CMD) poetry install --without dev
$(EXEC_CMD) poetry run python manage.py migrate
$(EXEC_CMD) poetry run python manage.py import_page_templates
make collectstatic
make index

Expand Down
2 changes: 1 addition & 1 deletion Procfile
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
postdeploy: python manage.py migrate && python manage.py update_index
postdeploy: python manage.py migrate && python manage.py import_page_templates && python manage.py update_index
web: gunicorn config.wsgi --log-file -
2 changes: 1 addition & 1 deletion content_manager/page_templates/pages_data.json
Original file line number Diff line number Diff line change
Expand Up @@ -2302,7 +2302,7 @@
"value": {
"title": "Carte horizontale simple",
"heading_tag": "h3",
"description": "<p data-block-key=\"ye5df\">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>",
"description": "<p data-block-key=\"ye5df\">Version des mod\u00e8les de page : 1.10.0RC3</p>",
"image": null,
"image_ratio": "",
"image_badge": [],
Expand Down
5 changes: 3 additions & 2 deletions content_manager/services/accessors.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def get_or_create_content_page(
# Don't replace or duplicate an already existing page
already_exists = ContentPage.objects.filter(slug=slug).first()
if already_exists:
sys.stdout.write(f"The {slug} page seem to already exist with id {already_exists.id}")
sys.stdout.write(f"The {slug} page seem to already exist with id {already_exists.id}\n")
return already_exists

new_page = parent_page.add_child(
Expand All @@ -58,9 +58,10 @@ def get_or_create_content_page(
)
)

allowed_page_fields = HEADER_FIELDS + ["source_url"]
if page_fields and len(page_fields):
for k, v in page_fields.items():
if k in HEADER_FIELDS:
if k in allowed_page_fields:
setattr(new_page, k, v)
new_page.save()

Expand Down
25 changes: 14 additions & 11 deletions content_manager/services/import_export.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import copy
import json
import os
import sys
from io import BytesIO
from pathlib import PosixPath
from urllib.request import urlretrieve
Expand All @@ -11,6 +12,7 @@
from django.core.files.images import ImageFile
from django.utils import timezone
from wagtail.images.models import Image
from wagtail.models import Page
from wagtail.utils.file import hash_filelike

from content_manager.constants import HEADER_FIELDS
Expand Down Expand Up @@ -122,9 +124,11 @@ def __init__(
self.parent_page = None

def get_or_create_page_templates_index(self) -> ContentPage:
# The templates index is created right under the Root page, like a site
parent_page = Page.objects.first()
body = [("subpageslist", None)]
return get_or_create_content_page(
slug="page_templates_index", title="Modèles de pages à copier", body=body, restriction_type="login"
slug="page_templates_index", title="Modèles de pages à copier", body=body, parent_page=parent_page
)

def import_pages(self):
Expand Down Expand Up @@ -153,7 +157,6 @@ def import_page(self, page_id: str) -> ContentPage:
"slug": raw_page["meta"]["slug"],
"title": raw_page["title"],
"body": raw_page["body"],
"restriction_type": "login",
"parent_page": self.parent_page,
}

Expand All @@ -162,22 +165,22 @@ def import_page(self, page_id: str) -> ContentPage:
for field in HEADER_FIELDS:
if raw_page[field]:
page_fields[field] = raw_page[field]

page_dict["page_fields"] = page_fields
return get_or_create_content_page(**page_dict)

def update_page(self, page_id, page):
raw_page = self.pages[page_id]
page.slug = raw_page["meta"]["slug"]
page.title = raw_page["title"]
page.body = raw_page["body"]
def update_page(self, source_page_id, existing_page):
raw_page = self.pages[source_page_id]
existing_page.slug = raw_page["meta"]["slug"]
existing_page.title = raw_page["title"]
existing_page.body = raw_page["body"]

for field in HEADER_FIELDS:
if raw_page[field]:
setattr(page, field, raw_page[field])
setattr(existing_page, field, raw_page[field])

page.save()
return page
existing_page.save()
sys.stdout.write(f"Page {existing_page.slug} has been updated.\n")
return existing_page

def update_image_ids(self, page_id):
page = self.pages[page_id]
Expand Down

0 comments on commit 5b6bf6e

Please sign in to comment.