Merge pull request #157 from nephila/feature/external_setup

Use djangocms-apphook-setup
This commit is contained in:
Iacopo Spalletti 2015-10-25 23:41:02 +01:00
commit 8459fe2557
12 changed files with 61 additions and 77 deletions

View file

@ -65,6 +65,8 @@ Features
* Per-Apphook configuration * Per-Apphook configuration
* Per-Apphook templates set * Per-Apphook templates set
* Auto Apphook setup * Auto Apphook setup
* Support for django CMS 3.2+ Wizard
* Haystack index support
Quickstart Quickstart
---------- ----------
@ -233,7 +235,7 @@ This feature is enable by default and will create:
* a ``BlogConfig`` with default values * a ``BlogConfig`` with default values
* a ``Blog`` CMS page and will attach ``djangocms_blog`` instance to it * a ``Blog`` CMS page and will attach ``djangocms_blog`` instance to it
* a **home page** is no, home is found. * a **home page** if no home is found.
All the items will be created in every language configured for the website 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 and the pages will be published. If not using **aldryn-apphook-reload** or
@ -301,6 +303,7 @@ Global Settings
* BLOG_AVAILABLE_PERMALINK_STYLES: Choices of permalinks styles; * BLOG_AVAILABLE_PERMALINK_STYLES: Choices of permalinks styles;
* BLOG_PERMALINK_URLS: URLConf corresponding to * BLOG_PERMALINK_URLS: URLConf corresponding to
BLOG_AVAILABLE_PERMALINK_STYLES; BLOG_AVAILABLE_PERMALINK_STYLES;
* BLOG_DEFAULT_OBJECT_NAME: Default name for Blog item (used in django CMS Wizard);
* BLOG_AUTO_SETUP: Enable the blog **Auto setup** feature; (default: ``True``) * BLOG_AUTO_SETUP: Enable the blog **Auto setup** feature; (default: ``True``)
* BLOG_AUTO_HOME_TITLE: Title of the home page created by **Auto setup**; * BLOG_AUTO_HOME_TITLE: Title of the home page created by **Auto setup**;
(default: ``Home``) (default: ``Home``)
@ -313,6 +316,7 @@ Per-Apphook settings
-------------------- --------------------
* application title: Free text title that can be used as title in templates; * application title: Free text title that can be used as title in templates;
* object name: Free text label for Blog items in django CMS Wizard;
* Post published by default: Per-Apphook setting for BLOG_DEFAULT_PUBLISHED; * Post published by default: Per-Apphook setting for BLOG_DEFAULT_PUBLISHED;
* Permalink structure: Per-Apphook setting for * Permalink structure: Per-Apphook setting for
BLOG_AVAILABLE_PERMALINK_STYLES; BLOG_AVAILABLE_PERMALINK_STYLES;

View file

@ -13,53 +13,3 @@ except ImportError:
class BlogAppConfig(AppConfig): class BlogAppConfig(AppConfig):
name = 'djangocms_blog' name = 'djangocms_blog'
verbose_name = _('django CMS 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
)
elif lang not in home.get_languages():
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

@ -4,16 +4,29 @@ from __future__ import absolute_import, print_function, unicode_literals
from aldryn_apphooks_config.app_base import CMSConfigApp from aldryn_apphooks_config.app_base import CMSConfigApp
from cms.apphook_pool import apphook_pool from cms.apphook_pool import apphook_pool
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from djangocms_apphook_setup.base import AutoCMSAppMixin
from .cms_appconfig import BlogConfig from .cms_appconfig import BlogConfig
from .menu import BlogCategoryMenu from .menu import BlogCategoryMenu
from .settings import get_setting
class BlogApp(CMSConfigApp): class BlogApp(AutoCMSAppMixin, CMSConfigApp):
name = _('Blog') name = _('Blog')
urls = ['djangocms_blog.urls'] urls = ['djangocms_blog.urls']
app_name = 'djangocms_blog' app_name = 'djangocms_blog'
app_config = BlogConfig app_config = BlogConfig
menus = [BlogCategoryMenu] menus = [BlogCategoryMenu]
auto_setup = {
'enabled': get_setting('AUTO_SETUP'),
'home title': get_setting('AUTO_HOME_TITLE'),
'page title': get_setting('AUTO_BLOG_TITLE'),
'namespace': get_setting('AUTO_NAMESPACE'),
'config_fields': {},
'config_translated_fields': {
'app_title': get_setting('AUTO_APP_TITLE'),
'object_name': get_setting('DEFAULT_OBJECT_NAME')
},
}
apphook_pool.register(BlogApp) apphook_pool.register(BlogApp)
BlogApp.setup()

