djangocms_blog/djangocms_blog/admin.py

176 lines
6.0 KiB
Python
Raw Normal View History

2014-01-04 16:07:09 +00:00
# -*- coding: utf-8 -*-
2015-09-12 22:46:05 +00:00
from __future__ import absolute_import, print_function, unicode_literals
2015-07-04 10:48:59 +00:00
from copy import deepcopy
2015-09-19 23:11:14 +00:00
from aldryn_apphooks_config.admin import BaseAppHookConfig, ModelAppHookConfig
2015-07-18 15:33:36 +00:00
from cms.admin.placeholderadmin import FrontendEditableAdminMixin, PlaceholderAdminMixin
2015-07-04 10:48:59 +00:00
from django import forms
from django.conf import settings
2015-07-04 10:48:59 +00:00
from django.contrib import admin
2014-10-11 10:46:39 +00:00
from django.contrib.auth import get_user_model
from parler.admin import TranslatableAdmin
2014-01-04 16:07:09 +00:00
2015-09-19 23:11:14 +00:00
from .cms_appconfig import BlogConfig
from .forms import PostAdminForm
2015-07-18 15:33:36 +00:00
from .models import BlogCategory, Post
2014-10-11 10:23:36 +00:00
from .settings import get_setting
2014-01-04 16:07:09 +00:00
2015-07-19 08:36:52 +00:00
try:
from admin_enhancer.admin import EnhancedModelAdminMixin
except ImportError:
class EnhancedModelAdminMixin(object):
pass
2014-01-04 16:07:09 +00:00
2015-09-19 23:11:14 +00:00
class BlogCategoryAdmin(EnhancedModelAdminMixin, ModelAppHookConfig, TranslatableAdmin):
2014-01-09 08:54:40 +00:00
def get_prepopulated_fields(self, request, obj=None):
2015-09-19 23:11:14 +00:00
app_config_default = self._app_config_select(request, obj)
if app_config_default is None and request.method == 'GET':
return {}
return {'slug': ('name',)}
2014-01-04 16:07:09 +00:00
class Media:
css = {
2015-09-19 23:11:14 +00:00
'all': ('%sdjangocms_blog/css/%s' % (settings.STATIC_URL, 'djangocms_blog_admin.css'),)
}
2014-01-04 16:07:09 +00:00
2015-09-19 23:11:14 +00:00
class PostAdmin(PlaceholderAdminMixin, FrontendEditableAdminMixin,
ModelAppHookConfig, TranslatableAdmin):
form = PostAdminForm
2015-09-30 07:09:28 +00:00
list_display = [
'title', 'author', 'date_published', 'app_config', 'languages', 'date_published_end'
]
list_filter = ('app_config',)
2014-01-04 16:07:09 +00:00
date_hierarchy = 'date_published'
raw_id_fields = ['author']
frontend_editable_fields = ('title', 'abstract', 'post_text')
2014-01-04 16:07:09 +00:00
enhance_exclude = ('main_image', 'tags')
_fieldsets = [
2014-01-04 16:07:09 +00:00
(None, {
2015-09-19 23:11:14 +00:00
'fields': [('title', 'categories', 'publish', 'app_config')]
2014-01-04 16:07:09 +00:00
}),
('Info', {
'fields': (['slug', 'tags'],
('date_published', 'date_published_end', 'enable_comments')),
'classes': ('collapse',)
}),
('Images', {
'fields': (('main_image', 'main_image_thumbnail', 'main_image_full'),),
'classes': ('collapse',)
}),
('SEO', {
2014-11-30 11:55:15 +00:00
'fields': [('meta_description', 'meta_title', 'meta_keywords')],
'classes': ('collapse',)
2014-01-04 16:07:09 +00:00
}),
]
2015-09-19 23:11:14 +00:00
app_config_values = {
'default_published': 'publish'
}
2015-09-30 07:09:28 +00:00
def languages(self, obj):
return ','.join(obj.get_available_languages())
2015-07-04 10:48:59 +00:00
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):
2015-09-19 23:11:14 +00:00
app_config_default = self._app_config_select(request, obj)
if app_config_default is None and request.method == 'GET':
return super(PostAdmin, self).get_fieldsets(request, obj)
if not obj:
config = app_config_default
else:
config = obj.app_config
fsets = deepcopy(self._fieldsets)
2015-09-19 23:11:14 +00:00
if config:
if config.use_abstract:
fsets[0][1]['fields'].append('abstract')
if not config.use_placeholder:
fsets[0][1]['fields'].append('post_text')
else:
if get_setting('USE_ABSTRACT'):
fsets[0][1]['fields'].append('abstract')
if not get_setting('USE_PLACEHOLDER'):
fsets[0][1]['fields'].append('post_text')
2014-10-11 10:23:36 +00:00
if get_setting('MULTISITE'):
2014-10-04 17:22:22 +00:00
fsets[1][1]['fields'][0].append('sites')
if request.user.is_superuser:
fsets[1][1]['fields'][0].append('author')
return fsets
def get_prepopulated_fields(self, request, obj=None):
return {'slug': ('title',)}
2014-01-04 16:07:09 +00:00
def save_model(self, request, obj, form, change):
2015-09-19 23:11:14 +00:00
if not obj.author_id and obj.app_config.set_author:
2014-10-11 10:46:39 +00:00
if get_setting('AUTHOR_DEFAULT') is True:
user = request.user
else:
user = get_user_model().objects.get(username=get_setting('AUTHOR_DEFAULT'))
obj.author = user
2014-01-04 16:07:09 +00:00
super(PostAdmin, self).save_model(request, obj, form, change)
class Media:
css = {
2015-09-19 23:11:14 +00:00
'all': ('%sdjangocms_blog/css/%s' % (settings.STATIC_URL, 'djangocms_blog_admin.css'),)
}
2015-09-19 23:11:14 +00:00
class BlogConfigAdmin(BaseAppHookConfig, TranslatableAdmin):
@property
def declared_fieldsets(self):
return [
(None, {
'fields': ('type', 'namespace',)
}),
('Generic', {
'fields': (
'config.default_published', 'config.use_placeholder', 'config.use_abstract',
'config.set_author',
)
}),
('Layout', {
'fields': (
'config.paginate_by', 'config.url_patterns', 'config.template_prefix',
),
'classes': ('collapse',)
}),
('Meta', {
'fields': (
'config.object_type',
)
}),
('Open Graph', {
'fields': (
'config.og_type', 'config.og_app_id', 'config.og_profile_id',
'config.og_publisher', 'config.og_author_url',
)
}),
('Twitter', {
'fields': (
'config.twitter_type', 'config.twitter_site', 'config.twitter_author',
)
}),
('Google+', {
'fields': (
'config.gplus_type', 'config.gplus_author',
)
}),
]
2015-09-19 23:11:14 +00:00
2014-01-04 16:07:09 +00:00
admin.site.register(BlogCategory, BlogCategoryAdmin)
admin.site.register(Post, PostAdmin)
2015-09-19 23:11:14 +00:00
admin.site.register(BlogConfig, BlogConfigAdmin)