diff --git a/cms_helper.py b/cms_helper.py index 4f77738..17d6bce 100755 --- a/cms_helper.py +++ b/cms_helper.py @@ -71,9 +71,6 @@ HELPER_SETTINGS = { 'hide_untranslated': False, } }, - 'MIDDLEWARE_CLASSES': [ - 'django.contrib.messages.middleware.MessageMiddleware', - ], 'MIGRATION_MODULES': { 'cmsplugin_filer_image': 'cmsplugin_filer_image.migrations_django', }, diff --git a/djangocms_blog/admin.py b/djangocms_blog/admin.py index 8c3e583..357a430 100755 --- a/djangocms_blog/admin.py +++ b/djangocms_blog/admin.py @@ -1,13 +1,14 @@ # -*- coding: utf-8 -*- +from copy import deepcopy + from admin_enhancer.admin import EnhancedModelAdminMixin from cms.admin.placeholderadmin import PlaceholderAdminMixin, FrontendEditableAdminMixin -from copy import deepcopy -from django.contrib import admin +from django import forms from django.conf import settings +from django.contrib import admin from django.contrib.auth import get_user_model from parler.admin import TranslatableAdmin -from .forms import PostAdminForm from .models import Post, BlogCategory from .settings import get_setting @@ -24,8 +25,7 @@ class BlogCategoryAdmin(EnhancedModelAdminMixin, TranslatableAdmin): class PostAdmin(EnhancedModelAdminMixin, FrontendEditableAdminMixin, - PlaceholderAdminMixin, TranslatableAdmin, admin.ModelAdmin): - form = PostAdminForm + PlaceholderAdminMixin, TranslatableAdmin): list_display = ['title', 'author', 'date_published', 'date_published_end'] date_hierarchy = 'date_published' raw_id_fields = ['author'] @@ -50,6 +50,16 @@ class PostAdmin(EnhancedModelAdminMixin, FrontendEditableAdminMixin, }), ] + def formfield_for_dbfield(self, db_field, **kwargs): + field = super(PostAdmin, self).formfield_for_dbfield(db_field, **kwargs) + if db_field.name == 'meta_description': + original_attrs = field.widget.attrs + original_attrs['maxlength'] = 160 + field.widget = forms.TextInput(original_attrs) + elif db_field.name == 'meta_title': + field.max_length = 70 + return field + def get_fieldsets(self, request, obj=None): fsets = deepcopy(self._fieldsets) if not get_setting('USE_PLACEHOLDER'): diff --git a/djangocms_blog/forms.py b/djangocms_blog/forms.py index edbac6d..b203bd7 100644 --- a/djangocms_blog/forms.py +++ b/djangocms_blog/forms.py @@ -1,11 +1,8 @@ # -*- coding: utf-8 -*- from django import forms from django.conf import settings -from parler.forms import TranslatableModelForm from taggit_autosuggest.widgets import TagAutoSuggest -from .models import Post - class LatestEntriesForm(forms.ModelForm): @@ -18,17 +15,3 @@ class LatestEntriesForm(forms.ModelForm): 'all': ('%sdjangocms_blog/css/%s' % (settings.STATIC_URL, 'djangocms_blog_admin.css'),) } - - -class PostAdminForm(TranslatableModelForm): - - def __init__(self, *args, **kwargs): - super(PostAdminForm, self).__init__(*args, **kwargs) - original_attrs = self.fields['meta_description'].widget.attrs - original_attrs['maxlength'] = 160 - self.fields['meta_description'].widget = forms.TextInput(original_attrs) - self.fields['meta_title'].max_length = 70 - - class Meta: - model = Post - exclude = () diff --git a/tests/__init__.py b/tests/__init__.py index 9531302..c4e61d0 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -145,8 +145,10 @@ class BaseTest(TestCase): request.errors = StringIO() return request - def post_request(self, page, lang, data): - request = self.request_factory.post(page.get_path(lang), data) + def post_request(self, page, lang, data, path=None): + if not path: + path = page.get_path(lang) + request = self.request_factory.post(path, data) request.current_page = page request.user = self.user request.session = {} diff --git a/tests/test_models.py b/tests/test_models.py index c6255fe..95e31af 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -90,6 +90,20 @@ class AdminTest(BaseTest): self.assertEqual(Post.objects.count(), 3) self.assertEqual(Post.objects.get(translations__slug='third-post').author.username, 'staff') + def test_admin_post_text(self): + page1, page2 = self.get_pages() + post = self._get_post(self.data['en'][0]) + + with self.settings(BLOG_USE_PLACEHOLDER=False): + data = {'post_text': 'ehi text'} + request = self.post_request(page1, 'en', data=data, path='/en/?edit_fields=post_text') + msg_mid = MessageMiddleware() + msg_mid.process_request(request) + post_admin = admin.site._registry[Post] + post_admin.edit_field(request, post.pk, 'en') + modified_post = Post.objects.get(pk=post.pk) + self.assertEqual(modified_post.safe_translation_getter('post_text'), data['post_text']) + class ModelsTest(BaseTest):