Skip to content

Commit

Permalink
Fix Caching issues with WhatsApp button
Browse files Browse the repository at this point in the history
  • Loading branch information
rodfersou committed Sep 10, 2015
1 parent cd41ef9 commit 48eca56
Show file tree
Hide file tree
Showing 15 changed files with 170 additions and 79 deletions.
6 changes: 5 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ There's a frood who really knows where his towel is.
2.4 (unreleased)
^^^^^^^^^^^^^^^^

- Fix Caching issues with WhatsApp button (closes `#56`_).
[rodfersou]

- Add Dutch translations.
[fredvd]

Expand Down Expand Up @@ -253,7 +256,8 @@ There's a frood who really knows where his towel is.

* Initial release [cleberjsantos]

.. _`#15`: https://github.com/collective/sc.social.like/pull/15
.. _`#36`: https://github.com/collective/sc.social.like/issues/36
.. _`#38`: https://github.com/collective/sc.social.like/issues/38
.. _`#39`: https://github.com/collective/sc.social.like/issues/39
.. _`#15`: https://github.com/collective/sc.social.like/pull/15
.. _`#56`: https://github.com/collective/sc.social.like/issues/56
7 changes: 7 additions & 0 deletions sc/social/like/browser/configure.zcml
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,11 @@
permission="zope.Public"
/>

<browser:resourceDirectory
name="sl_scripts"
directory="scripts"
layer="..interfaces.ISocialLikeLayer"
permission="zope.Public"
/>

</configure>
13 changes: 13 additions & 0 deletions sc/social/like/browser/scripts/social_like.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion sc/social/like/browser/stylesheets/social_like.css
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

#viewlet-social-like .whatsapp
{
display: inline-block;
display: none;
overflow: hidden;

width: 25px;
Expand Down
51 changes: 0 additions & 51 deletions sc/social/like/plugins/whatsapp/browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,43 +7,6 @@
from sc.social.like.utils import get_language
from zope.component import getMultiAdapter

import re


reg_b = re.compile(
r'(android|bb\\d+|meego).+mobile|avantgo|bada\\/|blackberry|blazer|compal|elaine|' +
r'fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|' +
r'mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\\/|plucker|' +
r'pocket|psp|series(4|6)0|symbian|treo|up\\.(browser|link)|vodafone|wap|' +
r'windows ce|xda|xiino',
re.I | re.M
)
reg_v = re.compile(
r'1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\\-)|ai(ko|rn)|' +
r'al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\\-m|r |s )|' +
r'avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\\-(n|u)|c55\\/|capi|' +
r'ccwa|cdm\\-|cell|chtm|cldc|cmd\\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\\-s|devi|' +
r'dica|dmob|do(c|p)o|ds(12|\\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|' +
r'wa|ze)|fetc|fly(\\-|_)|g1 u|g560|gene|gf\\-5|g\\-mo|go(\\.w|od)|gr(ad|un)|haie|' +
r'hcit|hd\\-(m|p|t)|hei\\-|hi(pt|ta)|hp( i|ip)|hs\\-c|ht(c(\\-| |_|a|g|p|s|t)|' +
r'tp)|hu(aw|tc)|i\\-(20|go|ma)|i230|iac( |\\-|\\/)|ibro|idea|ig01|ikom|im1k|inno|' +
r'ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\\/)|klon|kpt |kwc\\-|kyo(c|' +
r'k)|le(no|xi)|lg( g|\\/(k|l|u)|50|54|\\-[a-w])|libw|lynx|m1\\-w|m3ga|m50\\/|' +
r'ma(te|ui|xo)|mc(01|21|ca)|m\\-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|' +
r't(\\-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|' +
r'n7(0(0|1)|10)|ne((c|m)\\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|' +
r'owg1|p800|pan(a|d|t)|pdxg|pg(13|\\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\\-2|po(ck|' +
r'rt|se)|prox|psio|pt\\-g|qa\\-a|qc(07|12|21|32|60|\\-[2-7]|i\\-)|qtek|r380|r600|' +
r'raks|rim9|ro(ve|zo)|s55\\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\\-|oo|p\\-)|sdk\\/|' +
r'se(c(\\-|0|1)|47|mc|nd|ri)|sgh\\-|shar|sie(\\-|m)|sk\\-0|sl(45|id)|sm(al|ar|b3|' +
r'it|t5)|so(ft|ny)|sp(01|h\\-|v\\-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|' +
r'lk)|tcl\\-|tdg\\-|tel(i|m)|tim\\-|t\\-mo|to(pl|sh)|ts(70|m\\-|m3|m5)|tx\\-9|' +
r'up(\\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\\-v)|vm40|voda|vulc|' +
r'vx(52|53|60|61|70|80|81|83|85|98)|w3c(\\-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|' +
r'x700|yas\\-|your|zeto|zte\\-',
re.I | re.M
)


class PluginView(BrowserView):

Expand Down Expand Up @@ -77,17 +40,3 @@ def __init__(self, context, request):
)
)
)

