Skip to content

Commit b3eae99

Browse files
committed
feat: Add command to preload content
1 parent 4a6758a commit b3eae99

File tree

8 files changed

+112
-0
lines changed

8 files changed

+112
-0
lines changed

.dockerignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
.git
22
node_modules
3+
4+
demo-content/

README.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,15 @@ to see sections that can be removed - in each case, the section is noted with a
6868

6969
Options are also available for using Postgres/MySQL, uWSGI/Gunicorn/Guvicorn, etc.
7070

71+
Loading with pre-built page on install
72+
======================================
73+
74+
It is now possible to load the project with contents which shows how to use
75+
``django-cms`` and ``djangocms-frontend`` to build web pages.
76+
77+
Here is how to add demo content:
78+
1. Run ``docker compose run --rm web python manage.py democontent demo-content/help.json
79+
7180
Updating requirements
7281
=====================
7382

backend/management/__init__.py

Whitespace-only changes.

backend/management/commands/__init__.py

Whitespace-only changes.
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
import os
2+
import json
3+
4+
from django.db import transaction
5+
from django.contrib.auth import get_user_model
6+
from django.core.management.base import BaseCommand
7+
8+
from cms.api import create_page
9+
from cms.models.contentmodels import PageContent
10+
from cms.constants import TEMPLATE_INHERITANCE_MAGIC
11+
12+
from djangocms_transfer.importer import import_plugins_to_page
13+
from djangocms_transfer.datastructures import ArchivedPlaceholder, ArchivedPlugin
14+
15+
16+
LANGUAGE="en"
17+
User = get_user_model()
18+
19+
20+
class Command(BaseCommand):
21+
help = "Create Demo content"
22+
23+
def add_arguments(self, parser):
24+
parser.add_argument(
25+
"file", metavar='FILE', help="Input file to load from."
26+
)
27+
28+
def _page_create_details(self):
29+
user = User.objects.first()
30+
if not user:
31+
#TODO: delete possible after whole operation?
32+
username = "demo"
33+
password = "demo"
34+
user = User.objects.create_user(username=username, password=password)
35+
self.stdout.write(
36+
"User with username, '%s', and password, '%s', created" %
37+
username, password
38+
)
39+
40+
return {
41+
"title": f"{self.title.title()}",
42+
"template": TEMPLATE_INHERITANCE_MAGIC,
43+
"created_by": user,
44+
"in_navigation": True,
45+
"language": LANGUAGE,
46+
}
47+
48+
def loaddata(self, file):
49+
def parse_data(data):
50+
if not data:
51+
return data
52+
if "plugins" in data:
53+
return ArchivedPlaceholder(
54+
slot=data["placeholder"],
55+
plugins=data["plugins"],
56+
)
57+
if "plugin_type" in data:
58+
return ArchivedPlugin(**data)
59+
return data
60+
61+
fixture = open(file, "rb")
62+
try:
63+
plugins: list = json.loads(fixture.read(), object_hook=parse_data)
64+
65+
# create page and get pagecontent
66+
page = create_page(**self._page_create_details())
67+
page_content = page.get_admin_content(language=LANGUAGE)
68+
69+
# import plugin to page
70+
import_plugins_to_page(
71+
placeholders=plugins,
72+
pagecontent=page_content,
73+
language=LANGUAGE,
74+
)
75+
except Exception as e:
76+
e.args = (f"Problem installing fixture {self.fixture}: {e}",)
77+
raise
78+
finally:
79+
fixture.close()
80+
81+
def handle(self, file, *args, **options):
82+
# get title from file
83+
self.title = os.path.basename(file).split(".")[0]
84+
85+
# check if the title does not exist
86+
all_page_content = PageContent.admin_manager.all()
87+
for page_content in all_page_content:
88+
if page_content.title.lower() == self.title.lower():
89+
self.stdout.write(
90+
"Page with title, '%s', already exists" % self.title
91+
)
92+
return
93+
94+
with transaction.atomic():
95+
self.loaddata(file)
96+
97+
self.stdout.write("Page with title, '%s', created" % self.title)

backend/settings.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@
5050
'filer',
5151
'easy_thumbnails',
5252

53+
'djangocms_transfer',
54+
5355
# the default publishing implementation - optional, but used in most projects
5456
'djangocms_versioning',
5557

