Skip to content

Commit

Permalink
Merge 4a1abe7 into 67cb86b
Browse files Browse the repository at this point in the history
  • Loading branch information
claytonc committed Sep 25, 2017
2 parents 67cb86b + 4a1abe7 commit 0503029
Show file tree
Hide file tree
Showing 18 changed files with 285 additions and 34 deletions.
8 changes: 8 additions & 0 deletions sc/social/like/browser/configure.zcml
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,12 @@
permission="cmf.ManagePortal"
/>

<browser:page
name="sociallike-fallback-image"
for="Products.CMFPlone.interfaces.IPloneSiteRoot"
class=".fallbackimage.FallBackImage"
layer="..interfaces.ISocialLikeLayer"
permission="zope.Public"
/>

</configure>
31 changes: 31 additions & 0 deletions sc/social/like/browser/fallbackimage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# -*- coding: utf-8 -*-
from plone import api
from plone.formwidget.namedfile.converter import b64decode_file
from plone.namedfile.browser import Download
from plone.namedfile.file import NamedImage
from sc.social.like.interfaces import ISocialLikeSettings


class FallBackImage(Download):
"""Helper view to return the fallback image."""

def __init__(self, context, request):
super(FallBackImage, self).__init__(context, request)

record = ISocialLikeSettings.__identifier__ + '.fallback_image'
fallback_image = api.portal.get_registry_record(record, default=None)

if fallback_image is not None:
# set fallback image data for download
filename, data = b64decode_file(fallback_image)
data = NamedImage(data=data, filename=filename)
self.filename, self.data = filename, data
# enable image caching for 2 minutes
self.request.RESPONSE.setHeader('Cache-Control', 'max-age=120, public')
else:
# resource no longer available
self.data = NamedImage(data='')
self.request.RESPONSE.setStatus(410) # Gone

def _getFile(self):
return self.data
3 changes: 2 additions & 1 deletion sc/social/like/browser/viewlets.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
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_fallback_image
from sc.social.like.utils import get_language
from zope.component import getMultiAdapter
from zope.component import getUtility
Expand Down Expand Up @@ -110,7 +111,7 @@ def image_url(self):
if img:
return img.url
else:
return self.portal_url() + '/logo.png'
return self.portal_url() + get_fallback_image()

def image_width(self):
return self.image.width
Expand Down
5 changes: 5 additions & 0 deletions sc/social/like/configure.zcml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
name="sc.social.likes.plugins"
/>

<utility
component=".vocabularies.image_scales_vocabulary"
name="sc.social.likes.ImagesScales"
/>

<subscriber
for="plone.registry.interfaces.IRecordModifiedEvent"
handler=".subscribers.social_media_record_synchronizer"
Expand Down
29 changes: 29 additions & 0 deletions sc/social/like/interfaces.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
# -*- coding:utf-8 -*-
from plone.autoform import directives as form
from plone.formwidget.namedfile.widget import NamedImageFieldWidget
from plone.supermodel import model
from sc.social.like import LikeMessageFactory as _
from sc.social.like.config import DEFAULT_ENABLED_CONTENT_TYPES
from sc.social.like.config import DEFAULT_PLUGINS_ENABLED
from sc.social.like.utils import validate_canonical_domain
from sc.social.like.utils import validate_image_settings
from sc.social.like.vocabularies import FacebookButtonsVocabulary
from sc.social.like.vocabularies import FacebookVerbsVocabulary
from sc.social.like.vocabularies import TypeButtonVocabulary
Expand Down Expand Up @@ -212,3 +215,29 @@ class ISocialLikeSettings(model.Schema):
required=False,
default='',
)

model.fieldset(
'open_graph', label=u'Open Graph', fields=['fallback_image', 'image_scale'])

form.widget('fallback_image', NamedImageFieldWidget)
fallback_image = schema.ASCII(
title=_(u'Fallback image'),
description=_(
u'help_fallback_image',
default=u'Image that will be displayed on the share for content that does not have an image..\n '
u'If you do not enter a \"logo.png\" image will be the default.',
),
required=False,
constraint=validate_image_settings,
)