View file

@ -18,7 +18,9 @@ class BlogConfig(TranslatableModel, AppHookConfig):
""" """
translations = TranslatedFields( translations = TranslatedFields(
app_title=models.CharField(_('application title'), max_length=234), app_title=models.CharField(_('application title'), max_length=234),
object_name=models.CharField(_('object name'), max_length=234, default='Post'), object_name=models.CharField(
_('object name'), max_length=234, default=get_setting('DEFAULT_OBJECT_NAME')
),
) )
def get_app_title(self): def get_app_title(self):

View file

@ -6,7 +6,6 @@ from cms.toolbar_pool import toolbar_pool
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
from django.utils.translation import override, ugettext_lazy as _ from django.utils.translation import override, ugettext_lazy as _
from .apps import BlogAppConfig
from .models import BLOG_CURRENT_NAMESPACE, BLOG_CURRENT_POST_IDENTIFIER from .models import BLOG_CURRENT_NAMESPACE, BLOG_CURRENT_POST_IDENTIFIER
@ -52,5 +51,3 @@ class BlogToolbar(CMSToolbar):
menu.remove_item(pagetags) menu.remove_item(pagetags)
except ImportError: except ImportError:
pass pass
BlogAppConfig.setup()

View file

@ -4,6 +4,7 @@ from __future__ import absolute_import, print_function, unicode_literals
try: try:
from cms.wizards.wizard_base import Wizard from cms.wizards.wizard_base import Wizard
from cms.wizards.wizard_pool import wizard_pool from cms.wizards.wizard_pool import wizard_pool
from django import forms
from django.utils.text import slugify from django.utils.text import slugify
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from parler.forms import TranslatableModelForm from parler.forms import TranslatableModelForm
@ -20,12 +21,19 @@ try:
if kwargs.get('data', False): if kwargs.get('data', False):
kwargs['data']['1-app_config'] = self.default_appconfig kwargs['data']['1-app_config'] = self.default_appconfig
super(PostWizardForm, self).__init__(*args, **kwargs) super(PostWizardForm, self).__init__(*args, **kwargs)
self.fields['app_config'].widget = forms.Select(
attrs=self.fields['app_config'].widget.attrs,
choices=self.fields['app_config'].widget.choices,
)
self.fields['app_config'].widget.attrs['disabled'] = True self.fields['app_config'].widget.attrs['disabled'] = True
class Meta: class Meta:
model = Post model = Post
fields = ['app_config', 'title', 'abstract', 'categories'] fields = ['app_config', 'title', 'abstract', 'categories']
class Media:
js = ('admin/js/jquery.js', 'admin/js/jquery.init.js',)
class PostWizard(Wizard): class PostWizard(Wizard):
pass pass

View file

