Fix setting author

This commit is contained in:
Iacopo Spalletti 2015-11-25 10:34:50 +01:00
parent 129a8337bb
commit 70e6b6e523
2 changed files with 43 additions and 14 deletions

View file

@ -1,6 +1,11 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from __future__ import absolute_import, print_function, unicode_literals 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: 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
@ -35,6 +40,15 @@ try:
class Media: class Media:
js = ('admin/js/jquery.js', 'admin/js/jquery.init.js',) 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): class PostWizard(Wizard):
pass pass

View file

@ -5,7 +5,9 @@ import sys
from distutils.version import LooseVersion from distutils.version import LooseVersion
import cms import cms
from cms.utils.permissions import current_user
from djangocms_blog.settings import get_setting
from .base import BaseTest from .base import BaseTest
try: try:
@ -51,21 +53,34 @@ class WizardTest(BaseTest):
from djangocms_blog.models import Post from djangocms_blog.models import Post
self.get_pages() self.get_pages()
with current_user(self.user_staff):
wizs = [entry for entry in wizard_pool.get_entries() if entry.model == Post] wizs = [entry for entry in wizard_pool.get_entries() if entry.model == Post]
for wiz in wizs: for index, wiz in enumerate(wizs):
app_config = self.app_config_1.pk if wiz.title == 'New Blog' else self.app_config_2.pk app_config = self.app_config_1.pk if wiz.title == 'New Blog' else self.app_config_2.pk
form = wiz.form() form = wiz.form()
self.assertTrue(form.initial.get('app_config', False), app_config) self.assertTrue(form.initial.get('app_config', False), app_config)
self.assertTrue(form.fields['app_config'].widget.attrs['disabled']) self.assertTrue(form.fields['app_config'].widget.attrs['disabled'])
form = wiz.form(data={ form = wiz.form(data={
'1-title': 'title', '1-title': 'title{0}'.format(index),
'1-abstract': 'abstract', '1-abstract': 'abstract{0}'.format(index),
'1-categories': [self.category_1.pk], '1-categories': [self.category_1.pk],
}, prefix=1) }, prefix=1)
self.assertEqual(form.default_appconfig, app_config) self.assertEqual(form.default_appconfig, app_config)
self.assertTrue(form.is_valid()) self.assertTrue(form.is_valid())
self.assertTrue(form.cleaned_data['app_config'], app_config) 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): def test_wizard_import(self):
# The following import should not fail in any django CMS version # The following import should not fail in any django CMS version