image_scale = schema.Choice(
title=_(u'Image scale for shared'),
description=_(
u'help_image_scale',
default=u'Scale size of the image of the content to be shared.',
),
required=True,
default=u'large',
vocabulary='sc.social.likes.ImagesScales',
)
20 changes: 20 additions & 0 deletions sc/social/like/locales/pt_BR/LC_MESSAGES/sc.social.like.po
Original file line number Diff line number Diff line change
Expand Up @@ -422,3 +422,23 @@ msgstr "Cancelar"
#: sc/social/like/vocabularies.py:36
msgid "vertical"
msgstr "vertical"

#: ./sc/social/like/interfaces.py:209
msgid "Open Graph"
msgstr "Open Graph"

#: ./sc/social/like/interfaces.py:211
msgid "Image fallback"
msgstr "Imagem"

#: ./sc/social/like/interfaces.py:213
msgid "help_fallback_image"
msgstr "Imagem que será exibida no compartilhamento para conteúdo que não possui uma imagem.\n Caso não informe uma, a imagem \"logo.png\" será a padrão."

#: ./sc/social/like/interfaces.py:223
msgid "Image scale for shared"
msgstr "Tamanho da imagem do compartilhamento"

#: ./sc/social/like/interfaces.py:225
msgid "help_image_scale"
msgstr "Tamanho em escala da imagem do conteúdo que será compartilhado."
20 changes: 20 additions & 0 deletions sc/social/like/locales/sc.social.like.pot
Original file line number Diff line number Diff line change
Expand Up @@ -422,3 +422,23 @@ msgstr ""
#: ./sc/social/like/vocabularies.py:36
msgid "vertical"
msgstr ""

#: ./sc/social/like/interfaces.py:209
msgid "Open Graph"
msgstr ""

#: ./sc/social/like/interfaces.py:211
msgid "Image fallback"
msgstr ""

#: ./sc/social/like/interfaces.py:213
msgid "help_fallback_image"
msgstr ""

#: ./sc/social/like/interfaces.py:223
msgid "Image scale for shared"
msgstr ""

#: ./sc/social/like/interfaces.py:225
msgid "help_image_scale"
msgstr ""
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>3047</version>
<version>3048</version>
</metadata>
12 changes: 12 additions & 0 deletions sc/social/like/tests/test_controlpanel.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# -*- coding: utf-8 -*-
# from cStringIO import StringIO
from plone import api
from plone.app.testing import logout
# from plone.formwidget.namedfile.converter import b64decode_file
from plone.registry.interfaces import IRegistry
from sc.social.like.config import PROJECTNAME
from sc.social.like.interfaces import ISocialLikeSettings
Expand Down Expand Up @@ -107,6 +109,14 @@ def test_twitter_username_record_in_registry(self):
self.assertTrue(hasattr(self.settings, 'twitter_username'))
self.assertEqual(self.settings.twitter_username, '')

def test_fallback_image_record_in_registry(self):
self.assertTrue(hasattr(self.settings, 'fallback_image'))
self.assertEqual(self.settings.fallback_image, None)

def test_image_scale_record_in_registry(self):
self.assertTrue(hasattr(self.settings, 'image_scale'))
self.assertEqual(self.settings.image_scale, 'large')

def test_records_removed_on_uninstall(self):
qi = self.portal['portal_quickinstaller']

Expand All @@ -126,6 +136,8 @@ def test_records_removed_on_uninstall(self):
ISocialLikeSettings.__identifier__ + '.fbbuttons',
ISocialLikeSettings.__identifier__ + '.fbshowlikes',
ISocialLikeSettings.__identifier__ + '.twitter_username',
ISocialLikeSettings.__identifier__ + '.fallback_image',
ISocialLikeSettings.__identifier__ + '.image_scale',
]

