Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
hvelarde committed Jan 5, 2017
1 parent 5cac285 commit 595cc9f
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 33 deletions.
2 changes: 1 addition & 1 deletion CHANGES.rst
Expand Up @@ -12,7 +12,7 @@ There's a frood who really knows where his towel is.
2.6b1 (2016-12-21)
^^^^^^^^^^^^^^^^^^

- Add Twitter Cards metadata (closes `#65`_).
- Add metadata for Twitter Cards (closes `#65`_).
[rodfersou]

- Code clean up; tests related with loading BMP images were removed as make no sense.
Expand Down
7 changes: 4 additions & 3 deletions sc/social/like/plugins/facebook/browser.py
Expand Up @@ -9,6 +9,7 @@
from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile
from sc.social.like.interfaces import ISocialLikeSettings
from sc.social.like.plugins.facebook.utils import facebook_language
from sc.social.like.testing import IS_PLONE_5 # TODO: move to config.py
from sc.social.like.utils import get_content_image
from sc.social.like.utils import get_language
from urllib import urlencode
Expand Down Expand Up @@ -46,9 +47,9 @@ def __init__(self, context, request):
self.image = get_content_image(context, width=1200, height=630)
self.typebutton # XXX: needed to initialize self.width

def metadata_enabled(self):
"""Disable metadata on Plone 5"""
return not api.env.plone_version().startswith('5')
@property
def is_plone5(self):
return IS_PLONE_5

def fbjs(self):
js_source = """
Expand Down
5 changes: 4 additions & 1 deletion sc/social/like/plugins/facebook/templates/metadata.pt
@@ -1,4 +1,7 @@
<tal:fb condition="view/metadata_enabled">
<tal:comment replace="nothing">
Plone 5 already includes metadata for Facebook and Twitter.
</tal:comment>
<tal:fb condition="not:view/is_plone5">
<meta property="og:site_name" tal:attributes="content view/portal_title" />
<meta property="og:url" tal:attributes="content view/url" />
<meta property="og:type" tal:attributes="content view/type" />
Expand Down
14 changes: 6 additions & 8 deletions sc/social/like/plugins/twitter/browser.py
Expand Up @@ -6,6 +6,7 @@
from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile
from Products.PythonScripts.standard import url_quote
from sc.social.like.interfaces import ISocialLikeSettings
from sc.social.like.testing import IS_PLONE_5 # TODO: move to config.py
from sc.social.like.utils import get_content_image
from sc.social.like.utils import get_language
from urllib import urlencode
Expand Down Expand Up @@ -46,9 +47,9 @@ def __init__(self, context, request):
)
)

def metadata_enabled(self):
"""Disable metadata on Plone 5"""
return not api.env.plone_version().startswith('5')
@property
def is_plone5(self):
return IS_PLONE_5

@property
def typebutton(self):
Expand Down Expand Up @@ -78,8 +79,5 @@ def share_link(self):
return url

def image_url(self):
""" Return url to image
"""
img = self.image
if img:
return img.url
"""Return image URL."""
return self.image.url if self.image else None
7 changes: 5 additions & 2 deletions sc/social/like/plugins/twitter/templates/metadata.pt
@@ -1,6 +1,9 @@
<tal:twitter condition="view/metadata_enabled">
<tal:comment replace="nothing">
Plone 5 already includes metadata for Facebook and Twitter.
</tal:comment>
<tal:twitter condition="not:view/is_plone5">
<meta name="twitter:card" content="summary_large_image" />
<meta name="twitter:image" tal:attributes="content view/image_url" tal:condition="view/image_url" />
<meta name="twitter:image" tal:condition="view/image_url" tal:attributes="content view/image_url" />
<meta name="twitter:site" tal:attributes="content string:@${view/via}" />
<meta name="twitter:title" tal:attributes="content view/title" />
<meta name="twitter:description" tal:attributes="content view/description" />
Expand Down
34 changes: 16 additions & 18 deletions sc/social/like/tests/test_plugin_twitter.py
Expand Up @@ -68,7 +68,7 @@ def setUp(self):

with api.env.adopt_roles(['Manager']):
self.image = api.content.create(
self.portal, 'Image', id='test-image')
self.portal, 'Image', title='Lorem Ipsum', description='Neque Porro')

set_image_field(self.image, load_image(1024, 768), 'image/png')

Expand All @@ -77,26 +77,24 @@ def setup_content(self, portal):
self.document = portal['my-document']

def test_plugin_view_metadata(self):
plugin = self.plugin
image = self.image
plugin_view = plugin.view()
view = image.restrictedTraverse(plugin_view)
view.title = 'Twitter Title'
view.description = 'Twitter Description'

def get_meta_content(name):
"""Return the content attribute of the meta tag specified by name."""
return html.find('*/meta[@name="{0}"]'.format(name)).attrib['content']

view = self.image.restrictedTraverse(self.plugin.view())
record = ISocialLikeSettings.__identifier__ + '.twitter_username'
api.portal.set_registry_record(record, 'plone')

metadata = view.metadata()
self.assertIn(
'<meta name="twitter:card" content="summary_large_image" />', metadata)
self.assertIn(
'<meta name="twitter:image" content="http://nohost/plone/test-image/@@images', metadata)
self.assertIn(
'<meta name="twitter:site" content="@plone" />', metadata)
self.assertIn(
'<meta name="twitter:title" content="Twitter Title" />', metadata)
self.assertIn(
'<meta name="twitter:description" content="Twitter Description" />', metadata)
from lxml import etree
html = etree.HTML(view.metadata())

self.assertEqual(get_meta_content('twitter:card'), 'summary_large_image')
expected = r'http://nohost/plone/lorem-ipsum/@@images/[0-9a-f--]+.png'
self.assertRegexpMatches(get_meta_content('twitter:image'), expected)
self.assertEqual(get_meta_content('twitter:site'), '@plone')
self.assertEqual(get_meta_content('twitter:title'), 'Lorem Ipsum')
self.assertEqual(get_meta_content('twitter:description'), 'Neque Porro')

def test_plugin_view_html(self):
plugin = self.plugin
Expand Down
1 change: 1 addition & 0 deletions setup.py
Expand Up @@ -66,6 +66,7 @@
extras_require={
'test': [
'AccessControl',
'lxml',
'mock',
'plone.app.robotframework',
'plone.app.testing [robot] >=4.2.2',
Expand Down

0 comments on commit 595cc9f

Please sign in to comment.