Merge pull request #46 from nephila/feature/author_optional

Make auto adding author optional
This commit is contained in:
Iacopo Spalletti 2014-10-11 09:33:36 +02:00
commit 9ef2f2e5d4
8 changed files with 54 additions and 1 deletions

View file

@ -9,6 +9,7 @@ HELPER_SETTINGS = {
], ],
'ROOT_URLCONF': 'tests.test_utils.urls', 'ROOT_URLCONF': 'tests.test_utils.urls',
'INSTALLED_APPS': [ 'INSTALLED_APPS': [
'django.contrib.messages',
'admin_enhancer', 'admin_enhancer',
'filer', 'filer',
'parler', 'parler',
@ -71,6 +72,9 @@ HELPER_SETTINGS = {
'hide_untranslated': False, 'hide_untranslated': False,
} }
}, },
'MIDDLEWARE_CLASSES': [
'django.contrib.messages.middleware.MessageMiddleware',
],
'META_SITE_PROTOCOL': 'http', 'META_SITE_PROTOCOL': 'http',
'META_SITE_DOMAIN': 'example.com', 'META_SITE_DOMAIN': 'example.com',
'META_USE_OG_PROPERTIES': True, 'META_USE_OG_PROPERTIES': True,

View file

@ -61,7 +61,8 @@ class PostAdmin(EnhancedModelAdminMixin, FrontendEditableAdmin,
return {'slug': ('title',)} return {'slug': ('title',)}
def save_model(self, request, obj, form, change): def save_model(self, request, obj, form, change):
if not obj.author_id: from .settings import BLOG_AUTHOR_AUTO
if not obj.author_id and BLOG_AUTHOR_AUTO:
obj.author = request.user obj.author = request.user
super(PostAdmin, self).save_model(request, obj, form, change) super(PostAdmin, self).save_model(request, obj, form, change)

View file

@ -180,10 +180,12 @@ class Post(ModelMeta, TranslatableModel):
def save(self, *args, **kwargs): def save(self, *args, **kwargs):
super(Post, self).save(*args, **kwargs) super(Post, self).save(*args, **kwargs)
main_lang = self.get_current_language()
for lang in self.get_available_languages(): for lang in self.get_available_languages():
self.set_current_language(lang) self.set_current_language(lang)
if not self.slug and self.title: if not self.slug and self.title:
self.slug = slugify(self.title) self.slug = slugify(self.title)
self.set_current_language(main_lang)
self.save_translations() self.save_translations()
def get_absolute_url(self): def get_absolute_url(self):

View file

@ -34,3 +34,4 @@ BLOG_GPLUS_AUTHOR = getattr(settings, 'BLOG_GPLUS_AUTHOR', 'get_author_gplus')
BLOG_ENABLE_COMMENTS = getattr(settings, 'BLOG_ENABLE_COMMENTS', True) BLOG_ENABLE_COMMENTS = getattr(settings, 'BLOG_ENABLE_COMMENTS', True)
BLOG_USE_PLACEHOLDER = getattr(settings, 'BLOG_USE_PLACEHOLDER', True) BLOG_USE_PLACEHOLDER = getattr(settings, 'BLOG_USE_PLACEHOLDER', True)
BLOG_MULTISITE = getattr(settings, 'BLOG_MULTISITE', True) BLOG_MULTISITE = getattr(settings, 'BLOG_MULTISITE', True)
BLOG_AUTHOR_AUTO = getattr(settings, 'BLOG_AUTHOR_AUTO', True)

View file

@ -6,9 +6,11 @@
<h3><a href="{{ post.get_absolute_url }}">{{ post.title }}</a></h3> <h3><a href="{{ post.get_absolute_url }}">{{ post.title }}</a></h3>
{% block blog_meta %} {% block blog_meta %}
<ul class="post-detail"> <ul class="post-detail">
{% if post.author %}
<li> <li>
{% trans "by" %} <a href="{% url 'djangocms_blog:posts-author' post.author.get_username %}">{{ post.author.get_full_name }}</a> {% trans "by" %} <a href="{% url 'djangocms_blog:posts-author' post.author.get_username %}">{{ post.author.get_full_name }}</a>
</li> </li>
{% endif %}
<li> <li>
{{ post.date_published|date:"M d, Y" }} {{ post.date_published|date:"M d, Y" }}
</li> </li>

View file

@ -13,9 +13,11 @@
<h2>{% render_model post "title" %}</h2> <h2>{% render_model post "title" %}</h2>
{% block blog_meta %} {% block blog_meta %}
<ul class="post-detail"> <ul class="post-detail">
{% if post.author %}
<li> <li>
{% trans "by" %} <a href="{% url 'djangocms_blog:posts-author' post.author.get_username %}">{{ post.author.get_full_name }}</a> {% trans "by" %} <a href="{% url 'djangocms_blog:posts-author' post.author.get_username %}">{{ post.author.get_full_name }}</a>
</li> </li>
{% endif %}
<li> <li>
{{ post.date_published|date:"M d, Y" }} {{ post.date_published|date:"M d, Y" }}
</li> </li>

View file

@ -145,6 +145,16 @@ class BaseTest(TestCase):
request.errors = StringIO() request.errors = StringIO()
return request return request
def post_request(self, page, lang, data):
request = self.request_factory.post(page.get_path(lang), data)
request.current_page = page
request.user = self.user
request.session = {}
request.cookies = SimpleCookie()
request.errors = StringIO()
request._dont_enforce_csrf_checks = True
return request
def get_page_request(self, page, user, path=None, edit=False, lang_code='en'): def get_page_request(self, page, user, path=None, edit=False, lang_code='en'):
from cms.middleware.toolbar import ToolbarMiddleware from cms.middleware.toolbar import ToolbarMiddleware
path = path or page and page.get_absolute_url() path = path or page and page.get_absolute_url()

View file

@ -2,7 +2,9 @@
from cms.api import add_plugin from cms.api import add_plugin
from cms.utils.copy_plugins import copy_plugins_to from cms.utils.copy_plugins import copy_plugins_to
from cms.utils.plugins import downcast_plugins from cms.utils.plugins import downcast_plugins
from copy import deepcopy
from django.contrib import admin from django.contrib import admin
from django.contrib.messages.middleware import MessageMiddleware
from django.contrib.sites.models import Site from django.contrib.sites.models import Site
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
from django.utils.timezone import now from django.utils.timezone import now
@ -43,6 +45,35 @@ class AdminTest(BaseTest):
fsets = post_admin.get_fieldsets(request) fsets = post_admin.get_fieldsets(request)
self.assertTrue('author' in fsets[1][1]['fields'][0]) self.assertTrue('author' in fsets[1][1]['fields'][0])
def test_admin_auto_author(self):
page1, page2 = self.get_pages()
request = self.get_page_request('/', self.user_staff, r'/en/blog/', edit=False)
data = deepcopy(self.data['en'][0])
data['date_published_0'] = now().strftime('%Y-%m-%d')
data['date_published_1'] = now().strftime('%H:%M:%S')
data['categories'] = self.category_1.pk
request = self.post_request(page1, 'en', data=data)
msg_mid = MessageMiddleware()
msg_mid.process_request(request)
post_admin = admin.site._registry[Post]
post_admin.add_view(request)
self.assertEqual(Post.objects.count(), 1)
self.assertEqual(Post.objects.get(translations__slug='first-post').author_id, 1)
settings.BLOG_AUTHOR_AUTO = False
data = deepcopy(self.data['en'][1])
data['date_published_0'] = now().strftime('%Y-%m-%d')
data['date_published_1'] = now().strftime('%H:%M:%S')
data['categories'] = self.category_1.pk
request = self.post_request(page1, 'en', data=data)
msg_mid = MessageMiddleware()
msg_mid.process_request(request)
post_admin = admin.site._registry[Post]
post_admin.add_view(request)
self.assertEqual(Post.objects.count(), 2)
self.assertEqual(Post.objects.get(translations__slug='second-post').author_id, None)
settings.BLOG_AUTHOR_AUTO = True
class ModelsTest(BaseTest): class ModelsTest(BaseTest):