products/CPSRSS

changeset 245:f8a474119003

moved getRSSItems skins script to view class
author Georges Racinet on purity.racinet.fr <georges@racinet.fr>
date Mon, 06 Dec 2010 06:11:50 +0100
parents 350fa123d006
children 6b024ed7b935
files browser/channels.py browser/configure.zcml skins/getRSSItems.py skins/widget_portlet_rss.pt
diffstat 4 files changed, 131 insertions(+), 88 deletions(-) [+]
line diff
     1.1 --- a/browser/channels.py
     1.2 +++ b/browser/channels.py
     1.3 @@ -15,7 +15,16 @@
     1.4  # You should have received a copy of the GNU General Public License
     1.5  # along with this program.  If not, see <http://www.gnu.org/licenses/>.
     1.6  
     1.7 +from copy import deepcopy
     1.8 +import logging
     1.9 +import operator
    1.10 +
    1.11 +from Globals import InitializeClass
    1.12 +from AccessControl import ClassSecurityInfo
    1.13 +from Acquisition import aq_inner
    1.14 +
    1.15  from Products.CMFCore.utils import getToolByName
    1.16 +from Products.CMFCore.permissions import View
    1.17  from Products.CPSonFive.browser import AqSafeBrowserView
    1.18  
    1.19  from Products.CPSUtil.id import generateId
    1.20 @@ -26,9 +35,16 @@
    1.21  
    1.22  from Products.CPSRSS.interfaces import IRSSChannelContainer
    1.23  
    1.24 +from Products.CPSUtil.text import summarize
    1.25 +
    1.26 +logger = logging.getLogger(__name__)
    1.27 +
    1.28 +DEFAULT_RSS_ITEM_DISPLAY = 'cpsportlet_rssitem_display'
    1.29  
    1.30  class ManageChannels(AqSafeBrowserView):
    1.31  
    1.32 +    security = ClassSecurityInfo()
    1.33 +
    1.34      def __init__(self, *args, **kwargs):
    1.35          AqSafeBrowserView.__init__(self, *args, **kwargs)
    1.36          self.aqSafeSet('container', self.lookupContainer())
    1.37 @@ -58,6 +74,95 @@
    1.38              return ()
    1.39          return cont.objectValues([RSSChannel.meta_type])
    1.40  
    1.41 +    # traditional security declaration is necessary for browser:view,
    1.42 +    # and further in current Five 1.3.2 takes precedence over zcml
    1.43 +    # Actual protection must be declared, docstring created on the fly if
    1.44 +    # missing
    1.45 +    security.declareProtected(View, 'rssItems')
    1.46 +    def rssItems(self, cont_id=None, **kw):
    1.47 +        # straight adaptation from old skins script
    1.48 +        # now that proof-of-concept works, should be split
    1.49 +        if cont_id is None:
    1.50 +            cont = self.aqSafeGet('container')
    1.51 +        else:
    1.52 +            cont = self.context[cont_id]
    1.53 +
    1.54 +        if cont is None:
    1.55 +            return ()
    1.56 +
    1.57 +        logger.info("RSS channels from %r", cont)
    1.58 +        first_item = int(kw.get('first_item', 1))
    1.59 +        max_items = int(kw.get('max_items', 0))
    1.60 +        max_words = int(kw.get('max_words', 0))
    1.61 +
    1.62 +        data_items = []
    1.63 +        channels_ids = kw.get('channels')
    1.64 +        for channel_id in channels_ids:
    1.65 +            if not cont.hasObject(channel_id):
    1.66 +                continue
    1.67 +            channel = cont[channel_id]
    1.68 +            if channel is None:
    1.69 +                continue
    1.70 +            data = channel.getData(max_items + first_item - 1)
    1.71 +            lines = deepcopy(data['lines']) # RSSChan did a simple copy
    1.72 +            for line in lines:
    1.73 +                # lines will be shuffled around (timely sort), so channel 
    1.74 +                # dependent display options have to be copied
    1.75 +                line['newWindow'] = data['newWindow']
    1.76 +            data_items += lines
    1.77 +            if first_item > 1:
    1.78 +                data_items = data_items[first_item - 1:]
    1.79 +
    1.80 +        # If there is more than 1 channel we need to sort the rss items to
    1.81 +        # only keep the most recent ones, up to max_items.
    1.82 +        if len(channels_ids) > 1:
    1.83 +            # NOTE: One should replace "modified" with "updated" if switching
    1.84 +            # to a newer version of Feed Parser
    1.85 +            # http://feedparser.org/docs/date-parsing.html
    1.86 +            # Relying on the 'modified_parsed' item for the sorting.
    1.87 +            data_items.sort(key=operator.itemgetter('modified_parsed'),
    1.88 +                            reverse=True)
    1.89 +            data_items = data_items[:max_items]
    1.90 +
    1.91 +        render_method = kw.get('render_method') or DEFAULT_RSS_ITEM_DISPLAY
    1.92 +        render_method = getattr(aq_inner(self.context), render_method, None)
    1.93 +
    1.94 +        order = 0
    1.95 +        for item in data_items:
    1.96 +            description = item['description']
    1.97 +            modified = item['modified']
    1.98 +            author = item['author']
    1.99 +            if not author:
   1.100 +                author = 'unknown'
   1.101 +
   1.102 +            # Item rendering and display
   1.103 +            rendered = ''
   1.104 +
   1.105 +            # render the item using a custom display method (.zpt, .py, .dtml)
   1.106 +            if render_method is not None:
   1.107 +                item['summary'] = summarize(description, max_words)
   1.108 +                kw.update({'item': item,
   1.109 +                           'order': order,
   1.110 +                          })
   1.111 +                rendered = apply(render_method, (), kw)
   1.112 +
   1.113 +            # this information is used by custom templates that call
   1.114 +            # getRSSItems() directly. GR TODO: who are these ?
   1.115 +            data_items[order].update(
   1.116 +                {'description': description,
   1.117 +                 'rendered': rendered,
   1.118 +                 'metadata':
   1.119 +                    {'creator': author,
   1.120 +                     'contributor': author,
   1.121 +                     'date': modified,
   1.122 +                     'issued': modified,
   1.123 +                     'created': modified,
   1.124 +                    },
   1.125 +                })
   1.126 +            order += 1
   1.127 +
   1.128 +        return data_items
   1.129 +
   1.130      def redirectManageChannels(self):
   1.131          self.request.RESPONSE.redirect('/'.join((
   1.132                  self.context.absolute_url_path(), 'manage_channels.html')))
   1.133 @@ -93,3 +198,5 @@
   1.134  
   1.135          cont._setObject(cid, channel)
   1.136          self.redirectManageChannels()
   1.137 +
   1.138 +InitializeClass(ManageChannels)
     2.1 --- a/browser/configure.zcml
     2.2 +++ b/browser/configure.zcml
     2.3 @@ -26,6 +26,22 @@
     2.4      attribute="refresh"
     2.5      />
     2.6  
     2.7 +  <browser:view
     2.8 +    for="Products.CPSCore.interfaces.ICPSProxy"
     2.9 +    name="channels_restricted"
    2.10 +    class=".browser.channels.ManageChannels"
    2.11 +    allowed_attributes="rssItems"
    2.12 +    permission="zope2.View"
    2.13 +    />
    2.14 +
    2.15 +  <browser:view
    2.16 +    for="Products.CPSCore.interfaces.ICPSSite"
    2.17 +    name="channels_restricted"
    2.18 +    class=".browser.channels.ManageChannels"
    2.19 +    allowed_attributes="rssItems"
    2.20 +    permission="zope2.View"
    2.21 +    />
    2.22 +
    2.23    <!--browser:page
    2.24      for="Products.CPSRSS.interfaces.IRSSChannel"
    2.25      name="edit.html"
     3.1 --- a/skins/getRSSItems.py
     3.2 +++ b/skins/getRSSItems.py
     3.3 @@ -1,90 +1,7 @@
     3.4  ##parameters=**kw
     3.5 -#
     3.6 -# $Id$
     3.7 -"""The script that returns RSS items according to the portlet parameters.
     3.8 +"""This script is deprecated in favor of direct call to a view.
     3.9  """
    3.10  
    3.11 -from copy import deepcopy
    3.12 -from logging import getLogger
    3.13 -
    3.14 -import operator
    3.15 -
    3.16 -from Products.CPSUtil.text import summarize
    3.17 -
    3.18 -LOG_KEY = 'CPSPortlets.getRSSItems'
    3.19 -
    3.20 -DEFAULT_RSS_ITEM_DISPLAY = 'cpsportlet_rssitem_display'
    3.21 -
    3.22 -#logger = getLogger(LOG_KEY)
    3.23 -
    3.24 -rsstool = getattr(context, 'portal_rss', None)
    3.25 -if rsstool is None:
    3.26 -    return []
    3.27 -
    3.28 -first_item = int(kw.get('first_item', 1))
    3.29 -max_items = int(kw.get('max_items', 0))
    3.30 -max_words = int(kw.get('max_words', 0))
    3.31 -
    3.32 -data_items = []
    3.33 -channels_ids = kw.get('channels')
    3.34 -for channel_id in channels_ids:
    3.35 -    channel = getattr(rsstool.aq_inner.aq_explicit, channel_id, None)
    3.36 -    if channel is None:
    3.37 -        continue
    3.38 -    data = channel.getData(max_items + first_item - 1)
    3.39 -    lines = deepcopy(data['lines']) # RSSChan did a simple copy
    3.40 -    for line in lines:
    3.41 -        # lines will be shuffled around (timely sort), so channel dependent
    3.42 -        # display options have to be copied
    3.43 -        line['newWindow'] = data['newWindow']
    3.44 -    data_items += lines
    3.45 -    if first_item > 1:
    3.46 -        data_items = data_items[first_item - 1:]
    3.47 -
    3.48 -# If there is more than 1 channel we need to sort the rss items to only keep the
    3.49 -# more recent ones, up to max_items.
    3.50 -if len(channels_ids) > 1:
    3.51 -    # NOTE: One should replace "modified" with "updated" if switching to a newer
    3.52 -    # version of Feed Parser http://feedparser.org/docs/date-parsing.html
    3.53 -    # Relying on the 'modified_parsed' item for the sorting.
    3.54 -    data_items.sort(key=operator.itemgetter('modified_parsed'), reverse=True)
    3.55 -    data_items = data_items[:max_items]
    3.56 -
    3.57 -render_method = kw.get('render_method') or DEFAULT_RSS_ITEM_DISPLAY
    3.58 -render_method = getattr(context, render_method, None)
    3.59 -
    3.60 -order = 0
    3.61 -for item in data_items:
    3.62 -    description = item['description']
    3.63 -    modified = item['modified']
    3.64 -    author = item['author']
    3.65 -    if not author:
    3.66 -        author = 'unknown'
    3.67 -
    3.68 -    # Item rendering and display
    3.69 -    rendered = ''
    3.70 -
    3.71 -    # render the item using a custom display method (.zpt, .py, .dtml)
    3.72 -    if render_method is not None:
    3.73 -        item['summary'] = summarize(description, max_words)
    3.74 -        kw.update({'item': item,
    3.75 -                   'order': order,
    3.76 -                  })
    3.77 -        rendered = apply(render_method, (), kw)
    3.78 -
    3.79 -    # this information is used by custom templates that call getRSSItems()
    3.80 -    # directly.
    3.81 -    data_items[order].update(
    3.82 -        {'description': description,
    3.83 -         'rendered': rendered,
    3.84 -         'metadata':
    3.85 -            {'creator': author,
    3.86 -             'contributor': author,
    3.87 -             'date': modified,
    3.88 -             'issued': modified,
    3.89 -             'created': modified,
    3.90 -            },
    3.91 -        })
    3.92 -    order += 1
    3.93 -
    3.94 -return data_items
    3.95 +portal = context.portal_url.getPortalObject()
    3.96 +view = portal.restrictedTraverse('@@channels_restricted')
    3.97 +return view.rssItems(cont_id='portal_rss', **kw)
     4.1 --- a/skins/widget_portlet_rss.pt
     4.2 +++ b/skins/widget_portlet_rss.pt
     4.3 @@ -1,6 +1,9 @@
     4.4  <tal:block define="ds options/datastructure;
     4.5                     export request/form/export|nothing;
     4.6 -                   rssitems python: here.getRSSItems(**dict(ds))"
     4.7 +                   portal here/portal_url/getPortalObject;
     4.8 +                   view nocall:portal/@@channels_restricted;
     4.9 +                   rssitems python: view.rssItems(cont_id='portal_rss',
    4.10 +                                                  **dict(ds))"
    4.11             condition="rssitems">
    4.12  
    4.13    <tal:block condition="not:export">