@ -4,6 +4,7 @@ from __future__ import unicode_literals
from django.db import migrations, models from django.db import migrations, models
import django.utils.timezone import django.utils.timezone
import aldryn_apphooks_config.fields import aldryn_apphooks_config.fields
from djangocms_blog.settings import get_setting
class Migration(migrations.Migration): class Migration(migrations.Migration):
@ -16,7 +17,7 @@ class Migration(migrations.Migration):
migrations.AddField( migrations.AddField(
model_name='blogconfigtranslation', model_name='blogconfigtranslation',
name='object_name', name='object_name',
field=models.CharField(verbose_name='object name', default='Post', max_length=234), field=models.CharField(verbose_name='object name', default=get_setting('DEFAULT_OBJECT_NAME'), max_length=234),
), ),
migrations.AlterField( migrations.AlterField(
model_name='authorentriesplugin', model_name='authorentriesplugin',

View file

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

View file

@ -3,6 +3,7 @@ from south.utils import datetime_utils as datetime
from south.db import db from south.db import db
from south.v2 import SchemaMigration from south.v2 import SchemaMigration
from django.db import models from django.db import models
from djangocms_blog.settings import get_setting
class Migration(SchemaMigration): class Migration(SchemaMigration):
@ -10,7 +11,7 @@ class Migration(SchemaMigration):
def forwards(self, orm): def forwards(self, orm):
# Adding field 'BlogConfigTranslation.object_name' # Adding field 'BlogConfigTranslation.object_name'
db.add_column('djangocms_blog_blogconfig_translation', 'object_name', db.add_column('djangocms_blog_blogconfig_translation', 'object_name',
self.gf('django.db.models.fields.CharField')(default='Post', max_length=234), self.gf('django.db.models.fields.CharField')(default=get_setting('DEFAULT_OBJECT_NAME'), max_length=234),
keep_default=False) keep_default=False)

View file

@ -49,6 +49,7 @@ setup(
'django-meta>=0.2', 'django-meta>=0.2',
'django-meta-mixin>=0.2.1', 'django-meta-mixin>=0.2.1',
'aldryn-apphooks-config>=0.2.6', 'aldryn-apphooks-config>=0.2.6',
'djangocms-apphook-setup',
'aldryn-search' 'aldryn-search'
], ],
license='BSD', license='BSD',

View file

@ -17,34 +17,37 @@ class SetupTest(BaseTest):
@classmethod @classmethod
def setUpClass(cls): def setUpClass(cls):
# Skipping initialization to start with clean database
super(BaseTest, cls).setUpClass() super(BaseTest, cls).setUpClass()
def setUp(self):
super(SetupTest, self).setUp()
from cms.apphook_pool import apphook_pool
delete = [
'djangocms_blog',
'djangocms_blog.cms_app',
'djangocms_blog.cms_apps',
]
for module in delete:
if module in sys.modules:
del sys.modules[module]
BlogConfig.cmsapp = None
apphook_pool.clear()
def test_setup_from_url(self): def test_setup_from_url(self):
# Tests starts with no page and no config # Tests starts with no page and no config
self.assertFalse(Page.objects.exists()) self.assertFalse(Page.objects.exists())
self.assertFalse(BlogConfig.objects.exists()) self.assertFalse(BlogConfig.objects.exists())
# importing admin triggers the auto setup # importing cms_app triggers the auto setup
from djangocms_blog import cms_toolbar # NOQA from djangocms_blog import cms_app # NOQA
# Home and blog, published and draft # Home and blog, published and draft
self.assertEqual(Page.objects.count(), 4) self.assertEqual(Page.objects.count(), 4)
self.assertEqual(BlogConfig.objects.count(), 1) self.assertEqual(BlogConfig.objects.count(), 1)
def setUp(self):
from cms.toolbar_pool import toolbar_pool
from djangocms_blog import cms_toolbar
toolbar_pool.unregister(cms_toolbar.BlogToolbar)
delete = [
'djangocms_blog',
'djangocms_blog.cms_toolbar',
]
for module in delete:
if module in sys.modules:
del sys.modules[module]
def test_setup_filled(self): def test_setup_filled(self):
# Tests starts with no page and no config # Tests starts with no page and no config
@ -66,8 +69,8 @@ class SetupTest(BaseTest):
) )
home.publish(lang) home.publish(lang)
# importing admin triggers the auto setup # importing cms_app triggers the auto setup
from djangocms_blog import cms_toolbar # NOQA from djangocms_blog import cms_app # NOQA
# Home and blog, published and draft # Home and blog, published and draft
self.assertEqual(Page.objects.count(), 4) self.assertEqual(Page.objects.count(), 4)

View file

@ -21,6 +21,7 @@ deps =
py26: unittest2 py26: unittest2
django-parler<1.5 django-parler<1.5
https://github.com/aldryn/aldryn-apphooks-config/archive/master.zip https://github.com/aldryn/aldryn-apphooks-config/archive/master.zip
https://github.com/nephila/djangocms-apphook-setup/archive/master.zip
[testenv:isort] [testenv:isort]
deps = isort deps = isort