-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Move metadata to the viewlet template #124
Conversation
494c868
to
1f4a3c2
Compare
0e922f9
to
67be257
Compare
sc/social/like/browser/viewlets.py
Outdated
@@ -61,7 +67,16 @@ class SocialMetadataViewlet(BaseLikeViewlet): | |||
"""Viewlet used to insert metadata into page header | |||
""" | |||
render = ViewPageTemplateFile('templates/metadata.pt') | |||
render_method = 'metadata' | |||
|
|||
def __init__(self, context, request, view, manager): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
once again, don't add any code into the __init__()
method or we can get unexpected errors.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
sc/social/like/browser/viewlets.py
Outdated
def via(self): | ||
record = ISocialLikeSettings.__identifier__ + '.twitter_username' | ||
try: | ||
return api.portal.get_registry_record(record) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
api.portal.get_registry_record()
now supports a default value, use it instead.
sc/social/like/browser/viewlets.py
Outdated
self.description = context.Description() | ||
self.portal_state = getMultiAdapter((self.context, self.request), | ||
name=u'plone_portal_state') | ||
self.site_url = self.portal_state.portal_url() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use the API: self.site_url = api.portal.get().absolute_url()
<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" /> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
og:image
must include metadata about the image itself.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, just for curiosity.. why? this is not a requirement to make it work AFAIK
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that's a best practice and the way it works right now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
sc/social/like/browser/viewlets.py
Outdated
if img: | ||
return img.url | ||
else: | ||
return '{0}/logo.png'.format(self.site_url) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use string concatenation as it's faster than format()
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that's an unexpected information, thank you for sharing.
anyway, do you plan to build a rocket? https://hackernoon.com/yes-python-is-slow-and-i-dont-care-13763980b5a1
😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not building a rocket but... which line is easier to read and understand (plus 3 times faster) in this case?
return '{0}/logo.png'.format(self.site_url)
or:
return self.site_url + '/logo.png'
the answer, again, is obvious to me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🍻
data-href view/canonical_url;"></div> | ||
</tal:fb> | ||
<!-- Facebook --> | ||
<script type="application/javascript" tal:content="view/fbjs" /> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
don't use the type
attribute for JavaScript.
<!-- Google+ --> | ||
<script type="application/javascript"> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
don't use the type
attribute for JavaScript.
@@ -1,6 +1,17 @@ | |||
<tal:linkedin> | |||
<!-- Linkedin --> | |||
<script type="application/javascript"> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
don't use the type
attribute for JavaScript.
overall work is looking good; this change will leave the code way faster an easier to maintain. FYI @claytonc |
sc/social/like/browser/viewlets.py
Outdated
(self.context, self.request), name=u'plone_portal_state') | ||
self.site_url = portal_state.portal_url() | ||
self.url = self.context.absolute_url() | ||
self.language = facebook_language(get_language(self.context), self.language) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I forgot this attribute (what gave an exception) adding in this commit.
sc/social/like/utils.py
Outdated
return img | ||
if view is None: | ||
return | ||
# XXX: If I recall correctly, Dexterity works different to do this |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking with more attention, we are using the view.getImageSize
method, not image.getImageSize
method, so it is probably "ok" like this.
8a145c2
to
c51aa31
Compare
og_type = view.type() | ||
self.assertIn('website', og_type) | ||
|
||
def test_plugin_view_document(self): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tests moved at viewlet, when test metadata.
og_type = view.type() | ||
self.assertIn('website', og_type) | ||
|
||
def test_plugin_view_image(self): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These tests are not needed anymore since we are using scale large by default for all plugins.
self.assertEqual(view.image_width(), 1200) | ||
self.assertEqual(view.image_height(), 675) | ||
|
||
def test_plugin_view_image_large(self): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These tests are not needed anymore since we are using scale large by default for all plugins.
self.assertEqual(view.image_width(), 1200) | ||
self.assertEqual(view.image_height(), 675) | ||
|
||
def test_plugin_view_newsitem(self): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These tests are not needed anymore since we are using scale large by default for all plugins.
self.assertEqual(view.image_width(), 1024) | ||
self.assertEqual(view.image_height(), 768) | ||
|
||
def test_plugin_view_newsitem_large(self): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These tests are not needed anymore since we are using scale large by default for all plugins.
|
||
# FIXME: we need to rethink this feature | ||
@unittest.skipIf(IS_PLONE_5, 'Metadata viewlet is disabled in Plone 5') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now metadata is always shown
@@ -92,13 +92,13 @@ def test_privacy_plugin_view_html(self): | |||
html = view.link() | |||
self.assertIn('Share on Google+', html) | |||
|
|||
def test_plugin_view_metadata(self): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Keep testing javascript existence, but in plugin template now.
|
||
# FIXME: we need to rethink this feature | ||
@unittest.skipIf(IS_PLONE_5, 'Metadata viewlet is disabled in Plone 5') | ||
def test_plugin_language(self): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Language (and script) moved into plugin template
@@ -68,27 +67,6 @@ def setUp(self): | |||
self.plugins = dict(getUtilitiesFor(IPlugin)) | |||
self.plugin = self.plugins[name] | |||
|
|||
# FIXME: we need to rethink this feature | |||
@unittest.skipIf(IS_PLONE_5, 'Metadata viewlet is disabled in Plone 5') | |||
def test_plugin_view_metadata(self): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Test moved into viewlet test
05c141d
to
d3cd2a4
Compare
title='My document', | ||
description='Lorem Ipsum', | ||
) | ||
set_image_field(self.document, load_image(1024, 768), 'image/png') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Document content type doesn't have image
field.
d3cd2a4
to
a14c6c7
Compare
@hvelarde green 🍻 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please rebase against master after #126 is merged.
<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" /> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you're completely removing fb:admins
and fb:app_id
properties from the code; this needs to be fixed.
please add those to the test so this doesn't happen anymore without notice.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, I misunderstand the issue.. I'll add back now, thank you.
Plone 5 already includes metadata for Facebook and Twitter. | ||
</tal:comment> | ||
<tal:fb condition="not:view/is_plone_5"> | ||
<meta property="og:site_name" tal:attributes="content view/portal_title" /> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you're completely removing og:site_name
property from the code; this needs to be fixed.
please add those to the test so this doesn't happen anymore without notice.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, I misunderstand the issue.. I'll add back now, thank you.
sc/social/like/browser/viewlets.py
Outdated
def update(self): | ||
self.setup() | ||
|
||
def setup(self): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rename setup()
to update()
.
sc/social/like/browser/viewlets.py
Outdated
@@ -74,6 +94,66 @@ def enabled(self): | |||
return True | |||
return self.helper.enabled(self.view) | |||
|
|||
@property | |||
def via(self): | |||
record = dict( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use here the following pattern instead for better readability:
record = ISocialLikeSettings.__identifier__ + '.twitter_username'
return api.portal.get_registry_record(record, default='')
a14c6c7
to
aa5d8ff
Compare
The plugins() method inherited from BaseLikeViewlet takes too long and is not necessary here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can't give a full review to all modifications because we're not familiar with the whole integration with all these external services (Twitter, Facebook and such) and what your changes may implicate, so we reviewed from a Plone/Python perspective, ok?
@@ -57,15 +65,33 @@ def update(self): | |||
return | |||
|
|||
|
|||
class SocialMetadataViewlet(BaseLikeViewlet): | |||
"""Viewlet used to insert metadata into page header | |||
class SocialMetadataViewlet(ViewletBase): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't you still inherit BaseLikeViewlet
before removing it completely if your plan is backwards compatibility? If you're no inheriting it, what's the motivation of keeping it above and adding a TODO
to remove instead of removing it already?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's more complicated: in fact we broke the plugin feature in release 2.5 after migrating everything to plone.app.registry. I was thinking about that yesterday when I was reviewing the work done. We didn't want to change too many things in this PR but it's inevitable because the code is way too complex.
if we really want to maintain the plugin extensibility we need to redesign it from scratch to use only one utility and one view per plugin, and using the extension mechanism already present in z3c.form. unfortunately, we have not enough heads and hands to do so in the way we may want.
we'll need to rework the whole PR.
index = ViewPageTemplateFile('templates/metadata.pt') | ||
|
||
def update(self): | ||
registry = getUtility(IRegistry) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any reason for not using plone.api directly since it's already a dependency of sc.social.like
Line 45 in 40cfc4c
'plone.api', |
app_id
, admins
and twitter_site
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
because plone.api sucks at handling the registry as I would have to add one call to get_registry_record()
for every field and that leads to more lines of code at the end.
we need to implement something like api.registry.get_record()
and api.registry.get_proxy()
.
import re | ||
import unittest | ||
|
||
|
||
# set the "SKIP_CODE_PROFILING" environent variable to skip profiling |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where does SKIP_CODE_PROFILING
comes from? Is it from .travis.yml
? If so, a small doc here would be a nice addition.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just added that for running test locally as the profiling pollutes the test results.
<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"> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no need to write tal:condition inside a <tal: tag, you can write just condition instead.
superseded by #135 |
refs. #112