Skip to content

Commit d85a3b1

Browse files
committed
feat: Add command to preload content
1 parent 3f834cf commit d85a3b1

File tree

8 files changed

+126
-0
lines changed

8 files changed

+126
-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+
democontent/

README.rst

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,19 @@ 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 add pages.
76+
77+
Here is how to use the command:
78+
1. To list the available files in the ``democontent`` folder.
79+
Run ``docker compose run --rm web python manage.py democontent``
80+
81+
2. To add page. Run ``docker compose run --rm web python manage.py democontent <FILE_PATH>``
82+
83+
7184
Updating requirements
7285
=====================
7386

backend/management/__init__.py

Whitespace-only changes.

backend/management/commands/__init__.py

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

democontent/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": {}}}}]}]

democontent/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)