demo-content/gridrowpercolumn.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[{"placeholder": "Content", "plugins": [{"pk": 62, "creation_date": "2025-07-15T13:31:14.722Z", "position": 1, "plugin_type": "GridRowPlugin", "parent_id": null, "data": {"ui_item": "GridRow", "tag_type": "div", "config": {"vertical_alignment": "", "horizontal_alignment": "", "gutters": "", "attributes": {}, "row_cols_xs": 3, "row_cols_sm": 7, "row_cols_md": null, "row_cols_lg": null, "row_cols_xl": null, "row_cols_xxl": null, "plugin_title": {"show": false, "title": ""}, "responsive_visibility": null, "margin_x": "", "margin_y": "", "margin_devices": null, "padding_x": "", "padding_y": "", "padding_devices": null}}}, {"pk": 65, "creation_date": "2025-07-17T21:19:38.749Z", "position": 2, "plugin_type": "GridContainerPlugin", "parent_id": null, "data": {"ui_item": "GridContainer", "tag_type": "div", "config": {"size_x": "", "size_y": "", "padding_x": "", "padding_y": "", "padding_devices": null, "margin_x": "", "margin_y": "", "margin_devices": ["xs"], "responsive_visibility": [], "background_context": "", "background_opacity": "", "background_shadow": "", "plugin_title": {"show": false, "title": ""}, "container_type": "container", "attributes": {}}}}]}]

