From 70e6b6e52354dfec3b7e435ff491a24846ad5b68 Mon Sep 17 00:00:00 2001 From: Iacopo Spalletti Date: Wed, 25 Nov 2015 10:34:50 +0100 Subject: [PATCH] Fix setting author --- djangocms_blog/cms_wizards.py | 14 ++++++++++++ tests/test_wizards.py | 43 +++++++++++++++++++++++------------ 2 files changed, 43 insertions(+), 14 deletions(-) diff --git a/djangocms_blog/cms_wizards.py b/djangocms_blog/cms_wizards.py index d308278..56e706e 100644 --- a/djangocms_blog/cms_wizards.py +++ b/djangocms_blog/cms_wizards.py @@ -1,6 +1,11 @@ # -*- coding: utf-8 -*- from __future__ import absolute_import, print_function, unicode_literals +from cms.utils.permissions import get_current_user +from django.contrib.auth import get_user_model + +from djangocms_blog.settings import get_setting + try: from cms.wizards.wizard_base import Wizard from cms.wizards.wizard_pool import wizard_pool @@ -35,6 +40,15 @@ try: class Media: js = ('admin/js/jquery.js', 'admin/js/jquery.init.js',) + def save(self, commit=True): + if not self.instance.author_id and self.instance.app_config.set_author: + if get_setting('AUTHOR_DEFAULT') is True: + user = get_current_user() + else: + user = get_user_model().objects.get(username=get_setting('AUTHOR_DEFAULT')) + self.instance.author = user + return super(PostWizardForm, self).save(commit) + class PostWizard(Wizard): pass diff --git a/tests/test_wizards.py b/tests/test_wizards.py index 3cc6901..2f755d1 100644 --- a/tests/test_wizards.py +++ b/tests/test_wizards.py @@ -5,7 +5,9 @@ import sys from distutils.version import LooseVersion import cms +from cms.utils.permissions import current_user +from djangocms_blog.settings import get_setting from .base import BaseTest try: @@ -51,21 +53,34 @@ class WizardTest(BaseTest): from djangocms_blog.models import Post self.get_pages() - wizs = [entry for entry in wizard_pool.get_entries() if entry.model == Post] - for wiz in wizs: - app_config = self.app_config_1.pk if wiz.title == 'New Blog' else self.app_config_2.pk - form = wiz.form() - self.assertTrue(form.initial.get('app_config', False), app_config) - self.assertTrue(form.fields['app_config'].widget.attrs['disabled']) + with current_user(self.user_staff): + wizs = [entry for entry in wizard_pool.get_entries() if entry.model == Post] + for index, wiz in enumerate(wizs): + app_config = self.app_config_1.pk if wiz.title == 'New Blog' else self.app_config_2.pk + form = wiz.form() + self.assertTrue(form.initial.get('app_config', False), app_config) + self.assertTrue(form.fields['app_config'].widget.attrs['disabled']) - form = wiz.form(data={ - '1-title': 'title', - '1-abstract': 'abstract', - '1-categories': [self.category_1.pk], - }, prefix=1) - self.assertEqual(form.default_appconfig, app_config) - self.assertTrue(form.is_valid()) - self.assertTrue(form.cleaned_data['app_config'], app_config) + form = wiz.form(data={ + '1-title': 'title{0}'.format(index), + '1-abstract': 'abstract{0}'.format(index), + '1-categories': [self.category_1.pk], + }, prefix=1) + self.assertEqual(form.default_appconfig, app_config) + self.assertTrue(form.is_valid()) + self.assertTrue(form.cleaned_data['app_config'], app_config) + instance = form.save() + self.assertEqual(instance.author, self.user_staff) + + with self.settings(BLOG_AUTHOR_DEFAULT='normal'): + for index, wiz in enumerate(wizs): + form = wiz.form(data={ + '1-title': 'title-2{0}'.format(index), + '1-abstract': 'abstract-2{0}'.format(index), + '1-categories': [self.category_1.pk], + }, prefix=1) + instance = form.save() + self.assertEqual(instance.author, self.user_normal) def test_wizard_import(self): # The following import should not fail in any django CMS version