Merge pull request #392 from fsbraun/image_presets

Preset image and thumbnail sizes on a per app level.
This commit is contained in:
Iacopo Spalletti 2018-01-03 07:58:08 +01:00 committed by GitHub
commit 09b78eff17
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 51 additions and 0 deletions

View File

@ -412,6 +412,7 @@ class BlogConfigAdmin(BaseAppHookConfig, TranslatableAdmin):
'fields': (
'config.paginate_by', 'config.url_patterns', 'config.template_prefix',
'config.menu_structure', 'config.menu_empty_categories',
('config.default_image_full', 'config.default_image_thumbnail'),
),
'classes': ('collapse',)
}),

View File

@ -11,6 +11,11 @@ from parler.models import TranslatableModel, TranslatedFields
from .settings import MENU_TYPE_COMPLETE, get_setting
try: # pragma: no cover
from cmsplugin_filer_image.models import ThumbnailOption # NOQA
except ImportError: # pragma: no cover
from filer.models import ThumbnailOption # NOQA
class BlogConfig(TranslatableModel, AppHookConfig):
"""
@ -36,6 +41,20 @@ class BlogConfigForm(AppDataForm):
label=_('Post published by default'), required=False,
initial=get_setting('DEFAULT_PUBLISHED')
)
default_image_full = forms.ModelChoiceField(
label=_('Default size of full images'),
queryset=ThumbnailOption.objects.all(),
required=False,
help_text=_('If left empty the image size will have to be set for '
'every newly created post.'),
)
default_image_thumbnail = forms.ModelChoiceField(
label=_('Default size of thumbnail images'),
queryset=ThumbnailOption.objects.all(),
required=False,
help_text=_('If left empty the thumbnail image size will have to be '
'set for every newly created post.'),
)
url_patterns = forms.ChoiceField(
label=_('Permalink structure'), required=False,
initial=get_setting('AVAILABLE_PERMALINK_STYLES')[0][0],

View File

@ -56,6 +56,7 @@ class PostAdminForm(TranslatableModelForm):
qs = BlogCategory.objects
config = None
if getattr(self.instance, 'app_config_id', None):
qs = qs.namespace(self.instance.app_config.namespace)
elif 'initial' in kwargs and 'app_config' in kwargs['initial']:
@ -69,3 +70,9 @@ class PostAdminForm(TranslatableModelForm):
# Don't allow app_configs to be added here. The correct way to add an
# apphook-config is to create an apphook on a cms Page.
self.fields['app_config'].widget.can_add_related = False
if config:
self.initial['main_image_full'] = \
config.app_data['config'].get('default_image_full')
self.initial['main_image_thumbnail'] = \
config.app_data['config'].get('default_image_thumbnail')

View File

@ -30,6 +30,12 @@ from djangocms_blog.settings import MENU_TYPE_NONE, get_setting
from .base import BaseTest
try: # pragma: no cover
from cmsplugin_filer_image.models import ThumbnailOption # NOQA
except ImportError: # pragma: no cover
from filer.models import ThumbnailOption # NOQA
try:
from unittest import SkipTest
except ImportError:
@ -48,6 +54,20 @@ class AdminTest(BaseTest):
def setUp(self):
super(AdminTest, self).setUp()
admin.autodiscover()
self.default_thumbnail = ThumbnailOption.objects.create(
name='Blog thumbnail',
width=120,
height=120,
crop=True,
upscale=True,
)
self.default_full = ThumbnailOption.objects.create(
name='Blog image',
width=800,
height=200,
crop=True,
upscale=True,
)
def test_admin_post_views(self):
self.get_pages()
@ -237,6 +257,8 @@ class AdminTest(BaseTest):
self.assertFalse('abstract' in fsets[0][1]['fields'])
self.app_config_1.app_data.config.use_abstract = True
self.app_config_1.app_data.config.default_image_full = self.default_full
self.app_config_1.app_data.config.default_image_thumbnail = self.default_thumbnail
self.app_config_1.save()
with self.settings(BLOG_MULTISITE=True):
@ -264,6 +286,8 @@ class AdminTest(BaseTest):
self.category_1.pk, self.category_1.safe_translation_getter('name', language_code='en')
))
self.assertContains(response, 'id="id_sites" name="sites"')
self.assertContains(response, 'selected="selected">Blog image')
self.assertContains(response, 'selected="selected">Blog thumbnail')
self.user.sites.add(self.site_1)
with self.login_user_context(self.user):