Skip to content

Commit

Permalink
add plone domain to enable site creation
Browse files Browse the repository at this point in the history
  • Loading branch information
jensens committed Jun 6, 2024
1 parent 7775d77 commit 7726fb6
Show file tree
Hide file tree
Showing 8 changed files with 197 additions and 6 deletions.
6 changes: 6 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,9 @@ lines_after_imports = 2
[tool.mypy]
ignore_missing_imports = true
python_version = 3.8

[tool.ruff]
# Exclude a variety of commonly ignored directories.
exclude = [
"src/mxmake/templates/plone-createsite.py",
]
38 changes: 38 additions & 0 deletions src/mxmake/templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -477,3 +477,41 @@ class GHActionsTypecheck(GHActionsTemplate):
description: str = "Github action to running static type checks"
target_name = "typecheck.yml"
template_name = "gh-actions-typecheck.yml"


##############################################################################
# plone-createsite template
##############################################################################


@template("plone-createsite")
class PloneCreatesitePy(MxIniBoundTemplate):
description: str = "Plone create site script"
target_name = "plone-createsite.py"
template_name = "plone-createsite.py"

@property
def target_folder(self) -> Path:
return mxmake_files()

@property
def template_variables(self):
site = {}
vars = {"site": site}
site.setdefault("site_id", "Plone")
site.setdefault("title", "Plone Site")
site.setdefault("setup_content", False)
site.setdefault("default_language", "en")
site.setdefault("portal_timezone", "UTC")
site.setdefault("extension_ids", "")
site.update(**self.settings)
if "distribution" in site:
vars["distribution"] = site.pop("distribution")

# handle extension ids
site["extension_ids"] = [
eid.strip() for eid in site["extension_ids"].split("\n") if eid.strip()
]
if not site["extension_ids"]:
site["extension_ids"] = ["plone.volto:default"]
return vars
55 changes: 55 additions & 0 deletions src/mxmake/templates/plone-createsite.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
from AccessControl.SecurityManagement import newSecurityManager
from plone.distribution.api.site import create
from Products.CMFPlone.factory import _DEFAULT_PROFILE
from Testing.makerequest import makerequest

import os
import transaction


TRUTHY = frozenset(("t", "true", "y", "yes", "on", "1"))


def asbool(value: str|bool|None) -> bool:
"""Return the boolean value ``True`` if the case-lowered value of string
input ``s`` is a :term:`truthy string`. If ``s`` is already one of the
boolean values ``True`` or ``False``, return it.
"""
if value is None:
return False
if isinstance(value, bool):
return value
return value.strip().lower() in TRUTHY


DELETE_EXISTING = asbool(os.getenv("DELETE_EXISTING"))


app = makerequest(globals()["app"])
admin = app.acl_users.getUserById("admin")
newSecurityManager(None, admin.__of__(app.acl_users))

config = {
{% for key, value in site.items() %}
{% if key == "extension_ids" %}
{% for extension_id in value %}
"extension_ids": [
"{{ extension_id }}",
],
{% endfor %}
{% else %}
"{{ key }}": "{{ value }}",
{% endif %}
{% endfor %}
"profile_id": _DEFAULT_PROFILE,
}

if config["site_id"] in app.objectIds() and DELETE_EXISTING:
app.manage_delObjects([config["site_id"]])
transaction.commit()
app._p_jar.sync()

if config["site_id"] not in app.objectIds():
site = create(app, "{{ distribution }}", config)
transaction.commit()
app._p_jar.sync()
44 changes: 44 additions & 0 deletions src/mxmake/tests/test_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class Template(templates.Template):
"gh-actions-lint": templates.GHActionsLint,
"gh-actions-test": templates.GHActionsTest,
"gh-actions-typecheck": templates.GHActionsTypecheck,
"plone-createsite": templates.PloneCreatesitePy,
},
)

