Skip to content

Commit

Permalink
Avoid using an instance memoizer in the portlet as it could cause harm
Browse files Browse the repository at this point in the history
  • Loading branch information
hvelarde committed Apr 27, 2018
1 parent 96bb5bb commit 9fa3e98
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 26 deletions.
3 changes: 2 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ Changelog
1.1b2 (unreleased)
^^^^^^^^^^^^^^^^^^

- Nothing changed yet.
- Avoid using an instance memoizer in the portlet as it could cause harm.
[hvelarde]


1.1b1 (2017-06-12)
Expand Down
37 changes: 12 additions & 25 deletions src/sc/periodicals/portlets/latest_periodical.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,20 @@
from plone.app.portlets.cache import render_cachekey
from plone.app.portlets.portlets import base
from plone.memoize import ram
from plone.memoize import view
from plone.memoize.compress import xhtml_compress
from plone.memoize.instance import memoize
from plone.portlets.interfaces import IPortletDataProvider
from Products.CMFCore.utils import getToolByName
from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile
from sc.periodicals import _
from sc.periodicals.content import IPeriodical
from zope import schema
from zope.formlib import form
from zope.interface import implements
from zope.interface import implementer


class ILatestPeriodicalPortlet(IPortletDataProvider):
"""This portlet shows the latest published periodical.
"""
"""This portlet shows the latest published periodical."""

header = schema.TextLine(
title=_(u'Portlet header'),
Expand Down Expand Up @@ -49,14 +48,8 @@ class ILatestPeriodicalPortlet(IPortletDataProvider):
required=False)


@implementer(ILatestPeriodicalPortlet)
class Assignment(base.Assignment):
"""
Portlet assignment.
This is what is actually managed through the portlets UI and associated
with columns.
"""

implements(ILatestPeriodicalPortlet)

def __init__(self, header=u'', image_scale=None, count=5, text=u''):

Expand All @@ -67,19 +60,13 @@ def __init__(self, header=u'', image_scale=None, count=5, text=u''):

@property
def title(self):
"""This property is used to give the title of the portlet in the
"manage portlets" screen. Here, we use the title that the user gave.
"""
return _(u'Latest Periodical')


class Renderer(base.Renderer):

_template = ViewPageTemplateFile('latest_periodical.pt')

def __init__(self, *args):
base.Renderer.__init__(self, *args)

@ram.cache(render_cachekey)
def render(self):
return xhtml_compress(self._template())
Expand All @@ -93,6 +80,7 @@ def available(self):
def title(self):
return self.data.header

@view.memoize
def get_latest_periodical(self):
"""
@return: returns the catalog results (brains) if exists
Expand All @@ -104,20 +92,19 @@ def get_latest_periodical(self):
sort_order='reverse',
review_state='published',
)

assert results in (0, 1)
return results[0] if results else None

def published_news_articles(self):
return self._data()

@memoize
def _data(self):
"""
@return:returns the articles in a periodical
"""
periodical = self.get_latest_periodical()
if periodical is None:
return []
return ()

context = aq_inner(self.context)
catalog = getToolByName(context, 'portal_catalog')
Expand All @@ -132,7 +119,7 @@ def _data(self):
sort_limit=count,
)

return articles[:count] if articles else []
return articles[:count] if articles else ()

def get_image_scale(self):
return self.data.image_scale
Expand All @@ -148,6 +135,7 @@ def get_text(self, mt='text/x-html-safe'):
orig = self.data.text
if not orig:
orig = u''

context = aq_inner(self.context)
if not isinstance(orig, unicode):
# Apply a potentially lossy transformation, and hope we stored
Expand All @@ -158,10 +146,9 @@ def get_text(self, mt='text/x-html-safe'):

# Portal transforms needs encoded strings
orig = orig.encode('utf-8')

transformer = getToolByName(context, 'portal_transforms')
data = transformer.convertTo(mt, orig,
context=context, mimetype='text/html')
data = transformer.convertTo(
mt, orig, context=context, mimetype='text/html')
result = data.getData()
if result:
if isinstance(result, str):
Expand All @@ -171,7 +158,7 @@ def get_text(self, mt='text/x-html-safe'):

def get_periodical_title(self):
periodical = self.get_latest_periodical()
return periodical.Title if periodical is not None else None
return periodical.Title if periodical else ''


class AddForm(base.AddForm):
Expand Down

0 comments on commit 9fa3e98

Please sign in to comment.