Skip to content

Commit

Permalink
Merge 7c9cb45 into 40cfc4c
Browse files Browse the repository at this point in the history
  • Loading branch information
rodfersou committed Aug 25, 2017
2 parents 40cfc4c + 7c9cb45 commit 2efeeb2
Show file tree
Hide file tree
Showing 27 changed files with 219 additions and 344 deletions.
4 changes: 4 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ There's a frood who really knows where his towel is.
2.10.2 (unreleased)
^^^^^^^^^^^^^^^^^^^

- Move all metadata to the viewlet template. After this change the metadata is always shown.
Also remove some unused metadata, since the provider use Open Graph as fallback. (closes `#112 <https://github.com/collective/sc.social.like/issues/112>`_).
[rodfersou]

- Remove useless scale caching on the request as it seems to be causing colateral issues (closes `#109 <https://github.com/collective/sc.social.like/issues/109>`_).
[rodfersou]

Expand Down
19 changes: 16 additions & 3 deletions sc/social/like/browser/templates/metadata.pt
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
<tal:metas i18n:domain="sc.social.like" tal:condition="view/enabled">
<tal:plugin tal:repeat="plugin view/plugins">
<tal:replace replace="structure plugin/html" />
</tal:plugin>
<meta name="twitter:card" content="summary_large_image" />
<meta name="twitter:site" tal:attributes="content string:@${view/via}" />
<meta property="og:site_name" tal:attributes="content view/portal_title" />
<meta property="og:url" tal:attributes="content view/canonical_url" />
<meta property="og:type" tal:attributes="content view/type" />
<meta property="og:locale" tal:attributes="content view/language" />
<meta property="og:title" tal:attributes="content view/title" />
<meta property="og:description" tal:attributes="content view/description" />
<meta property="og:image" tal:attributes="content view/image_url" />
<tal:image tal:condition="nocall:view/image">
<meta property="og:image:height" tal:attributes="content view/image_height" />
<meta property="og:image:width" tal:attributes="content view/image_width" />
<meta property="og:image:type" tal:attributes="content view/image_type" />
</tal:image>
<meta tal:condition="view/admins" property="fb:admins" tal:attributes="content view/admins" />
<meta tal:condition="view/app_id" property="fb:app_id" tal:attributes="content view/app_id" />
</tal:metas>
89 changes: 88 additions & 1 deletion sc/social/like/browser/viewlets.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
# -*- coding: utf-8 -*-
from Acquisition import aq_inner
from Acquisition import aq_parent
from plone import api
from plone.api.exc import InvalidParameterError
from plone.app.layout.viewlets import ViewletBase
from plone.memoize.view import memoize
from Products.CMFCore.interfaces import ISiteRoot
from Products.CMFCore.WorkflowCore import WorkflowException
from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile
from sc.social.like.behaviors import ISocialMedia
from sc.social.like.interfaces import ISocialLikeSettings
from sc.social.like.plugins.facebook.utils import facebook_language
from sc.social.like.utils import get_content_image
from sc.social.like.utils import get_language
from zope.component import getMultiAdapter


Expand Down Expand Up @@ -60,8 +67,19 @@ def update(self):
class SocialMetadataViewlet(BaseLikeViewlet):
"""Viewlet used to insert metadata into page header
"""
language = 'en_US'

render = ViewPageTemplateFile('templates/metadata.pt')
render_method = 'metadata'

def update(self):
self.title = self.context.title
self.description = self.context.Description()
portal = api.portal.get()
self.site_url = portal.absolute_url()
self.url = self.context.absolute_url()
self.portal_title = portal.title
self.language = facebook_language(get_language(self.context), self.language)
self.image = get_content_image(self.context)