def is_mobile_browser(self):
"""Return a bool indicating if is the user is usign a mobile
browser. This value will be used to define if the viewlet
will be rendered or not.
:returns: True if the user agent matches a mobile browser,
False otherwise
:rtype: bool
"""
user_agent = self.request.environ.get('HTTP_USER_AGENT', '')
b = reg_b.search(user_agent)
v = reg_v.search(user_agent[0:4])
return bool(b or v)
2 changes: 1 addition & 1 deletion sc/social/like/plugins/whatsapp/templates/plugin.pt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<tal:wa condition="view/is_mobile_browser" i18n:domain="sc.social.like">
<tal:wa i18n:domain="sc.social.like">
<a class="whatsapp"
tal:attributes="href view/whatsappurl"
i18n:translate=""
Expand Down
7 changes: 7 additions & 0 deletions sc/social/like/profiles/default/jsregistry.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0"?>
<object name="portal_javascripts" purge="False">
<javascript id="++resource++sl_scripts/social_like.js"
cacheable="True" compression="safe" cookable="True" enabled="True"
inline="False" authenticated="False"
insert-after="*" />
</object>
2 changes: 1 addition & 1 deletion sc/social/like/profiles/default/metadata.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0"?>
<metadata>
<version>3020</version>
<version>3030</version>
</metadata>
23 changes: 0 additions & 23 deletions sc/social/like/tests/test_plugin_whatsapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,9 @@ class PluginViewsTest(unittest.TestCase):

layer = INTEGRATION_TESTING

def _set_mobile_browser(self, is_mobile):
if is_mobile:
self.request.environ['HTTP_USER_AGENT'] = (
'Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_2_1 like Mac OS X; en-us) '
'AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8C148 '
'Safari/6533.18.5'
)
else:
del(self.request.environ['HTTP_USER_AGENT'])

def setUp(self):
self.portal = self.layer['portal']
self.request = self.layer['request']
self._set_mobile_browser(True)
self.adapter = LikeControlPanelAdapter(self.portal)
setRoles(self.portal, TEST_USER_ID, ['Manager'])
self.setup_content(self.portal)
Expand Down Expand Up @@ -104,15 +93,3 @@ def test_plugin_urlnoscript_encoding(self):
'Not%C3%ADcia%20-%20http%3A//nohost/plone/my-document">Share in WhatsApp',
html
)

def test_is_mobile(self):
plugin = self.plugin
document = self.document
plugin_view = plugin.view()
view = document.restrictedTraverse(plugin_view)

self._set_mobile_browser(False)
self.assertFalse(view.is_mobile_browser())

self._set_mobile_browser(True)
self.assertTrue(view.is_mobile_browser())
68 changes: 68 additions & 0 deletions sc/social/like/tests/test_upgrades.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# -*- coding: utf-8 -*-
from plone import api
from sc.social.like.testing import INTEGRATION_TESTING

import unittest


class UpgradeTestCaseBase(unittest.TestCase):

layer = INTEGRATION_TESTING

def setUp(self, from_version, to_version):
self.portal = self.layer['portal']
self.setup = self.portal['portal_setup']
self.profile_id = u'sc.social.like:default'
self.from_version = from_version
self.to_version = to_version

