Skip to content
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

Closed
wants to merge 13 commits into from
Closed

Conversation

rodfersou
Copy link
Member

@rodfersou rodfersou commented Aug 24, 2017

refs. #112

@rodfersou rodfersou force-pushed the unify-metadata branch 2 times, most recently from 494c868 to 1f4a3c2 Compare August 24, 2017 04:50
@rodfersou rodfersou changed the title Move metadata to the view template Move metadata to the viewlet template Aug 24, 2017
@rodfersou rodfersou force-pushed the unify-metadata branch 2 times, most recently from 0e922f9 to 67be257 Compare August 24, 2017 04:53
@@ -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):
Copy link
Member

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.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

def via(self):
record = ISocialLikeSettings.__identifier__ + '.twitter_username'
try:
return api.portal.get_registry_record(record)
Copy link
Member

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.

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()
Copy link
Member

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" />
Copy link
Member

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.

Copy link
Member Author

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

Copy link
Member

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.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

if img:
return img.url
else:
return '{0}/logo.png'.format(self.site_url)
Copy link
Member

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().

Copy link
Member Author

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

😄

Copy link
Member

@hvelarde hvelarde Aug 24, 2017

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.

Copy link
Member Author

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" />
Copy link
Member

@hvelarde hvelarde Aug 24, 2017

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">
Copy link
Member

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">
Copy link
Member

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.

@hvelarde
Copy link
Member

hvelarde commented Aug 24, 2017

overall work is looking good; this change will leave the code way faster an easier to maintain.

FYI @claytonc

(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)
Copy link
Member Author

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.

return img
if view is None:
return
# XXX: If I recall correctly, Dexterity works different to do this
Copy link
Member Author

Choose a reason for hiding this comment

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

Copy link
Member Author

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.

@rodfersou rodfersou force-pushed the unify-metadata branch 4 times, most recently from 8a145c2 to c51aa31 Compare August 25, 2017 13:25
og_type = view.type()
self.assertIn('website', og_type)

def test_plugin_view_document(self):
Copy link
Member Author

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):
Copy link
Member Author

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):
Copy link
Member Author

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):
Copy link
Member Author

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):
Copy link
Member Author

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')
Copy link
Member Author

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):
Copy link
Member Author

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):
Copy link
Member Author

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):
Copy link
Member Author

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

title='My document',
description='Lorem Ipsum',
)
set_image_field(self.document, load_image(1024, 768), 'image/png')
Copy link
Member

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.

@rodfersou
Copy link
Member Author

@hvelarde green 🍻

@rodfersou rodfersou self-assigned this Aug 25, 2017
Copy link
Member

@hvelarde hvelarde left a 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" />
Copy link
Member

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.

Copy link
Member Author

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" />
Copy link
Member

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.

Copy link
Member Author

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.

def update(self):
self.setup()

def setup(self):
Copy link
Member

Choose a reason for hiding this comment

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

rename setup() to update().

@@ -74,6 +94,66 @@ def enabled(self):
return True
return self.helper.enabled(self.view)

@property
def via(self):
record = dict(
Copy link
Member

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='')

The plugins() method inherited from BaseLikeViewlet takes too long and is not necessary here.
@hvelarde hvelarde requested a review from claytonc August 26, 2017 01:44
Copy link
Member

@idgserpro idgserpro left a 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):
Copy link
Member

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?

Copy link
Member

@hvelarde hvelarde Aug 29, 2017

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)
Copy link
Member

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

'plone.api',
in app_id, admins and twitter_site?

Copy link
Member

@hvelarde hvelarde Aug 29, 2017

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
Copy link
Member

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.

Copy link
Member

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">
Copy link
Member Author

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.

@hvelarde
Copy link
Member

superseded by #135

@hvelarde hvelarde closed this Sep 13, 2017
@hvelarde hvelarde deleted the unify-metadata branch September 13, 2017 03:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants