root/qRSS2Syndication/trunk/utils.py

Revision 893 (checked in by mylan, 1 year ago)

Fix limiting number of syndication results bug.

Line 
1 from Products.CMFCore.utils import getToolByName
2 from Acquisition import aq_base
3
4
5 def setupRSS2Types(context,
6                     rss2_types = (),
7                     only_published = 0,
8                     include_subfolders = 0,
9                     articles_number = 20,
10                     REQUEST = None):
11     """ Save all needed RSS2 properties into 'syndication_information' """
12     obj=aq_base(context)
13     status = 'success'
14     message = 'Your changes have been saved'
15
16     syInfo = getattr(obj, 'syndication_information', None)
17
18     if syInfo is None:
19         message = 'Syndication is Disabled'
20         status = 'failed'
21     syInfo.rss2_types = list(rss2_types)
22     syInfo.only_published = only_published
23     syInfo.include_subfolders = include_subfolders
24     syInfo.max_items = int(articles_number)
25     return status, message
26
27 def getRSS2Properties(context):
28      """ Return directory of RSS2 properties from 'syndication_information' """
29      obj=aq_base(context)
30      syInfo = getattr(obj, 'syndication_information', None)
31      syPropeties={}
32      syPropeties['rss2_types'] = getattr(syInfo,'rss2_types',[])
33      syPropeties['only_published'] = getattr(syInfo,'only_published',0)
34      syPropeties['include_subfolders'] = getattr(syInfo,'include_subfolders',0)
35      syPropeties['articles_number'] =int(getattr(syInfo,'max_items',20))
36      return  syPropeties
37
38
39
40 def listSyndicatableContent(context):
41     """ List folder contents - catalog query
42         * filtered types only
43         * sort on effective
44         * take only first 'articles_number' of elements """
45     res = []
46     ps = getToolByName(context,'portal_syndication')
47     if ps.isSyndicationAllowed(context):
48         cpath = '/'.join(context.getPhysicalPath())
49         syProperties = getRSS2Properties(context)
50         types = syProperties['rss2_types']
51         only_published = syProperties['only_published']
52         include_subfolders = syProperties['include_subfolders']
53         articles_number = syProperties['articles_number']
54
55         catalog = getToolByName(context,'portal_catalog')
56         args = {'portal_type':types,
57                 'path':cpath,
58                 'sort_on':'effective',
59                 'sort_order':'reverse',}
60         if only_published:
61             args['review_state'] = 'published'
62         if include_subfolders == 0:
63             path_length=len(cpath)+1
64             for i in catalog.searchResults(args):
65                 if i.getPath()[path_length:].find('/')<0:
66                     res.append(i)
67             res = res[:articles_number]
68         else:
69             args['sort_limit'] = articles_number
70             res=catalog.searchResults(args)
71         res = [r.getObject() for r in res]
72     return res
73
74
75 def getFileContentType(context):
76     """  !ATAudio specific only! Get the content type of file field method """
77     return context.Schema()['file'].getContentType(context)
Note: See TracBrowser for help on using the browser.