Hi Patrick,
here's some example code.
Context: Sometimes I put links into the toolbar menu where the editors
can add new things, like tv broadcasts, or clay vases. When the new
thing is a CMSPlugin and should appear on a newly created page, I let
their model class inherit from this mixin and call `ensure_is_in_page`
from the admin class.
class CreatePageMixin(object):
def ensure_is_in_page(self, user, parent_page_id):
try:
self.placeholder.page.pk
except AttributeError:
page = self.create_page(user, parent_page_id)
self.assign_to_page(page)
def create_page(self, user, parent_page_id):
from cms import api # Not available before model creation
try:
parent = Page.objects.get(pk=parent_page_id)
except Page.DoesNotExist:
parent = self.get_parent_page() # to implement if required
page = api.create_page(
self.name,
self.PAGE_TEMPLATE, # define this in each child class
'de', # or any other language
created_by=user,
parent=parent
)
return page
def assign_to_page(self, page):
placeholder = page.placeholders.get(slot=self.PLACEHOLDER)
self.placeholder = placeholder
self.language = 'de'
self.position = 0
self.plugin_type = self.PLUGIN_TYPE
That's probably not exactly what you need but should give you an idea of
what's possible.
Best
Philipp
--
Philipp Zedler
Developer für's Web
Am langen Rain 12
39031 Bruneck
Italien
++39 / 324 / 77 345 96
http://www.zedler.it