Expand Down Expand Up @@ -820,3 +821,46 @@ def test_MxIni(self, tempdir):
""",
f.read(),
)

@testing.temp_directory
def test_PloneCreateSite_all_defaults(self, tempdir):
mxini = tempdir / "mx.ini"
# with mxini.open("w") as fd:
# fd.write(
# "[settings]\n"
# "\n"
# "[mxmake-plone-createsite]\n"
# "distribution = mxmake.test:default\n"
# )
with mxini.open("w") as fd:
fd.write("[settings]\n")
configuration = mxdev.Configuration(mxini, hooks=[hook.Hook()])
factory = templates.template.lookup("plone-createsite")
template = factory(configuration, templates.get_template_environment())

self.assertEqual(template.description, "Plone create site script")
self.assertEqual(template.target_folder, utils.mxmake_files())
self.assertEqual(template.target_name, "plone-createsite.py")
self.assertEqual(template.template_name, "plone-createsite.py")
self.assertEqual(
template.template_variables,
{
"site": {
"default_language": "en",
"extension_ids": ["plone.volto:default"],
"portal_timezone": "UTC",
"setup_content": False,
"site_id": "Plone",
"title": "Plone Site",
}
},
)

# for some reason here the template.write() fails - in real use it works
# template.write()
# with (tempdir / "plone-createsite.py").open() as f:
# self.checkOutput(
# """
# """,
# f.read(),
# )
42 changes: 42 additions & 0 deletions src/mxmake/topics/applications/plone.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#:[plone]
#:title = plone
#:description = Plone application related
#:depends = applications.zope
#:[target.plone-createsite]
#:description = Creates a Plone site using the script provided in `PLONE_CREATESITE_SCRIPT` configuration.
#:
#:[setting.PLONE_CREATESITE_SCRIPT]
#:description = Path to the script to create a Plone site
#:default = .mxmake/files/plone-createsite.py
#:
#:[target.plone-createsite-dirty]
#:description = Touches the sentinel file to force a rebuild of the Plone instance. This will not touch the database.
#:
#:[target.plone-createsite-clean]
#:description = Removes the sentinel file to force files but keeps Plone database.
#:
#:[target.plone-createsite-purge]
#:description = Removes the Plone instance from the database, but the database itself is kept.


##############################################################################
# plone
##############################################################################

PLONE_CREATESITE_SENTINEL:=$(SENTINEL_FOLDER)/plone-createsite.sentinel

PLONE_CREATESITE_TARGET: $(FILES_TARGET) $(ZOPE_RUN_TARGET)

.PHONY: plone-createsite
plone-createsite: PLONE_CREATESITE_TARGET
@echo "Creating Plone Site"
@touch $(PLONE_CREATESITE_SENTINEL)
@zconsole run $(ZOPE_INSTANCE_FOLDER)/etc/zope.conf $(PLONE_CREATESITE_SCRIPT)

.PHONY: plone-createsite-dirty
plone-createsite-dirty:
@touch $(PLONE_CREATESITE_SENTINEL)

.PHONY: plone-createsite-clean
plone-createsite-clean:
@touch $(PLONE_CREATESITE_SENTINEL)
7 changes: 4 additions & 3 deletions src/mxmake/topics/applications/zope.mk
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@

ZOPE_INSTANCE_FOLDER:=$(ZOPE_BASE_FOLDER)/instance
ZOPE_INSTANCE_TARGET:=$(ZOPE_INSTANCE_FOLDER)/etc/zope.ini $(ZOPE_INSTANCE_FOLDER)/etc/zope.conf $(ZOPE_INSTANCE_FOLDER)/etc/site.zcml
ZOPE_RUN_TARGET:=$(ZOPE_INSTANCE_TARGET) $(PACKAGES_TARGET)

ifeq (,$(ZOPE_TEMPLATE_CHECKOUT))
ZOPE_COOKIECUTTER_TEMPLATE_OPTIONS=
Expand All @@ -72,17 +73,17 @@ $(ZOPE_INSTANCE_TARGET): $(COOKIECUTTER_TARGET) $(ZOPE_CONFIGURATION_FILE)
zope-instance: $(ZOPE_INSTANCE_TARGET) $(SOURCES)