def enabled(self):
"""Validates if the viewlet should be enabled for this context
Expand All @@ -74,6 +92,75 @@ def enabled(self):
return True
return self.helper.enabled(self.view)

@property
def via(self):
record = ISocialLikeSettings.__identifier__ + '.twitter_username'
return api.portal.get_registry_record(record, default='')

@property
def canonical_url(self):
if not ISocialMedia.providedBy(self.context):
# use current URL if the object don't provide the behavior
return self.url
return self.context.canonical_url

def image_height(self):
""" Return height to image
"""
if not self.image:
return
return self.image.height

def image_type(self):
""" Return content type to image
"""
if not self.image:
return
type = getattr(self.image, 'content_type', None)
if type is not None:
return type
return getattr(self.image, 'mimetype', 'image/jpeg')

def image_width(self):
""" Return width to image
"""
if not self.image:
return
return self.image.width

def image_url(self):
""" Return url to image
"""
if not self.image:
return self.site_url + '/logo.png'
return self.image.url

@property
def app_id(self):
record = ISocialLikeSettings.__identifier__ + '.facebook_app_id'
return api.portal.get_registry_record(record, default='')

@property
def admins(self):
record = ISocialLikeSettings.__identifier__ + '.facebook_username'
return api.portal.get_registry_record(record, default='')

def _isPortalDefaultView(self):
if not ISiteRoot.providedBy(aq_parent(aq_inner(self.context))):
return False
putils = api.portal.get_tool('plone_utils')
return putils.isDefaultPage(self.context)

def _isPortal(self):
if ISiteRoot.providedBy(aq_inner(self.context)):
return True
return self._isPortalDefaultView()

def type(self):
if self._isPortal():
return 'website'
return 'article'


class SocialLikesViewlet(BaseLikeViewlet):
"""Viewlet used to display the buttons
Expand Down
4 changes: 0 additions & 4 deletions sc/social/like/plugins/email/browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@

class PluginView(BrowserView):

typebutton = ''
language = 'en'

metadata = None
plugin = link = ViewPageTemplateFile('plugin.pt')

@property
Expand Down
72 changes: 0 additions & 72 deletions sc/social/like/plugins/facebook/browser.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
# -*- coding:utf-8 -*-
from Acquisition import aq_inner
from Acquisition import aq_parent
from plone import api
from plone.api.exc import InvalidParameterError
from Products.CMFCore.interfaces import ISiteRoot
from Products.CMFCore.utils import getToolByName
from Products.Five import BrowserView
from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile
from sc.social.like.behaviors import ISocialMedia
from sc.social.like.config import IS_PLONE_5
from sc.social.like.interfaces import ISocialLikeSettings
from sc.social.like.plugins.facebook.utils import facebook_language
from sc.social.like.utils import get_content_image
from sc.social.like.utils import get_language
from urllib import urlencode
from zope.component import getMultiAdapter
Expand All @@ -23,10 +17,8 @@

class PluginView(BrowserView):

fb_enabled = False
language = 'en_US'

metadata = ViewPageTemplateFile('templates/metadata.pt')
plugin = ViewPageTemplateFile('templates/plugin.pt')
link = ViewPageTemplateFile('templates/link.pt')

Expand All @@ -40,12 +32,9 @@ def __init__(self, context, request):
self.description = context.Description()
self.portal_state = getMultiAdapter((self.context, self.request),
name=u'plone_portal_state')
self.portal = self.portal_state.portal()
self.site_url = self.portal_state.portal_url()
self.portal_title = self.portal_state.portal_title()
self.url = context.absolute_url()
self.language = facebook_language(get_language(context), self.language)
self.image = get_content_image(context, width=1200, height=630)
self.typebutton # XXX: needed to initialize self.width

@property
Expand All @@ -56,10 +45,6 @@ def canonical_url(self):
# use current URL if the object don't provide the behavior
return self.url

@property
def is_plone_5(self):
return IS_PLONE_5

def fbjs(self):
js_source = """
(function() {{
Expand All @@ -72,37 +57,6 @@ def fbjs(self):
""".format(self.language)
return js_source

def image_height(self):
""" Return height to image
"""
img = self.image
if img:
return img.height

def image_type(self):
""" Return content type to image
"""
img = self.image
if img:
return getattr(img, 'content_type',
getattr(img, 'mimetype', 'image/jpeg'))

