Skip to content

Commit

Permalink
Merge 19d7f8a into 67cb86b
Browse files Browse the repository at this point in the history
  • Loading branch information
claytonc authored Sep 21, 2017
2 parents 67cb86b + 19d7f8a commit 303ba96
Show file tree
Hide file tree
Showing 13 changed files with 186 additions and 4 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=".imagefallback.ImageFallBack"
layer="..interfaces.ISocialLikeLayer"
permission="zope.Public"
/>

</configure>
27 changes: 27 additions & 0 deletions sc/social/like/browser/imagefallback.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# -*- 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 ImageFallBack(Download):

filename = None
data = None

def __call__(self):
self.setup()
super(ImageFallBack, self).__call__()

def setup(self):
fallback_image = api.portal.get_registry_record('fallback_image', interface=ISocialLikeSettings)
if fallback_image:
filename, data = b64decode_file(fallback_image)
data = NamedImage(data=data, filename=filename)
self.data = data
self.filename = filename

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'Image fallback'),
description=_(
u'help_fallback_image',
default=u'Content that does not have an image will be displayed on the share.\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 "O conteúdo que não possuir uma imagem essa será exibida no compartilhamento.\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
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 -*-
4 changes: 2 additions & 2 deletions sc/social/like/upgrades/v3047/configure.zcml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
xmlns:genericsetup="http://namespaces.zope.org/genericsetup">

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

<genericsetup:upgradeDepends
Expand Down
40 changes: 40 additions & 0 deletions sc/social/like/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# -*- coding:utf-8 -*-
from Acquisition import aq_base
from plone import api
from plone.formwidget.namedfile.converter import b64decode_file
from plone.namedfile.file import NamedBlobImage
from Products.Archetypes.interfaces import IBaseContent
from Products.CMFPlone.utils import safe_hasattr
from sc.social.like import LikeMessageFactory as _
Expand Down Expand Up @@ -198,3 +201,40 @@ def validate_og_lead_image(image):
raise ValueError(MSG_INVALID_OG_LEAD_IMAGE_ASPECT_RATIO)

return True


def get_fallback_image():
from sc.social.like.interfaces import ISocialLikeSettings

fallback_image = api.portal.get_registry_record('fallback_image', interface=ISocialLikeSettings)
if fallback_image:
filename, data = b64decode_file(fallback_image)
return '/@@sociallike-fallback-image/' + filename
else:
return '/logo.png'


def validate_image_settings(value):
"""Check image fallback be in formats mime type, dimensions and size."""

filename, data = b64decode_file(value)
image = NamedBlobImage(data=data, filename=filename)

if image is None:
return True

if image.contentType not in OG_LEAD_IMAGE_MIME_TYPES:
raise Invalid(MSG_INVALID_OG_LEAD_IMAGE_MIME_TYPE)

if image.getSize() > OG_LEAD_IMAGE_MAX_SIZE:
raise Invalid(MSG_INVALID_OG_LEAD_IMAGE_SIZE)

width, height = image.getImageSize()
if width < OG_LEAD_IMAGE_MIN_WIDTH or height < OG_LEAD_IMAGE_MIN_HEIGHT:
raise Invalid(MSG_INVALID_OG_LEAD_IMAGE_DIMENSIONS)

aspect_ratio = float(width) / float(height)
if aspect_ratio < OG_LEAD_IMAGE_MIN_ASPECT_RATIO:
raise Invalid(MSG_INVALID_OG_LEAD_IMAGE_ASPECT_RATIO)

return True
19 changes: 19 additions & 0 deletions sc/social/like/vocabularies.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
# -*- coding:utf-8 -*-
from plone.app.imaging.utils import getAllowedSizes
from plone.memoize import forever
from sc.social.like import LikeMessageFactory as _
from sc.social.like.config import OG_LEAD_IMAGE_MIN_HEIGHT
from sc.social.like.config import OG_LEAD_IMAGE_MIN_WIDTH
from sc.social.like.plugins import IPlugin
from zope.component import getUtilitiesFor
from zope.interface import implementer
from zope.interface import provider
from zope.schema.interfaces import IVocabularyFactory
from zope.schema.vocabulary import SimpleTerm
from zope.schema.vocabulary import SimpleVocabulary
Expand Down Expand Up @@ -46,3 +50,18 @@ def __call__(self, context):
SimpleTerm(value=u'Like', title=_(u'Like')),
SimpleTerm(value=u'Share', title=_(u'Share')),
])


@provider(IVocabularyFactory)
def image_scales_vocabulary(object):
"""Obtains available scales from plone.app.imaging"""
terms = []
for scale, (width, height) in getAllowedSizes().iteritems():
if width >= OG_LEAD_IMAGE_MIN_WIDTH and height >= OG_LEAD_IMAGE_MIN_HEIGHT:
translated = _(
'imagescale_{0:s}'.format(scale),
default='{0:s} ${{width}}x${{height}}'.format(scale),
mapping={'width': str(width), 'height': str(height)})
terms.append(SimpleTerm(scale, scale, translated))

return SimpleVocabulary(terms)

0 comments on commit 303ba96

Please sign in to comment.