demo-content/help.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[{"placeholder": "Content", "plugins": [{"pk": 70, "creation_date": "2025-03-13T17:13:26.875Z", "position": 1, "plugin_type": "GridContainerPlugin", "parent_id": null, "data": {"ui_item": "GridContainer", "tag_type": "div", "config": {"container_type": "container", "attributes": {}, "plugin_title": {"show": false, "title": ""}, "background_context": "", "background_opacity": "100", "background_shadow": "", "responsive_visibility": null, "margin_x": "", "margin_y": "", "margin_devices": null, "padding_x": "", "padding_y": "", "padding_devices": null, "size_x": "vw-100", "size_y": "50"}}}, {"pk": 71, "creation_date": "2025-03-13T17:15:04.988Z", "position": 2, "plugin_type": "GridRowPlugin", "parent_id": 70, "data": {"ui_item": "GridRow", "tag_type": "div", "config": {"vertical_alignment": "", "horizontal_alignment": "", "gutters": "", "attributes": {}, "row_cols_xs": null, "row_cols_sm": null, "row_cols_md": null, "row_cols_lg": null, "row_cols_xl": null, "row_cols_xxl": null, "plugin_title": {"show": false, "title": ""}, "responsive_visibility": null, "margin_x": "mx-1", "margin_y": "my-1", "margin_devices": null, "padding_x": "px-1", "padding_y": "py-1", "padding_devices": null}}}, {"pk": 72, "creation_date": "2025-03-13T17:16:12.137Z", "position": 3, "plugin_type": "GridColumnPlugin", "parent_id": 71, "data": {"ui_item": "GridColumn", "tag_type": "div", "config": {"column_alignment": null, "xs_col": null, "xs_order": null, "xs_offset": null, "xs_ml": null, "xs_mr": null, "sm_col": null, "sm_order": null, "sm_offset": null, "sm_ml": null, "sm_mr": null, "md_col": null, "md_order": null, "md_offset": null, "md_ml": null, "md_mr": null, "lg_col": null, "lg_order": null, "lg_offset": null, "lg_ml": null, "lg_mr": null, "xl_col": null, "xl_order": null, "xl_offset": null, "xl_ml": null, "xl_mr": null, "xxl_col": null, "xxl_order": null, "xxl_offset": null, "xxl_ml": null, "xxl_mr": null}}}, {"pk": 73, "creation_date": "2025-03-13T17:17:31.056Z", "position": 4, "plugin_type": "CardPlugin", "parent_id": 72, "data": {"ui_item": "Card", "tag_type": "div", "config": {"card_alignment": "center", "card_outline": "secondary", "card_text_color": "primary", "card_full_height": false, "attributes": {}, "background_context": "light", "background_opacity": "50", "background_shadow": "", "responsive_visibility": null, "margin_x": "", "margin_y": "", "margin_devices": null}}}, {"pk": 74, "creation_date": "2025-03-13T17:17:31.065Z", "position": 5, "plugin_type": "CardInnerPlugin", "parent_id": 73, "data": {"ui_item": "CardInner", "tag_type": "div", "config": {"inner_type": "card-body"}}}, {"pk": 75, "creation_date": "2025-03-13T17:20:28.371Z", "position": 6, "plugin_type": "TextPlugin", "parent_id": 74, "data": {"body": "<p><strong>Expensive</strong></p>"}}, {"pk": 76, "creation_date": "2025-03-13T17:16:12.141Z", "position": 7, "plugin_type": "GridColumnPlugin", "parent_id": 71, "data": {"ui_item": "GridColumn", "tag_type": "div", "config": {"column_alignment": null, "xs_col": null, "xs_order": null, "xs_offset": null, "xs_ml": null, "xs_mr": null, "sm_col": null, "sm_order": null, "sm_offset": null, "sm_ml": null, "sm_mr": null, "md_col": null, "md_order": null, "md_offset": null, "md_ml": null, "md_mr": null, "lg_col": null, "lg_order": null, "lg_offset": null, "lg_ml": null, "lg_mr": null, "xl_col": null, "xl_order": null, "xl_offset": null, "xl_ml": null, "xl_mr": null, "xxl_col": null, "xxl_order": null, "xxl_offset": null, "xxl_ml": null, "xxl_mr": null}}}, {"pk": 77, "creation_date": "2025-03-13T17:17:31.056Z", "position": 8, "plugin_type": "CardPlugin", "parent_id": 76, "data": {"ui_item": "Card", "tag_type": "div", "config": {"card_alignment": "", "card_outline": "secondary", "card_text_color": "primary", "card_full_height": false, "attributes": {}, "background_context": "light", "background_opacity": "50", "background_shadow": "", "responsive_visibility": null, "margin_x": "", "margin_y": "", "margin_devices": null}}}, {"pk": 78, "creation_date": "2025-03-13T17:17:31.065Z", "position": 9, "plugin_type": "CardInnerPlugin", "parent_id": 77, "data": {"ui_item": "CardInner", "tag_type": "div", "config": {"inner_type": "card-body"}}}, {"pk": 79, "creation_date": "2025-03-13T17:20:28.371Z", "position": 10, "plugin_type": "TextPlugin", "parent_id": 78, "data": {"body": "<p><strong>Medium</strong></p>\n\n<p><cms-plugin alt=\"Link - Home page \" title=\"Link - Home page\" id=\"80\"></cms-plugin></p>\n\n<p>\u00a0</p>"}}, {"pk": 80, "creation_date": "2025-03-14T22:18:23.726Z", "position": 11, "plugin_type": "LinkPlugin", "parent_id": 79, "data": {"template": "default", "name": "Home page", "link": {"internal_link": "cms.page:1", "anchor": "#adebayo"}, "target": "_blank", "attributes": {}}}, {"pk": 81, "creation_date": "2025-03-13T17:15:43.388Z", "position": 12, "plugin_type": "GridColumnPlugin", "parent_id": 71, "data": {"ui_item": "GridColumn", "tag_type": "div", "config": {"column_alignment": null, "xs_col": null, "xs_order": null, "xs_offset": null, "xs_ml": null, "xs_mr": null, "sm_col": null, "sm_order": null, "sm_offset": null, "sm_ml": null, "sm_mr": null, "md_col": null, "md_order": null, "md_offset": null, "md_ml": null, "md_mr": null, "lg_col": null, "lg_order": null, "lg_offset": null, "lg_ml": null, "lg_mr": null, "xl_col": null, "xl_order": null, "xl_offset": null, "xl_ml": null, "xl_mr": null, "xxl_col": null, "xxl_order": null, "xxl_offset": null, "xxl_ml": null, "xxl_mr": null}}}, {"pk": 82, "creation_date": "2025-03-13T17:17:31.056Z", "position": 13, "plugin_type": "CardPlugin", "parent_id": 81, "data": {"ui_item": "Card", "tag_type": "div", "config": {"card_alignment": "", "card_outline": "secondary", "card_text_color": "primary", "card_full_height": false, "attributes": {}, "background_context": "light", "background_opacity": "50", "background_shadow": "", "responsive_visibility": null, "margin_x": "", "margin_y": "", "margin_devices": null}}}, {"pk": 83, "creation_date": "2025-03-13T17:17:31.065Z", "position": 14, "plugin_type": "CardInnerPlugin", "parent_id": 82, "data": {"ui_item": "CardInner", "tag_type": "div", "config": {"inner_type": "card-body"}}}, {"pk": 84, "creation_date": "2025-03-13T17:20:28.371Z", "position": 15, "plugin_type": "TextPlugin", "parent_id": 83, "data": {"body": "<p><strong>Free</strong></p>"}}]}]

0 commit comments

Comments
 (0)