Skip to content

Commit

Permalink
Fix setting author
Browse files Browse the repository at this point in the history
  • Loading branch information
yakky committed Feb 1, 2016
1 parent 129a833 commit 2b35201
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 14 deletions.
14 changes: 14 additions & 0 deletions 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
Expand Down Expand Up @@ -35,6 +40,15 @@ class Meta:
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

Expand Down
43 changes: 29 additions & 14 deletions tests/test_wizards.py
Expand Up @@ -5,7 +5,9 @@
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:
Expand Down Expand Up @@ -51,21 +53,34 @@ def test_wizard_init(self):
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
Expand Down

0 comments on commit 2b35201

Please sign in to comment.