Skip to content
This repository has been archived by the owner on Oct 29, 2019. It is now read-only.

Optional Meta Page Support #398

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@
{% block title %}{{ article.title }} - {{ block.super }}{% endblock %}
{% block breadcrumb %}{% endblock %}

{% block html_tag %} itemscope itemtype="http://schema.org/Blog" {% endblock %}

{% block meta_tags %}
<meta name="viewport" content="width=device-width,initial-scale=1">
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

meta_tags is very generic here, that can break your frontend. Should be something like {% blog_meta_tags %} or so. Also the viewport meta tag should be handled by the project frontend, not the blog /cc @vxsx

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To elaborate further: https://github.com/aldryn/aldryn-newsblog/blob/master/aldryn_newsblog/boilerplates/bootstrap3/templates/aldryn_newsblog/base.html#L3 is the standard template. If you want meta integration you just adapt the base.html template to look like:

{% block meta_tags %}
    {{ block.super }}
    {% block newsblog_meta_tags %}{% endblock %}
{% endblock %}
{% block content %}{% endblock content %}

and the article_detail.html just provides the facilities to overwrite it:

{% block newsblog_meta_tags %}
    {% if meta %}
        {% include "aldryn_newsblog/includes/meta.html" with meta=meta %}
    {% endif %}
{% endif %}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this solution :D If you want I can add it to the pull request.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

aye 👍

{% if meta %}{% include 'aldryn_newsblog/includes/meta.html' with meta=meta %}{% endif %}
{% endblock %}

{% block newsblog_content %}
<div class="aldryn-newsblog-detail">
{% include "aldryn_newsblog/includes/article.html" with detail_view="true" %}
Expand Down
61 changes: 60 additions & 1 deletion aldryn_newsblog/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
from django.db import connection, models
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.template.defaultfilters import truncatewords, striptags

try:
from django.utils.encoding import force_unicode
except ImportError:
Expand Down Expand Up @@ -52,6 +54,13 @@
raise ImproperlyConfigured(
'Neither LANGUAGES nor LANGUAGE was found in settings.')

if 'meta' in settings.INSTALLED_APPS:
from meta.models import ModelMeta
else:
class ModelMeta(object):
def as_meta(self):
return {}


# At startup time, SQL_NOW_FUNC will contain the database-appropriate SQL to
# obtain the CURRENT_TIMESTAMP.
Expand All @@ -70,7 +79,8 @@
@version_controlled_content(follow=['app_config'])
class Article(TranslatedAutoSlugifyMixin,
TranslationHelperMixin,
TranslatableModel):
TranslatableModel,
ModelMeta):

