1
1
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
4
4
from Testing .makerequest import makerequest
5
5
6
- import transaction
6
+ import json
7
+ import logging
7
8
import os
9
+ import transaction
10
+
11
+
12
+ logging .basicConfig (format = "%(message)s" )
8
13
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 )
9
20
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" ))
11
27
12
28
13
29
def asbool (s ):
@@ -22,7 +38,7 @@ def asbool(s):
22
38
return s .lower () in truthy
23
39
24
40
25
- app = makerequest (app )
41
+ app = makerequest (globals ()[ " app" ] )
26
42
27
43
request = app .REQUEST
28
44
@@ -31,56 +47,60 @@ def asbool(s):
31
47
newSecurityManager (None , admin )
32
48
33
49
# 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" )
37
51
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 ])
80
91
transaction .commit ()
81
92
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
+ )
85
104
transaction .commit ()
86
105
app ._p_jar .sync ()
106
+ logger .info (" - Site created!" )
0 commit comments