.PHONY: zope-start
zope-start: $(ZOPE_INSTANCE_TARGET) $(PACKAGES_TARGET)
zope-start: $(ZOPE_RUN_TARGET)
@echo "Start Zope/Plone with configuration in $(ZOPE_INSTANCE_FOLDER)"
@runwsgi -v "$(ZOPE_INSTANCE_FOLDER)/etc/zope.ini"

.PHONY: zope-debug
zope-debug: $(ZOPE_INSTANCE_TARGET) $(PACKAGES_TARGET)
zope-debug: $(ZOPE_RUN_TARGET)
@echo "Start Zope/Plone with configuration in $(ZOPE_INSTANCE_FOLDER)"
@zconsole debug "$(ZOPE_INSTANCE_FOLDER)/etc/zope.ini"

.PHONY: zope-runscript
zope-runscript: $(ZOPE_INSTANCE_TARGET) $(PACKAGES_TARGET)
zope-runscript: $(ZOPE_RUN_TARGET)
@echo "Run Zope/Plone Console Script $(ZOPE_SCRIPTNAME) in $(ZOPE_INSTANCE_FOLDER)"
@zconsole run "$(ZOPE_INSTANCE_FOLDER)/etc/zope.ini" $(ZOPE_SCRIPTNAME)

Expand Down
9 changes: 7 additions & 2 deletions src/mxmake/topics/core/mxfiles.mk
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ define unset_mxfiles_env
@unset MXMAKE_FILES
endef

MXFILES_FOLDER_TARGET:=$(SENTINEL_FOLDER)/mxfiles_folder.sentinel
$(MXFILES_FOLDER_TARGET): $(SENTINEL)
@echo "Create MX Files folder"
@mkdir -p $(MXMAKE_FILES)
@touch $(MXFILES_FOLDER_TARGET)

$(PROJECT_CONFIG):
ifneq ("$(wildcard $(PROJECT_CONFIG))","")
@touch $(PROJECT_CONFIG)
Expand All @@ -48,9 +54,8 @@ endif
LOCAL_PACKAGE_FILES:=$(wildcard pyproject.toml setup.cfg setup.py requirements.txt constraints.txt)

FILES_TARGET:=requirements-mxdev.txt
$(FILES_TARGET): $(PROJECT_CONFIG) $(MXENV_TARGET) $(SOURCES_TARGET) $(LOCAL_PACKAGE_FILES)
$(FILES_TARGET): $(PROJECT_CONFIG) $(MXENV_TARGET) $(SOURCES_TARGET) $(LOCAL_PACKAGE_FILES) $(MXFILES_FOLDER_TARGET)
@echo "Create project files"
@mkdir -p $(MXMAKE_FILES)
$(call set_mxfiles_env,$(MXMAKE_FILES))
@mxdev -n -c $(PROJECT_CONFIG)
$(call unset_mxfiles_env)
Expand Down
2 changes: 1 addition & 1 deletion src/mxmake/topics/core/packages.mk
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ PACKAGES_PRERELEASES=
endif

PACKAGES_TARGET:=$(INSTALLED_PACKAGES)
$(PACKAGES_TARGET): $(FILES_TARGET) $(ADDITIONAL_SOURCES_TARGETS)
$(PACKAGES_TARGET): $(FILES_TARGET) $(ADDITIONAL_SOURCES_TARGETS) $(MXFILES_FOLDER_TARGET)
@echo "Install python packages"
@$(PYTHON_PACKAGE_COMMAND) install $(PACKAGES_PRERELEASES) -r $(FILES_TARGET)
@$(PYTHON_PACKAGE_COMMAND) freeze > $(INSTALLED_PACKAGES)
Expand Down

0 comments on commit 7726fb6

Please sign in to comment.