# TranslatedAutoSlugifyMixin options
slug_source_field_name = 'title'
Expand Down Expand Up @@ -141,6 +151,34 @@ class Article(TranslatedAutoSlugifyMixin,

objects = RelatedManager()

_metadata = {
'title': 'get_title',
'description': 'get_description',
'str_keywords': 'get_keywords',
'og_description': 'get_description',
'twitter_description': 'get_description',
'gplus_description': 'get_description',
'locale': 'get_locale',
'image': 'get_image_full_url',
# 'object_type': 'get_meta_attribute',
# 'og_type': 'get_meta_attribute',
# 'og_app_id': 'get_meta_attribute',
# 'og_profile_id': 'get_meta_attribute',
# 'og_publisher': 'get_meta_attribute',
# 'og_author_url': 'get_meta_attribute',
# 'og_author': 'get_meta_attribute',
# 'twitter_type': 'get_meta_attribute',
# 'twitter_site': 'get_meta_attribute',
# 'twitter_author': 'get_meta_attribute',
# 'gplus_type': 'get_meta_attribute',
# 'gplus_author': 'get_meta_attribute',
'published_time': 'publishing_date',
# 'modified_time': 'date_modified',
# 'expiration_time': 'date_published_end',
'tag': 'get_tags',
'url': 'get_absolute_url_meta',
}

class Meta:
ordering = ['-publishing_date']

Expand All @@ -160,6 +198,9 @@ def future(self):
"""
return (self.is_published and self.publishing_date > now())

def get_tags(self):
return ','.join(self.tags.names().values_list('name', flat=True))

def get_absolute_url(self, language=None):
"""Returns the url for this Article in the selected permalink format."""
if not language:
Expand Down Expand Up @@ -191,6 +232,9 @@ def get_absolute_url(self, language=None):
with override(language):
return reverse('{0}article-detail'.format(namespace), kwargs=kwargs)

def get_absolute_url_meta(self, meta_req=None):
return self.get_absolute_url()

def get_search_data(self, language=None, request=None):
"""
Provides an index for use with Haystack, or, for populating
Expand All @@ -217,6 +261,21 @@ def get_search_data(self, language=None, request=None):
text_bits.append(plugin_text_content)
return ' '.join(text_bits)

def get_locale(self):
return get_current_language()

def get_title(self):
return self.title

def get_description(self):
return self.meta_description or striptags(truncatewords(self.lead_in, 50))

def get_keywords(self):
return (self.meta_keywords if self.meta_keywords else self.get_tags())

def get_image_full_url(self):
return self.build_absolute_uri(self.featured_image.url) if self.featured_image else None

def save(self, *args, **kwargs):
# Update the search index
if self.update_search_on_save:
Expand Down
7 changes: 7 additions & 0 deletions aldryn_newsblog/templates/aldryn_newsblog/article_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@
{{ article.title }} - {{ block.super }}
{% endblock %}

{% block html_tag %} itemscope itemtype="http://schema.org/Blog" {% endblock %}

{% block meta_tags %}
<meta name="viewport" content="width=device-width,initial-scale=1">
{% if meta %}{% include 'aldryn_newsblog/includes/meta.html' with meta=meta %}{% endif %}
{% endblock %}

{% block newsblog_content %}
{% include "aldryn_newsblog/includes/article.html" with detail_view="true" %}

Expand Down
51 changes: 51 additions & 0 deletions aldryn_newsblog/templates/aldryn_newsblog/includes/meta.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{% load meta_extra %}
{% if meta %}
{% autoescape off %}
{% if meta.description %}{% meta 'description' meta.description %}{% endif %}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can't this be automatically generated?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used this template:
https://github.com/nephila/djangocms-page-meta/blob/develop/djangocms_page_meta/templates/djangocms_page_meta/meta.html

Perhaps @nephila also want to review how to improve it.

PS: Thanks to @nephila for his work!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As this requires djangocms-page-meta @Nekmo you could simply do:

{% include "djangocms_page_meta/meta.html" %}

instead

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I know, but I've made small changes and thought it would be necessary to make further changes in the future.

Moreover, this creates a dependency with DjangoCMS-Page-Meta for something for which it was not intended.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Though we should find then a smarter solution by auto generating it. We need to keep it DRY

{% if meta.str_keywords %}{% meta 'keywords' meta.str_keywords %}{% endif %}
{% if meta.use_og %}
{% if meta.title %}{% og_prop 'title' meta.title %}{% endif %}
{% if meta.url %}{% og_prop 'url' meta.url %}{% endif %}
{% if meta.og_description %}{% og_prop 'description' meta.og_description %}{% endif %}
{% if meta.image %}{% og_prop 'image' meta.image %}{% endif %}
{% if meta.og_type %}{% og_prop 'type' meta.og_type %}{% endif %}
{% if meta.site_name %}{% og_prop 'site_name' meta.site_name %}{% endif %}
{% if meta.og_author_url %}
{% generic_prop 'article' 'author' meta.og_author_url %}
{% elif meta.og_author %}
{% generic_prop 'article' 'author:first_name' meta.og_author.first_name %}
{% generic_prop 'article' 'author:last_name' meta.og_author.last_name %}
{% endif %}
{% if meta.published_time %}{% generic_prop 'article' 'published_time' meta.published_time %}{% endif %}
{% if meta.modified_time %}{% generic_prop 'article' 'modified_time' meta.modified_time %}{% endif %}
{% if meta.expiration_time %}{% generic_prop 'article' 'expiration_time' meta.expiration_time %}{% endif %}
{% if meta.og_publisher %}{% generic_prop 'article' 'publisher' meta.og_publisher %}{% endif %}
{% if meta.og_app_id %}{% generic_prop 'fb' 'app_id' meta.og_app_id %}{% endif %}
{% if meta.og_profile_id %}{% generic_prop 'fb' 'profile_id' meta.og_profile_id %}{% endif %}
{% if meta.fb_pages %}{% generic_prop 'fb' 'pages' meta.fb_pages %}{% endif %}
{% if meta.tag %}{% generic_prop 'article' 'tag' meta.tag %}{% endif %}
{% if meta.locale %}{% og_prop 'locale' meta.locale %}{% endif %}
{% endif %}
{% if meta.use_twitter %}
{% twitter_prop 'domain' meta.get_domain %}
{% if meta.twitter_type %}{% twitter_prop 'card' meta.twitter_type %}{% endif %}
{% if meta.title %}{% twitter_prop 'title' meta.title %}{% endif %}
{% if meta.url %}{% twitter_prop 'url' meta.url %}{% endif %}
{% if meta.twitter_description %}{% twitter_prop 'description' meta.twitter_description %}{% endif %}
{% if meta.image %}{% twitter_prop 'image:src' meta.image %}{% endif %}
{% if meta.twitter_author %}{% twitter_prop 'creator' meta.twitter_author %}{% endif %}
{% if meta.twitter_site %}{% twitter_prop 'site_name' meta.twitter_site %}{% endif %}
{% endif %}
{% if meta.use_googleplus %}
{% if meta.gplus_author %}
<link rel="author" href="{{ meta.gplus_author }}"/>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove slash at />

{% endif %}
{% if meta.title %}{% googleplus_prop 'name' meta.title %}{% endif %}
{% if meta.published_time %}{% googleplus_prop 'datePublished' meta.published_time %}{% endif %}
{% if meta.modified_time %}{% googleplus_prop 'dateModified' meta.modified_time %}{% endif %}
{% if meta.url %}{% googleplus_prop 'url' meta.url %}{% endif %}
{% if meta.gplus_description %}{% googleplus_prop 'description' meta.gplus_description %}{% endif %}
{% if meta.image %}{% googleplus_prop 'image' meta.image %}{% endif %}
{% endif %}
{% endautoescape %}
{% endif %}
1 change: 1 addition & 0 deletions aldryn_newsblog/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ def get_context_data(self, **kwargs):
self.queryset, self.object)
context['next_article'] = self.get_next_object(
self.queryset, self.object)
context['meta'] = self.get_object().as_meta()
return context

def get_prev_object(self, queryset=None, object=None):
Expand Down