Back to index

plone3  3.1.7
SyndicationTool.py
Go to the documentation of this file.
00001 ##############################################################################
00002 #
00003 # Copyright (c) 2001 Zope Corporation and Contributors. All Rights Reserved.
00004 #
00005 # This software is subject to the provisions of the Zope Public License,
00006 # Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
00007 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
00008 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00009 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
00010 # FOR A PARTICULAR PURPOSE.
00011 #
00012 ##############################################################################
00013 """ CMFDefault portal_syndication tool.
00014 
00015 Manage outbound RSS syndication of folder content.
00016 
00017 $Id: SyndicationTool.py 77186 2007-06-28 19:06:19Z yuppie $
00018 """
00019 
00020 from AccessControl import ClassSecurityInfo
00021 from Acquisition import aq_base
00022 from DateTime import DateTime
00023 from Globals import HTMLFile
00024 from Globals import InitializeClass
00025 from OFS.SimpleItem import SimpleItem
00026 from zope.interface import implements
00027 
00028 from Products.CMFCore.interfaces import ISyndicationTool
00029 from Products.CMFCore.PortalFolder import PortalFolderBase
00030 from Products.CMFCore.utils import _checkPermission
00031 from Products.CMFCore.utils import registerToolInterface
00032 from Products.CMFCore.utils import UniqueObject
00033 
00034 from exceptions import AccessControl_Unauthorized
00035 from permissions import ManagePortal
00036 from permissions import ManageProperties
00037 from SyndicationInfo import SyndicationInformation
00038 from utils import _dtmldir
00039 
00040 
00041 class SyndicationTool(UniqueObject, SimpleItem):
00042 
00043     """
00044         The syndication tool manages the site-wide policy for
00045         syndication of folder content as RSS.
00046     """
00047 
00048     implements(ISyndicationTool)
00049     __implements__ = SimpleItem.__implements__
00050 
00051     id = 'portal_syndication'
00052     meta_type = 'Default Syndication Tool'
00053 
00054     security = ClassSecurityInfo()
00055 
00056     #Default Sitewide Values
00057     isAllowed = 0
00058     syUpdatePeriod = 'daily'
00059     syUpdateFrequency = 1
00060     syUpdateBase = DateTime()
00061     max_items = 15
00062 
00063     #ZMI Methods
00064     manage_options = ( ( { 'label'  : 'Overview'
00065                          , 'action' : 'overview'
00066                          , 'help'   : ( 'CMFDefault'
00067                                       , 'Syndication-Tool_Overview.stx' )
00068                          }
00069                         ,{ 'label'  : 'Properties'
00070                          , 'action' : 'propertiesForm'
00071                          , 'help'   : ( 'CMFDefault'
00072                                       , 'Syndication-Tool_Properties.stx' )
00073                          }
00074                         ,{ 'label'  : 'Policies'
00075                          , 'action' : 'policiesForm'
00076                          , 'help'   : ( 'CMFDefault'
00077                                       , 'Syndication-Tool_Policies.stx' )
00078                          }
00079                         ,{ 'label'  : 'Reports'
00080                          , 'action' : 'reportForm'
00081                          , 'help'   : ( 'CMFDefault'
00082                                       , 'Syndication-Tool_Reporting.stx' )
00083                          }
00084                         )
00085                      )
00086 
00087     security.declareProtected(ManagePortal, 'overview')
00088     overview = HTMLFile('synOverview', _dtmldir)
00089 
00090     security.declareProtected(ManagePortal, 'propertiesForm')
00091     propertiesForm = HTMLFile('synProps', _dtmldir)
00092 
00093     security.declareProtected(ManagePortal, 'policiesForm')
00094     policiesForm = HTMLFile('synPolicies', _dtmldir)
00095 
00096     security.declareProtected(ManagePortal, 'reportForm')
00097     reportForm = HTMLFile('synReports', _dtmldir)
00098 
00099     security.declareProtected(ManagePortal, 'editProperties')
00100     def editProperties( self
00101                       , updatePeriod=None
00102                       , updateFrequency=None
00103                       , updateBase=None
00104                       , isAllowed=None
00105                       , max_items=None
00106                       , REQUEST=None
00107                       ):
00108         """
00109         Edit the properties for the SystemWide defaults on the
00110         SyndicationTool.
00111         """
00112         if isAllowed is not None:
00113             self.isAllowed = isAllowed
00114 
00115         if updatePeriod is not None:
00116             self.syUpdatePeriod = updatePeriod
00117         else:
00118             try:
00119                 del self.syUpdatePeriod
00120             except (AttributeError, KeyError):
00121                 pass
00122 
00123         if updateFrequency is not None:
00124             self.syUpdateFrequency = int(updateFrequency)
00125         else:
00126             try:
00127                 del self.syUpdateFrequency
00128             except (AttributeError, KeyError):
00129                 pass
00130 
00131         if updateBase is not None:
00132             if type( updateBase ) is type( '' ):
00133                 updateBase = DateTime( updateBase )
00134             self.syUpdateBase = updateBase
00135         else:
00136             try:
00137                 del self.syUpdateBase
00138             except (AttributeError, KeyError):
00139                 pass
00140 
00141         if max_items is not None:
00142             self.max_items = int(max_items)
00143         else:
00144             try:
00145                 del self.max_items
00146             except (AttributeError, KeyError):
00147                 pass
00148 
00149         if REQUEST is not None:
00150             REQUEST['RESPONSE'].redirect( self.absolute_url()
00151                                         + '/propertiesForm'
00152                                         + '?manage_tabs_message=Tool+Updated.'
00153                                         )
00154 
00155     security.declarePublic( 'editSyInformationProperties' )
00156     def editSyInformationProperties( self
00157                                    , obj
00158                                    , updatePeriod=None
00159                                    , updateFrequency=None
00160                                    , updateBase=None
00161                                    , max_items=None
00162                                    , REQUEST=None
00163                                    ):
00164         """
00165         Edit syndication properties for the obj being passed in.
00166         These are held on the syndication_information object.
00167         Not Sitewide Properties.
00168         """
00169         if not _checkPermission( ManageProperties, obj ):
00170             raise AccessControl_Unauthorized
00171 
00172         syInfo = getattr(obj, 'syndication_information', None)
00173 
00174         if syInfo is None:
00175             raise 'Syndication is Disabled'
00176 
00177         if updatePeriod is not None:
00178             syInfo.syUpdatePeriod = updatePeriod
00179         else:
00180             syInfo.syUpdatePeriod = self.syUpdatePeriod
00181 
00182         if updateFrequency is not None:
00183             syInfo.syUpdateFrequency = int(updateFrequency)
00184         else:
00185             syInfo.syUpdateFrequency = self.syUpdateFrequency
00186 
00187         if updateBase is not None:
00188             if type( updateBase ) is type( '' ):
00189                 updateBase = DateTime( updateBase )
00190             syInfo.syUpdateBase = updateBase
00191         else:
00192             syInfo.syUpdateBase = self.syUpdateBase
00193 
00194         if max_items is not None:
00195             syInfo.max_items = int(max_items)
00196         else:
00197             syInfo.max_items = self.max_items
00198 
00199     security.declarePublic('enableSyndication')
00200     def enableSyndication(self, obj):
00201         """
00202         Enable syndication for the obj
00203         """
00204         if not self.isSiteSyndicationAllowed():
00205             raise 'Syndication is Disabled'
00206 
00207         if hasattr(aq_base(obj), 'syndication_information'):
00208             raise 'Syndication Information Exists'
00209 
00210         syInfo = SyndicationInformation()
00211         obj._setObject('syndication_information', syInfo)
00212         syInfo = obj._getOb('syndication_information')
00213         syInfo.syUpdatePeriod = self.syUpdatePeriod
00214         syInfo.syUpdateFrequency = self.syUpdateFrequency
00215         syInfo.syUpdateBase = self.syUpdateBase
00216         syInfo.max_items = self.max_items
00217         syInfo.description = "Channel Description"
00218 
00219     security.declarePublic('disableSyndication')
00220     def disableSyndication(self, obj):
00221         """
00222         Disable syndication for the obj; and remove it.
00223         """
00224         syInfo = getattr(obj, 'syndication_information', None)
00225 
00226         if syInfo is None:
00227             raise 'This object does not have Syndication Information'
00228 
00229         obj._delObject('syndication_information')
00230 
00231     security.declarePublic('getSyndicatableContent')
00232     def getSyndicatableContent(self, obj):
00233         """
00234         An interface for allowing folderish items to implement an
00235         equivalent of PortalFolderBase.contentValues()
00236         """
00237         if hasattr(obj, 'synContentValues'):
00238             values = obj.synContentValues()
00239         else:
00240             values = PortalFolderBase.contentValues(obj)
00241         return values
00242 
00243     security.declarePublic('buildUpdatePeriods')
00244     def buildUpdatePeriods(self):
00245         """
00246         Return a list of possible update periods for the xmlns: sy
00247         """
00248         updatePeriods = ( ('hourly',  'Hourly')
00249                         , ('daily',   'Daily')
00250                         , ('weekly',  'Weekly')
00251                         , ('monthly', 'Monthly')
00252                         , ('yearly',  'Yearly')
00253                         )
00254         return updatePeriods
00255 
00256     security.declarePublic('isSiteSyndicationAllowed')
00257     def isSiteSyndicationAllowed(self):
00258         """
00259         Return sitewide syndication policy
00260         """
00261         return self.isAllowed
00262 
00263     security.declarePublic('isSyndicationAllowed')
00264     def isSyndicationAllowed(self, obj=None):
00265         """
00266         Check whether syndication is enabled for the site.  This
00267         provides for extending the method to check for whether a
00268         particular obj is enabled, allowing for turning on only
00269         specific folders for syndication.
00270         """
00271         syInfo = getattr(aq_base(obj), 'syndication_information',
00272                          None)
00273         if syInfo is None:
00274             return 0
00275         else:
00276             return self.isSiteSyndicationAllowed()
00277 
00278     security.declarePublic('getUpdatePeriod')
00279     def getUpdatePeriod( self, obj=None ):
00280         """
00281         Return the update period for the RSS syn namespace.
00282         This is either on the object being passed or the
00283         portal_syndication tool (if a sitewide value or default
00284         is set)
00285 
00286         NOTE:  Need to add checks for sitewide policies!!!
00287         """
00288         if not self.isSiteSyndicationAllowed():
00289             raise 'Syndication is Not Allowed'
00290 
00291         if obj is None:
00292             return self.syUpdatePeriod
00293 
00294         syInfo = getattr(obj, 'syndication_information', None)
00295 
00296         if syInfo is not None:
00297             return syInfo.syUpdatePeriod
00298         else:
00299             return 'Syndication is Not Allowed'
00300 
00301     security.declarePublic('getUpdateFrequency')
00302     def getUpdateFrequency(self, obj=None):
00303         """
00304         Return the update frequency (as a positive integer) for
00305         the syn namespace.  This is either on the object being
00306         pass or the portal_syndication tool (if a sitewide value
00307         or default is set).
00308 
00309         Note:  Need to add checks for sitewide policies!!!
00310         """
00311         if not self.isSiteSyndicationAllowed():
00312             raise 'Syndication is not Allowed'
00313 
00314         if obj is None:
00315             return self.syUpdateFrequency
00316 
00317         syInfo = getattr(obj, 'syndication_information',
00318                             None)
00319         if syInfo is not None:
00320             return syInfo.syUpdateFrequency
00321         else:
00322             return 'Syndication is not Allowed'
00323 
00324     security.declarePublic('getUpdateBase')
00325     def getUpdateBase(self, obj=None):
00326         """
00327         Return the base date to be used with the update frequency
00328         and the update period to calculate a publishing schedule.
00329 
00330         Note:  I'm not sure what's best here, creation date, last
00331         modified date (of the folder being syndicated) or some
00332         arbitrary date.  For now, I'm going to build a updateBase
00333         time from zopetime and reformat it to meet the W3CDTF.
00334         Additionally, sitewide policy checks might have a place
00335         here...
00336         """
00337         if not self.isSiteSyndicationAllowed():
00338             raise 'Syndication is not Allowed'
00339 
00340         if obj is None:
00341             when = self.syUpdateBase
00342             return when.ISO()
00343 
00344         syInfo = getattr(obj, 'syndication_information',
00345                             None)
00346         if syInfo is not None:
00347                 when = syInfo.syUpdateBase
00348                 return when.ISO()
00349         else:
00350             return 'Syndication is not Allowed'
00351 
00352     security.declarePublic('getHTML4UpdateBase')
00353     def getHTML4UpdateBase(self, obj=None):
00354         """
00355         Return HTML4 formated UpdateBase DateTime
00356         """
00357         if not self.isSiteSyndicationAllowed():
00358             raise 'Syndication is not Allowed'
00359 
00360         if obj is None:
00361             when = self.syUpdateBase
00362             return when.HTML4()
00363 
00364         syInfo = getattr(obj, 'syndication_information',
00365                             None)
00366         if syInfo is not None:
00367             when = syInfo.syUpdateBase
00368             return when.HTML4()
00369         else:
00370             return 'Syndication is not Allowed'
00371 
00372     def getMaxItems(self, obj=None):
00373         """
00374         Return the max_items to be displayed in the syndication
00375         """
00376         if not self.isSiteSyndicationAllowed():
00377             raise 'Syndication is not Allowed'
00378 
00379         if obj is None:
00380             return self.max_items
00381 
00382         syInfo = getattr(obj, 'syndication_information',
00383                             None)
00384         if syInfo is not None:
00385             return syInfo.max_items
00386         else:
00387             return 'Syndication is not Allowed'
00388 
00389 InitializeClass(SyndicationTool)
00390 registerToolInterface('portal_syndication', ISyndicationTool)