for r in records:
Expand Down
32 changes: 0 additions & 32 deletions sc/social/like/tests/test_view.py

This file was deleted.

66 changes: 66 additions & 0 deletions sc/social/like/tests/test_views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# -*- coding: utf-8 -*-
from plone import api
from plone.app.testing import setRoles
from plone.app.testing import TEST_USER_ID
from sc.social.like.browser.socialikes import SocialLikes
from sc.social.like.testing import INTEGRATION_TESTING

import unittest


class BrowserViewTestCase(unittest.TestCase):

layer = INTEGRATION_TESTING

def setUp(self):
self.portal = self.layer['portal']
self.request = self.layer['request']
setRoles(self.portal, TEST_USER_ID, ['Manager'])
self.portal.invokeFactory('Document', 'my-document')
self.document = self.portal['my-document']

def view(self, context=None):
context = context or self.portal
view = SocialLikes(context, self.request)
return view

def test_disabled_on_portal(self):
view = self.view(self.portal)
self.assertFalse(view.enabled)

def test_enabled_on_document(self):
view = self.view(self.document)
self.assertTrue(view.enabled)


class FallBackImageViewTestCase(unittest.TestCase):

layer = INTEGRATION_TESTING

def setUp(self):
self.portal = self.layer['portal']
self.request = self.layer['request']

def set_fallback_image(self, filename):
from sc.social.like.interfaces import ISocialLikeSettings
from sc.social.like.tests.utils import get_file_b64encoded
record = ISocialLikeSettings.__identifier__ + '.fallback_image'
logo = get_file_b64encoded(filename)
api.portal.set_registry_record(record, logo)

def test_no_fall_back_image(self):
view = api.content.get_view(
'sociallike-fallback-image', self.portal, self.request)
self.assertEqual(len(view()), 0)
self.assertEqual(self.request.RESPONSE.getStatus(), 410)

def test_fall_back_image(self):
self.set_fallback_image('imgtest_1920x1080.png')
view = api.content.get_view(
'sociallike-fallback-image', self.portal, self.request)
self.assertEqual(len(view()), 42901)
self.assertEqual(self.request.RESPONSE.getStatus(), 200)
self.assertEqual(
self.request.RESPONSE.getHeader('Cache-Control'),
'max-age=120, public',
)
16 changes: 16 additions & 0 deletions sc/social/like/tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,19 @@ def get_random_string(length):
from random import choice
from string import printable
return ''.join(choice(printable) for i in xrange(0, length))


def get_file(filename):
"""Return contents of file from current directory."""
import os
path = os.path.abspath(os.path.dirname(__file__))
path = os.path.join(path, 'images', filename)
with open(path, 'rb') as f:
return f.read()


def get_file_b64encoded(filename):
"""Load file from current directory and return it b64encoded."""
from plone.formwidget.namedfile.converter import b64encode_file
data = get_file(filename)
return b64encode_file(filename, data)
1 change: 1 addition & 0 deletions sc/social/like/upgrades/configure.zcml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@
<include package=".v3045" />
<include package=".v3046" />
<include package=".v3047" />
<include package=".v3048" />

</configure>
1 change: 1 addition & 0 deletions sc/social/like/upgrades/v3047/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# -*- coding:utf-8 -*-
1 change: 1 addition & 0 deletions sc/social/like/upgrades/v3048/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# -*- coding:utf-8 -*-
18 changes: 18 additions & 0 deletions sc/social/like/upgrades/v3048/configure.zcml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<configure
xmlns="http://namespaces.zope.org/zope"
xmlns:genericsetup="http://namespaces.zope.org/genericsetup">

<genericsetup:upgradeSteps
source="3047"
destination="3048"
profile="sc.social.like:default">

<genericsetup:upgradeDepends
title="Add fallback image in configlet"
description="Adds new field to the control panel configlet."
import_steps="plone.app.registry"
/>

</genericsetup:upgradeSteps>

</configure>
Loading

0 comments on commit 0503029

Please sign in to comment.