Autosetup implementation

This commit is contained in:
Iacopo Spalletti 2015-10-18 11:03:47 +02:00
parent 1133c5e48b
commit cd4f6553dd
5 changed files with 71 additions and 5 deletions

View File

@ -239,6 +239,8 @@ All the items will be created in every language configured for the website
and the pages will be published. If not using **aldryn-apphook-reload** or
**django CMS 3.2** auto-reload middleware you are required to reload the
project instance after this.
This will only work for the current website as detected by
``Site.objects.get_current()``.
The auto setup is execute once for each server start but it will skip any

View File

@ -2,3 +2,6 @@
__author__ = 'Iacopo Spalletti'
__email__ = 'i.spalletti@nephila.it'
__version__ = '0.6.0.dev1'
default_app_config = 'djangocms_blog.apps.BlogAppConfig'

View File

@ -1,10 +1,64 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import, print_function, unicode_literals
from django.apps import AppConfig
from django.utils.translation import ugettext_lazy as _
try:
from django.apps import AppConfig
except ImportError:
class AppConfig(object):
pass
class PageMetaConfig(AppConfig):
name = 'djangocms_page_meta'
verbose_name = _('django CMS Page Meta')
class BlogAppConfig(AppConfig):
name = 'djangocms_blog'
verbose_name = _('django CMS Blog')
@staticmethod
def setup():
from cms.api import create_page, create_title
from cms.exceptions import NoHomeFound
from cms.models import Page
from cms.utils import get_language_list
from cms.utils.conf import get_templates
from django.utils.translation import override
from .cms_appconfig import BlogConfig
from .settings import get_setting
if get_setting('AUTO_SETUP'):
configs = BlogConfig.objects.all()
if not configs.exists():
config = BlogConfig.objects.create(namespace='Blog')
langs = get_language_list()
blog = None
for lang in langs:
with override(lang):
config.set_current_language(lang)
config.app_title = get_setting('AUTO_APP_TITLE')
config.save()
default_template = get_templates()[0][0]
try:
home = Page.objects.get_home()
except NoHomeFound:
home = None
if not home:
home = create_page(
get_setting('AUTO_HOME_TITLE'), language=lang,
template=default_template, in_navigation=True, published=True
)
else:
create_title(
language=lang, title=get_setting('AUTO_HOME_TITLE'), page=home
)
home.publish(lang)
if not blog:
blog = create_page(
get_setting('AUTO_BLOG_TITLE'), language=lang, apphook='BlogApp',
apphook_namespace=config.namespace, parent=home,
template=default_template, in_navigation=True, published=True
)
else:
create_title(
language=lang, title=get_setting('AUTO_BLOG_TITLE'), page=blog
)
blog.publish(lang)

View File

@ -74,5 +74,10 @@ def get_setting(name):
'BLOG_DEFAULT_PUBLISHED': getattr(settings, 'BLOG_DEFAULT_PUBLISHED', False),
'BLOG_AVAILABLE_PERMALINK_STYLES': getattr(settings, 'BLOG_AVAILABLE_PERMALINK_STYLES', PERMALINKS), # NOQA
'BLOG_PERMALINK_URLS': getattr(settings, 'BLOG_PERMALINK_URLS', PERMALINKS_URLS),
'BLOG_AUTO_SETUP': getattr(settings, 'BLOG_AUTO_SETUP', True),
'BLOG_AUTO_HOME_TITLE': getattr(settings, 'BLOG_AUTO_HOME_TITLE', 'Home'),
'BLOG_AUTO_BLOG_TITLE': getattr(settings, 'BLOG_AUTO_BLOG_TITLE', 'Blog'),
'BLOG_AUTO_APP_TITLE': getattr(settings, 'BLOG_AUTO_APP_TITLE', 'Blog'),
}
return default['BLOG_%s' % name]

View File

@ -2,7 +2,7 @@
from __future__ import absolute_import, print_function, unicode_literals
from django.conf.urls import patterns, url
from .apps import BlogAppConfig
from .feeds import LatestEntriesFeed, TagFeed
from .settings import get_setting
from .views import (
@ -42,3 +42,5 @@ urlpatterns = patterns(
url(r'^tag/(?P<tag>[-\w]+)/feed/$',
TagFeed(), name='posts-tagged-feed'),
]
BlogAppConfig.setup()