Skip to content

Commit

Permalink
Keep in sync redundant fields found in the new Social Media configlet
Browse files Browse the repository at this point in the history
Changes on facebook_app_id, facebook_username, or twitter_username fields will be reflected in both configlets.
  • Loading branch information
hvelarde committed Mar 10, 2017
1 parent e3ee77f commit 3a0312d
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 1 deletion.
5 changes: 4 additions & 1 deletion CHANGES.rst
Expand Up @@ -6,7 +6,9 @@ There's a frood who really knows where his towel is.
2.10 (unreleased)
^^^^^^^^^^^^^^^^^

- Nothing changed yet.
- In Plone 5, keep in sync some redundant fields found in the new Social Media configlet.
Changes on ``facebook_app_id``, ``facebook_username``, or ``twitter_username`` fields will be reflected in both configlets (fixes `#100`_).
[hvelarde]


2.9 (2017-03-09)
Expand Down Expand Up @@ -277,3 +279,4 @@ Previous entries can be found in the HISTORY.rst file.
.. _`#65`: https://github.com/collective/sc.social.like/issues/65
.. _`#83`: https://github.com/collective/sc.social.like/issues/83
.. _`#91`: https://github.com/collective/sc.social.like/issues/91
.. _`#100`: https://github.com/collective/sc.social.like/issues/100
5 changes: 5 additions & 0 deletions sc/social/like/configure.zcml
Expand Up @@ -22,4 +22,9 @@
name="sc.social.likes.plugins"
/>

<subscriber
for="plone.registry.interfaces.IRecordModifiedEvent"
handler=".subscribers.social_media_record_synchronizer"
/>

</configure>
74 changes: 74 additions & 0 deletions sc/social/like/subscribers.py
@@ -0,0 +1,74 @@
# -*- coding: utf-8 -*-
"""Package event subscribers.
Plone 5 includes a new social media configlet that duplicates some
records already included in this package. To deal with that we use
an event subscriber declared in this module to synchronize the values
of the redundant records on every change.
"""
from plone import api
from plone.registry.interfaces import IRegistry
from sc.social.like.config import IS_PLONE_5
from sc.social.like.config import PROJECTNAME
from sc.social.like.interfaces import ISocialLikeSettings
from sc.social.like.logger import logger
from zope.component import getUtility
from zope.schema.interfaces import WrongType


# redundant fields so far
FIELDS = ('facebook_app_id', 'facebook_username', 'twitter_username')

# used to avoid RuntimeError due to infinite recursion
_FLAG = False


def social_media_record_synchronizer(event):
"""Synchronize Plone 5 social media records if needed."""
# subscribers are registered even if packages are not installed
qi = api.portal.get_tool('portal_quickinstaller')
if not qi.isProductInstalled(PROJECTNAME):
return

if not IS_PLONE_5:
return

# check if we've been here before as part of same event processing
global _FLAG
if _FLAG:
return

logger.info(u'Processing: ' + repr(event.record))
field = event.record.fieldName
if field not in FIELDS:
logger.info(u'No need to synchronize')
return

# find out which record we need to synchronize
from Products.CMFPlone.interfaces.controlpanel import ISocialMediaSchema
interface = event.record.interfaceName
if interface == ISocialLikeSettings.__identifier__:
# sc.social.like record modified; synchronize Plone record
record = 'plone.' + field
elif interface == ISocialMediaSchema.__identifier__:
# Plone record modified; synchronize sc.social.like record
record = ISocialLikeSettings.__identifier__ + '.' + field
else:
# unsupported interface name
logger.info(u'No need to synchronize')
return

registry = getUtility(IRegistry)
# we're going to modify a record and this will fire the same event
# we need to avoid RuntimeError due to infinite recursion
_FLAG = True
try:
registry[record] = str(event.record.value)
except WrongType:
# Plone 5 declares records as TextLine
registry[record] = unicode(event.record.value)
_FLAG = False

msg = '{0} was synchronized; new value is "{1}"'
logger.info(
msg.format(repr(registry.records[record]), event.record.value))

0 comments on commit 3a0312d

Please sign in to comment.