Skip to content

Commit feede00

Browse files
Update create_site.py to create a site using a distribution.
1 parent f70d074 commit feede00

File tree

2 files changed

+81
-53
lines changed

2 files changed

+81
-53
lines changed

skeleton/scripts/create_site.py

Lines changed: 73 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,29 @@
11
from AccessControl.SecurityManagement import newSecurityManager
2-
from Products.CMFPlone.factory import _DEFAULT_PROFILE
3-
from Products.CMFPlone.factory import addPloneSite
2+
from pathlib import Path
3+
from plone.distribution.api import site as site_api
44
from Testing.makerequest import makerequest
55

6-
import transaction
6+
import json
7+
import logging
78
import os
9+
import transaction
10+
11+
12+
logging.basicConfig(format="%(message)s")
813

14+
# Silence some loggers
15+
for logger_name in [
16+
"GenericSetup.componentregistry",
17+
"Products.MimetypesRegistry.MimeTypesRegistry",
18+
]:
19+
logging.getLogger(logger_name).setLevel(logging.ERROR)
920

10-
truthy = frozenset(('t', 'true', 'y', 'yes', 'on', '1'))
21+
logger = logging.getLogger("Plone Site Creation")
22+
logger.setLevel(logging.DEBUG)
23+
24+
SCRIPT_DIR = Path().cwd() / "scripts"
25+
26+
truthy = frozenset(("t", "true", "y", "yes", "on", "1"))
1127

1228

1329
def asbool(s):
@@ -22,7 +38,7 @@ def asbool(s):
2238
return s.lower() in truthy
2339

2440

25-
app = makerequest(app)
41+
app = makerequest(globals()["app"])
2642

2743
request = app.REQUEST
2844

@@ -31,56 +47,60 @@ def asbool(s):
3147
newSecurityManager(None, admin)
3248

3349
# VARS
34-
TYPE = os.getenv("TYPE", "volto")
35-
SITE_ID = os.getenv("SITE_ID", "Plone")
36-
SETUP_CONTENT = asbool(os.getenv("SETUP_CONTENT"))
50+
ANSWERS = os.getenv("ANSWERS", "default")
3751
DELETE_EXISTING = asbool(os.getenv("DELETE_EXISTING"))
38-
LANGUAGE = os.getenv("LANGUAGE", "en")
39-
TIMEZONE = os.getenv("TIMEZONE", "Europe/Berlin")
40-
ADDITIONAL_PROFILES = os.getenv("PROFILES", os.getenv("ADDITIONAL_PROFILES", ""))
41-
42-
PROFILES = {
43-
"volto": [
44-
"plone.app.caching:default",
45-
"plonetheme.barceloneta:default",
46-
"plone.volto:default",
47-
"plone.volto:default-homepage",
48-
],
49-
"classic": [
50-
"plone.app.caching:default",
51-
"plonetheme.barceloneta:default",
52-
],
53-
}
54-
55-
56-
def profile_ids(site_type):
57-
extension_ids = PROFILES[site_type]
58-
if ADDITIONAL_PROFILES:
59-
extension_ids.extend(
60-
[
61-
profile.strip()
62-
for profile in ADDITIONAL_PROFILES.split(" ")
63-
if profile.strip()
64-
]
65-
)
66-
return extension_ids
67-
68-
69-
payload = {
70-
"title": "Plone",
71-
"profile_id": _DEFAULT_PROFILE,
72-
"extension_ids": profile_ids(TYPE),
73-
"setup_content": SETUP_CONTENT,
74-
"default_language": LANGUAGE,
75-
"portal_timezone": TIMEZONE,
76-
}
77-
78-
if SITE_ID in app.objectIds() and DELETE_EXISTING:
79-
app.manage_delObjects([SITE_ID])
52+
DISTRIBUTION = os.getenv("DISTRIBUTION", "")
53+
54+
if not DISTRIBUTION:
55+
# We used to support setting TYPE 'volto' or 'classic'.
56+
TYPE = os.getenv("TYPE", "")
57+
if TYPE == "classic":
58+
DISTRIBUTION = "classic"
59+
elif TYPE == "volto":
60+
DISTRIBUTION = "default"
61+
62+
# Load default site creation parameters
63+
answers_file = SCRIPT_DIR / f"{ANSWERS}.json"
64+
answers = json.loads(answers_file.read_text())
65+
66+
# Override the defaults from the OS environment
67+
if DISTRIBUTION:
68+
answers["distribution"] = DISTRIBUTION
69+
SITE_ID = os.getenv("SITE_ID")
70+
if SITE_ID:
71+
answers["site_id"] = SITE_ID
72+
LANGUAGE = os.getenv("LANGUAGE")
73+
if LANGUAGE:
74+
answers["default_language"] = LANGUAGE
75+
SETUP_CONTENT = os.getenv("SETUP_CONTENT")
76+
if SETUP_CONTENT is not None:
77+
answers["setup_content"] = asbool(SETUP_CONTENT)
78+
TIMEZONE = os.getenv("TIMEZONE")
79+
if TIMEZONE:
80+
answers["portal_timezone"] = TIMEZONE
81+
82+
# Get the final site_id and distribution from the updated answers.
83+
site_id = answers["site_id"]
84+
DISTRIBUTION = answers["distribution"]
85+
logger.info(f"Creating a new Plone site @ {site_id}")
86+
logger.info(f" - Using the {DISTRIBUTION} distribution and answers from {answers_file}")
87+
88+
89+
if site_id in app.objectIds() and DELETE_EXISTING:
90+
app.manage_delObjects([site_id])
8091
transaction.commit()
8192
app._p_jar.sync()
82-
83-
if SITE_ID not in app.objectIds():
84-
site = addPloneSite(app, SITE_ID, **payload)
93+
logger.info(f" - Deleted existing site with id {site_id}")
94+
else:
95+
logger.info(
96+
f" - Stopping site creation, as there is already a site with id {site_id}. "
97+
"Set DELETE_EXISTING=1 to delete the existing site before creating a new one."
98+
)
99+
100+
if site_id not in app.objectIds():
101+
site = site_api._create_site(
102+
context=app, distribution_name=DISTRIBUTION, answers=answers
103+
)
85104
transaction.commit()
86105
app._p_jar.sync()
106+
logger.info(" - Site created!")

skeleton/scripts/default.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"site_id": "Plone",
3+
"title": "Welcome to Plone 6",
4+
"description": "Site created with a new Plone Distribution",
5+
"default_language": "en",
6+
"portal_timezone": "Europe/Berlin",
7+
"setup_content": true
8+
}

0 commit comments

Comments
 (0)