def image_width(self):
""" Return width to image
"""
img = self.image
if img:
return img.width

def image_url(self):
""" Return url to image
"""
img = self.image
if img:
return img.url
else:
return '{0}/logo.png'.format(self.site_url)

@property
def typebutton(self):
typerecord = ISocialLikeSettings.__identifier__ + '.typebutton'
Expand Down Expand Up @@ -143,14 +97,6 @@ def app_id(self):
except InvalidParameterError:
return ''

@property
def admins(self):
record = ISocialLikeSettings.__identifier__ + '.facebook_username'
try:
return api.portal.get_registry_record(record)
except InvalidParameterError:
return ''

@property
def fbshow_like(self):
record = ISocialLikeSettings.__identifier__ + '.fbbuttons'
Expand All @@ -167,24 +113,6 @@ def fbshow_share(self):
except InvalidParameterError:
return False

def _isPortalDefaultView(self):
context = self.context
if ISiteRoot.providedBy(aq_parent(aq_inner(context))):
putils = getToolByName(context, 'plone_utils')
return putils.isDefaultPage(context)
return False

def _isPortal(self):
context = self.context
if ISiteRoot.providedBy(aq_inner(context)):
return True
return self._isPortalDefaultView()

def type(self):
if self._isPortal():
return 'website'
return 'article'

def share_link(self):
params = dict(
app_id=self.app_id,
Expand Down
20 changes: 0 additions & 20 deletions sc/social/like/plugins/facebook/templates/metadata.pt

This file was deleted.

31 changes: 15 additions & 16 deletions sc/social/like/plugins/facebook/templates/plugin.pt
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
<tal:fb>
<!-- Facebook -->
<div class="fb-like"
tal:condition="view/fbshow_like"
tal:attributes="data-href view/canonical_url;
data-send string:false;
data-action view/fbaction;
data-layout view/typebutton;
data-width view/width;
data-show-faces string:false;
data-share view/fbshow_share"></div>
<div class="fb-share-button"
tal:condition="python: view.fbshow_share and not view.fbshow_like"
tal:attributes="data-layout view/typebutton;
data-href view/canonical_url;"></div>
</tal:fb>
<!-- Facebook -->
<script tal:content="view/fbjs" />
<div class="fb-like"
tal:condition="view/fbshow_like"
tal:attributes="data-href view/canonical_url;
data-send string:false;
data-action view/fbaction;
data-layout view/typebutton;
data-width view/width;
data-show-faces string:false;
data-share view/fbshow_share"></div>
<div class="fb-share-button"
tal:condition="python: view.fbshow_share and not view.fbshow_like"
tal:attributes="data-layout view/typebutton;
data-href view/canonical_url;"></div>
2 changes: 0 additions & 2 deletions sc/social/like/plugins/gplus/browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@

class PluginView(BrowserView):

gp_enabled = True
language = 'en'

metadata = ViewPageTemplateFile('templates/metadata.pt')
plugin = ViewPageTemplateFile('templates/plugin.pt')
link = ViewPageTemplateFile('templates/link.pt')

Expand Down
12 changes: 0 additions & 12 deletions sc/social/like/plugins/gplus/templates/metadata.pt

This file was deleted.

25 changes: 17 additions & 8 deletions sc/social/like/plugins/gplus/templates/plugin.pt
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
<tal:gp>
<!-- Google+ -->
<div class="g-plusone"
tal:attributes="data-annotation string:bubble;
data-size view/typebutton;
data-href view/url;
data-lang view/language;"></div>
</tal:gp>
<!-- Google+ -->
<script>
(function() {
var po = document.createElement('script');
po.type = 'application/javascript';
po.async = true;
po.src = '//apis.google.com/js/plusone.js';
var head = document.getElementsByTagName('head')[0];
head.appendChild(po);
})();
</script>

<div class="g-plusone"
tal:attributes="data-annotation string:bubble;
data-size view/typebutton;
data-href view/url;
data-lang view/language;"></div>
Loading

0 comments on commit 2efeeb2

Please sign in to comment.