From a2bd384d2b62e660cb39e16df0c30624510d242d Mon Sep 17 00:00:00 2001 From: Iacopo Spalletti Date: Sat, 19 Apr 2014 19:18:11 +0200 Subject: [PATCH] Add support for social tags via django-meta-mixin --- djangocms_blog/__init__.py | 2 +- djangocms_blog/admin.py | 26 ++++--- djangocms_blog/models.py | 67 ++++++++++++++++--- djangocms_blog/settings.py | 14 +++- .../templates/djangocms_blog/base.html | 8 ++- djangocms_blog/views.py | 3 +- 6 files changed, 96 insertions(+), 24 deletions(-) diff --git a/djangocms_blog/__init__.py b/djangocms_blog/__init__.py index 7d8a0d8..e8f0993 100644 --- a/djangocms_blog/__init__.py +++ b/djangocms_blog/__init__.py @@ -1 +1 @@ -__version__ = '0.2b1.dev1' +__version__ = '0.2b2' diff --git a/djangocms_blog/admin.py b/djangocms_blog/admin.py index d79d2c7..c7a573e 100755 --- a/djangocms_blog/admin.py +++ b/djangocms_blog/admin.py @@ -16,7 +16,7 @@ class BlogCategoryAdmin(EnhancedModelAdminMixin, TranslatableAdmin): class Media: css = { 'all': ('%sdjangocms_blog/css/%s' % (settings.STATIC_URL, - "djangocms_blog_admin.css"),) + "djangocms_blog_admin.css"),) } @@ -27,23 +27,29 @@ class PostAdmin(EnhancedModelAdminMixin, FrontendEditableAdmin, raw_id_fields = ['author'] frontend_editable_fields = ("title", "abstract") enhance_exclude = ('main_image', 'tags') - _fieldsets = [ (None, { - 'fields': [('title', 'slug', 'publish'), - ('categories', 'tags'), - ('date_published', 'date_published_end')] + 'fields': [('title', 'slug', 'publish'), 'abstract'] }), - (None, { - 'fields': [('main_image', 'main_image_thumbnail', 'main_image_full'), - 'abstract', ('meta_description', 'meta_keywords')] + ('Info', { + 'fields': (['categories', 'tags'], + ('date_published', 'date_published_end')), + 'classes': ('collapse',) + }), + ('Immagine', { + 'fields': (('main_image', 'main_image_thumbnail', 'main_image_full'),), + 'classes': ('collapse',) + }), + ('SEO', { + 'fields': [('meta_description', 'meta_keywords')], + 'classes': ('collapse',) }), ] def get_fieldsets(self, request, obj=None): if request.user.is_superuser: fsets = deepcopy(self._fieldsets) - fsets[0][1]['fields'].append('author') + fsets[1][1]['fields'][0].append('author') return fsets else: return self._fieldsets @@ -59,7 +65,7 @@ class PostAdmin(EnhancedModelAdminMixin, FrontendEditableAdmin, class Media: css = { 'all': ('%sdjangocms_blog/css/%s' % (settings.STATIC_URL, - "djangocms_blog_admin.css"),) + "djangocms_blog_admin.css"),) } diff --git a/djangocms_blog/models.py b/djangocms_blog/models.py index 3e82c9a..8f25bef 100644 --- a/djangocms_blog/models.py +++ b/djangocms_blog/models.py @@ -15,6 +15,7 @@ from parler.managers import TranslationManager from taggit_autosuggest.managers import TaggableManager from . import settings +from meta_mixin.models import ModelMeta from .managers import GenericDateTaggedManager BLOG_CURRENT_POST_IDENTIFIER = 'djangocms_post_current' @@ -57,7 +58,7 @@ class BlogCategory(TranslatableModel): self.save_translations() -class Post(TranslatableModel): +class Post(ModelMeta, TranslatableModel): """ Blog post """ @@ -81,13 +82,13 @@ class Post(TranslatableModel): blank=True, null=True) main_image_full = models.ForeignKey(ThumbnailOption, verbose_name=_('Main image full'), - related_name='blog_post_full', + related_name='djangocms_blog_post_full', blank=True, null=True) translations = TranslatedFields( title=models.CharField(_('Title'), max_length=255), slug=models.SlugField(_('slug'), blank=True, db_index=True), - abstract=HTMLField(_('Text')), + abstract=HTMLField(_('Abstract')), meta_description=models.TextField(verbose_name=_(u'Post meta description'), blank=True, default=''), meta_keywords=models.TextField(verbose_name=_(u'Post meta keywords'), @@ -99,6 +100,52 @@ class Post(TranslatableModel): objects = GenericDateTaggedManager() tags = TaggableManager(blank=True, related_name='djangocms_blog_tags') + _metadata = { + 'title': 'title', + 'description': 'get_description', + 'og_description': 'get_description', + 'twitter_description': 'get_description', + 'gplus_description': 'get_description', + 'keywords': 'get_keywords', + 'locale': None, + 'image': 'get_image_url', + 'object_type': settings.BLOG_TYPE, + 'og_type': settings.BLOG_FB_TYPE, + 'og_app_id': settings.BLOG_FB_APPID, + 'og_profile_id': settings.BLOG_FB_PROFILE_ID, + 'og_publisher': settings.BLOG_FB_PUBLISHER, + 'og_author_url': settings.BLOG_FB_AUTHOR_URL, + 'twitter_type': settings.BLOG_TWITTER_TYPE, + 'twitter_site': settings.BLOG_TWITTER_SITE, + 'twitter_author': settings.BLOG_TWITTER_AUTHOR, + 'gplus_type': settings.BLOG_GPLUS_TYPE, + 'gplus_author': settings.BLOG_GPLUS_AUTHOR, + 'published_time': 'date_published', + 'modified_time': 'date_modified', + 'expiration_time': 'date_published_end', + 'tag': 'get_tags', + 'url': 'get_absolute_url', + } + + def get_keywords(self): + return self.safe_translation_getter('meta_keywords', language_code=get_language()).strip().split(",") + + def get_description(self): + description = self.safe_translation_getter('meta_description', language_code=get_language()) + if not description: + description = self.safe_translation_getter('abstract', language_code=get_language()) + return description.strip() + + def get_image_url(self): + return self.main_image + + def get_tags(self): + taglist = [tag.name for tag in self.tags.all()] + return ",".join(taglist) + + def get_author(self): + return self.author + class Meta: verbose_name = _('blog article') verbose_name_plural = _('blog articles') @@ -128,13 +175,6 @@ class Post(TranslatableModel): return self.main_image_thumbnail.as_dict else: return settings.BLOG_IMAGE_THUMBNAIL_SIZE - - def get_full_url(self): - s = Site.objects.get_current() - if s.domain.find('http') > -1: - return "%s%s" % (s.domain, self.get_absolute_url()) - else: - return "http://%s%s" % (s.domain, self.get_absolute_url()) def full_image_options(self): if self.main_image_fulll_id: @@ -142,6 +182,13 @@ class Post(TranslatableModel): else: return settings.BLOG_IMAGE_FULL_SIZE + def get_full_url(self): + s = Site.objects.get_current() + if s.domain.find('http') > -1: + return "%s%s" % (s.domain, self.get_absolute_url()) + else: + return "http://%s%s" % (s.domain, self.get_absolute_url()) + class LatestPostsPlugin(CMSPlugin): diff --git a/djangocms_blog/settings.py b/djangocms_blog/settings.py index 235e8b3..add0c28 100644 --- a/djangocms_blog/settings.py +++ b/djangocms_blog/settings.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- - from django.conf import settings +from meta_mixin import settings as meta_settings BLOG_IMAGE_THUMBNAIL_SIZE = getattr(settings, 'BLOG_IMAGE_THUMBNAIL_SIZE', { 'size': '120x120', @@ -19,3 +19,15 @@ BLOG_TAGCLOUD_MAX = getattr(settings, 'BLOG_TAGCLOUD_MAX', 10) BLOG_PAGINATION = getattr(settings, 'BLOG_PAGINATION', 10) BLOG_LATEST_POSTS = getattr(settings, 'BLOG_LATEST_POSTS', 5) BLOG_POSTS_LIST_TRUNCWORDS_COUNT = getattr(settings, 'BLOG_POSTS_LIST_TRUNCWORDS_COUNT', 100) +BLOG_TYPE = getattr(settings, 'BLOG_TYPE', 'Article') +BLOG_FB_TYPE = getattr(settings, 'BLOG_FB_TYPE', 'Article') +BLOG_FB_APPID = getattr(settings, 'BLOG_FB_APPID', meta_settings.FB_APPID) +BLOG_FB_PROFILE_ID = getattr(settings, 'BLOG_FB_PROFILE_ID', meta_settings.FB_PROFILE_ID) +BLOG_FB_PUBLISHER = getattr(settings, 'BLOG_FB_PUBLISHER', meta_settings.FB_PUBLISHER) +BLOG_FB_AUTHOR_URL = getattr(settings, 'BLOG_FB_AUTHOR_URL', 'get_author_url') +BLOG_FB_AUTHOR = getattr(settings, 'BLOG_FB_AUTHOR', 'get_author_name') +BLOG_TWITTER_TYPE = getattr(settings, 'BLOG_TWITTER_TYPE', 'Summary') +BLOG_TWITTER_SITE = getattr(settings, 'BLOG_TWITTER_SITE', meta_settings.TWITTER_SITE) +BLOG_TWITTER_AUTHOR = getattr(settings, 'BLOG_TWITTER_AUTHOR', 'get_author_twitter') +BLOG_GPLUS_TYPE = getattr(settings, 'BLOG_GPLUS_SCOPE_CATEGORY', 'Article') +BLOG_GPLUS_AUTHOR = getattr(settings, 'BLOG_GPLUS_AUTHOR', 'get_author_gplus') diff --git a/djangocms_blog/templates/djangocms_blog/base.html b/djangocms_blog/templates/djangocms_blog/base.html index 6a78e80..233092d 100644 --- a/djangocms_blog/templates/djangocms_blog/base.html +++ b/djangocms_blog/templates/djangocms_blog/base.html @@ -1,7 +1,13 @@ {% extends "base.html" %} +{% block meta %} + {% if meta %} + {% include "meta_mixin/meta.html" %} + {% endif %} +{% endblock meta %} + {% block content %}
{% block content_blog %}{% endblock %}
-{% endblock %} \ No newline at end of file +{% endblock content %} \ No newline at end of file diff --git a/djangocms_blog/views.py b/djangocms_blog/views.py index 8a30619..a415abd 100644 --- a/djangocms_blog/views.py +++ b/djangocms_blog/views.py @@ -5,7 +5,6 @@ from django.contrib.auth.models import User from django.core.urlresolvers import resolve from django.http import Http404 from django.views.generic import ListView, DetailView -from parler.utils import get_active_language_choices from parler.views import ViewUrlMixin @@ -58,9 +57,11 @@ class PostDetailView(BaseBlogView, DetailView): def get_context_data(self, **kwargs): context = super(PostDetailView, self).get_context_data(**kwargs) + context['meta'] = self.get_object().as_meta() setattr(self.request, BLOG_CURRENT_POST_IDENTIFIER, self.get_object()) return context + class PostArchiveView(BaseBlogView, ListView): model = Post context_object_name = 'post_list'