def get_upgrade_step(self, title):
"""Get the named upgrade step."""
self.setup.setLastVersionForProfile(self.profile_id, self.from_version)
upgrades = self.setup.listUpgrades(self.profile_id)
steps = [s for s in upgrades[0] if s['title'] == title]
return steps[0] if steps else None

def execute_upgrade_step(self, step):
"""Execute an upgrade step."""
request = self.layer['request']
request.form['profile_id'] = self.profile_id
request.form['upgrades'] = [step['id']]
self.setup.manage_doUpgrades(request=request)

@property
def total_steps(self):
"""Return the number of steps in the upgrade."""
self.setup.setLastVersionForProfile(self.profile_id, self.from_version)
upgrades = self.setup.listUpgrades(self.profile_id)
assert len(upgrades) > 0
return len(upgrades[0])


class Upgrade1to2TestCase(UpgradeTestCaseBase):

def setUp(self):
UpgradeTestCaseBase.setUp(self, u'3020', u'3030')

def test_upgrade_to_3030_registrations(self):
version = self.setup.getLastVersionForProfile(self.profile_id)[0]
self.assertTrue(int(version) >= int(self.to_version))
self.assertEqual(self.total_steps, 3)

def test_update_resource_conditions(self):
# check if the upgrade step is registered
title = u'Fix caching for WhatsApp'
step = self.get_upgrade_step(title)
self.assertTrue(step is not None)

js_tool = api.portal.get_tool('portal_javascripts')
JS_ID = '++resource++sl_scripts/social_like.js'

# simulate state on previous version
js_tool.unregisterResource(JS_ID)

# run the upgrade step to validate the update
self.execute_upgrade_step(step)

# Check
self.assertIn(JS_ID, js_tool.getResourceIds())
20 changes: 20 additions & 0 deletions sc/social/like/upgrades/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,21 @@
# -*- coding:utf-8 -*-
from plone import api
from sc.social.like.config import PROJECTNAME

import logging

logger = logging.getLogger(PROJECTNAME)


def cook_css_resources(context):
"""Cook css resources."""
css_tool = api.portal.get_tool('portal_css')
css_tool.cookResources()
logger.info('CSS resources were cooked')


def cook_javascript_resources(context):
"""Cook javascript resources."""
js_tool = api.portal.get_tool('portal_javascripts')
js_tool.cookResources()
logger.info('Javascript resources were cooked')
3 changes: 2 additions & 1 deletion sc/social/like/upgrades/configure.zcml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@
<include package=".v3000" />
<include package=".v3010" />
<include package=".v3020" />
<include package=".v3030" />

</configure>
</configure>
13 changes: 13 additions & 0 deletions sc/social/like/upgrades/v3030/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# -*- coding:utf-8 -*-
from sc.social.like.config import PROJECTNAME

import logging

logger = logging.getLogger(PROJECTNAME)


def fix_caching_for_whatsapp(setup_tool):
"""Fix caching for WhatsApp."""
profile = 'profile-{0}:default'.format(PROJECTNAME)
setup_tool.runImportStepFromProfile(profile, 'jsregistry')
logger.info('Caching for WhatsApp were fixed.')
31 changes: 31 additions & 0 deletions sc/social/like/upgrades/v3030/configure.zcml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<configure
xmlns="http://namespaces.zope.org/zope"
xmlns:gs="http://namespaces.zope.org/genericsetup"
i18n_domain="sc.social.like">

<gs:upgradeSteps
source="3020"
destination="3030"
profile="sc.social.like:default">

<gs:upgradeStep
title="Fix caching for WhatsApp"
description="Fix caching issues with WhatsApp button"
handler=".fix_caching_for_whatsapp"
/>

<gs:upgradeStep
title="Cook CSS resources"
description="There were changes in the CSS files, so we need to cook the resources."
handler="..cook_css_resources"
/>

<gs:upgradeStep
title="Cook JS resources"
description="There were changes in the JS files, so we need to cook the resources."
handler="..cook_javascript_resources"
/>

</gs:upgradeSteps>

</configure>
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
'Products.CMFPlone >=4.1',
'Products.CMFQuickInstallerTool',
'Products.GenericSetup',
'plone.api',
'setuptools',
'zope.component',
'zope.i18nmessageid',
Expand Down

0 comments on commit 48eca56

Please sign in to comment.