source: products/SimpleBlog/branches/plone-2.5/content/blog.py

Last change on this file was 1, checked in by myroslav, 18 years ago

Building directory structure

  • Property svn:eol-style set to native
File size: 15.3 KB
Line 
1from Products.Archetypes.public import BaseFolderSchema, Schema
2from Products.Archetypes.public import StringField, LinesField, IntegerField, BooleanField
3from Products.Archetypes.public import SelectionWidget, LinesWidget, TextAreaWidget, MultiSelectionWidget, IntegerWidget, RichWidget, IdWidget, StringWidget, BooleanWidget
4from Products.Archetypes.public import BaseFolder, registerType
5from Products.Archetypes.public import DisplayList
6
7from Products.CMFCore import CMFCorePermissions
8from DateTime import DateTime
9from Products.SimpleBlog.config import DISPLAY_MODE, ENABLE_ADSENSE
10import Products.SimpleBlog.Permissions
11from Products.CMFCore.utils import getToolByName
12
13from Products.SimpleBlog import MetaWeblogAPI
14from Products.SimpleBlog import BloggerAPI
15from Products.SimpleBlog import MovableTypeAPI
16from Products.Archetypes.utils import  unique
17
18from Products.ATContentTypes.content.base import ATCTFolder
19
20schema = ATCTFolder.schema.copy() +  Schema((
21    StringField('description',
22                isMetadata=1,
23                accessor='Description',
24                searchable=1,
25                widget=TextAreaWidget(label='Description',
26                                      label_msgid="label_blog_description",
27                                      description_msgid="help_blog_description",
28                                      i18n_domain="SimpleBlog",
29                                      description='Give a description for this SimpleBlog.')),
30    # this field is deprecated, here for compatibility
31    StringField('displayMode',
32                vocabulary=DISPLAY_MODE,
33                widget=SelectionWidget(label='Display Mode', 
34                                       label_msgid="label_display_mode",
35                                       description_msgid="help_display_mode",
36                                       i18n_domain="SimpleBlog",
37                                       description='Choose the display mode.',
38                                       visible={'view' : 'invisible', 'edit':'invisible'}),
39                default='descriptionOnly'),
40    IntegerField('displayItems', 
41                widget=IntegerWidget(label='BlogEntries to display', 
42                                      label_msgid="label_display_items",
43                                      description_msgid="help_display_items",
44                                      i18n_domain="SimpleBlog",
45                                      description='Set the maximum number of BlogEntries to display.'), 
46                 default=20),
47    LinesField('categories', 
48               widget=LinesWidget(label='Possible Categories', 
49                                  label_msgid="label_categories",
50                                  description_msgid="help_categories",
51                                  i18n_domain="SimpleBlog",
52                                  description='Supply the list of possible categories that can be used in SimpleBlog Entries.')),
53    BooleanField('allowCrossPosting',
54             default=0, 
55             widget=BooleanWidget(condition="python:0", # hide the field
56                        label='Allow cross-posting',
57                        i18n_domain="SimpleBlog",
58                        label_msgid="label_allowCrossPosting",
59                        description_msgid="help_allowCrossPosting",
60                        description='When checked, this blog will include cross-post entries from other blogs.')),
61    LinesField('tags',
62               mutator = 'setTags',
63               widget=LinesWidget(label="Tags",
64                                  description='List of tags.'),
65               ),
66    BooleanField('warnForUnpublishedEntries', 
67               default=1,
68               schemata = 'interface',
69               widget=BooleanWidget(label='Show unpublished entries warning', 
70                      i18n_domain="SimpleBlog",
71                      label_msgid="label_warnForUnpublishedEntries",
72                      description_msgid="help_warnForUnpublishedEntries",
73                      description='When checked, a warning will be displayed on the blog\'s frontpage if there are entries that are not yet published.')),
74    BooleanField('showByline',
75                  default = 1,
76                  schemata = 'interface',
77                  widget=BooleanWidget(label="Show Byline footer",
78                                       label_msgid="label_show_buttons",
79                                       description_msgid="help_show_buttons",),
80               ),
81    BooleanField('showIcons',
82                  default = 1,
83                  schemata = 'interface',
84                  widget=BooleanWidget(label="Show Icons",
85                                       label_msgid="label_show_icons",
86                                       description_msgid="help_show_icons",),
87               ),
88    BooleanField('tagsEnabled',
89                  accessor = 'isTagsEnabled',
90                  default = 1,
91                  schemata = 'interface',
92                  widget = BooleanWidget(label='Enable technorati tags',
93                                     label_msgid="label_enable_tags",
94                                     description_msgid="help_enable_tags",),
95              ),
96    BooleanField('allowTrackback',
97                  default = 1,
98                  schemata = 'interface', 
99                  widget=BooleanWidget(label="Allow Trackback", 
100                                       label_msgid="label_allow_trackback",
101                                       description_msgid="help_allow_trackback",),
102               ),
103    # used to be new trackbaks notification email address
104    StringField('adminEmail',
105                accessor = 'getAdminEmail',
106                mutator = 'setAdminEmail',
107                schemata = 'interface',
108                default = '',
109                widget=StringWidget(label="Administrator's email", 
110                                    label_msgid="label_adminEmail",
111                                    description_msgid="help_adminEmail",
112                                    i18n_domain="SimpleBlog",
113                                    condition="python:0", # this line have to be removed in order to be visible/editable
114                                    description="Enter administrator's email for receiving notification about blog's activity"),
115               ),
116    BooleanField('allowDelicious',
117                default = 1,
118                accessor = 'isDeliciousEnabled',
119                schemata = 'interface', 
120                widget=BooleanWidget(label="Turn Delicious bookmarklet",
121                                    label_msgid="label_allow_delicious",
122                                    description_msgid="help_allow_delicious"),
123                ),
124    BooleanField('allowDigg',
125                default = 1,
126                accessor = 'isDiggEnabled',
127                schemata = 'interface',
128                widget=BooleanWidget(label="Turn Digg bookmarklet",
129                                    label_msgid="label_allow_digg",
130                                    description_msgid="help_allow_digg"),
131                ),
132    BooleanField('allowYahoo',
133                default = 1,
134                accessor = 'isYahooEnabled',
135                schemata = 'interface', 
136                widget=BooleanWidget(label="Turn Yahoo bookmarklet",
137                                    label_msgid="label_allow_yahoo",
138                                    description_msgid="help_allow_yahoo"),
139                ),
140    BooleanField('allowGoogle',
141                default = 1,
142                accessor = 'isGoogleEnabled',
143                schemata = 'interface',
144                widget=BooleanWidget(label="Turn Google bookmarklet",
145                                    label_msgid="label_allow_google",
146                                    description_msgid="help_allow_google"),
147                ),
148    BooleanField('allowSpurl',
149                default = 1,
150                accessor = 'isSpurlEnabled',
151                schemata = 'interface', 
152                widget=BooleanWidget(label="Turn Spurl bookmarklet",
153                                    label_msgid="label_allow_spurl",
154                                    description_msgid="help_allow_spurl"),
155                ),
156    BooleanField('enableTopAdsence',
157                 schemata = 'interface',
158                 default=0,
159                 accessor = 'isTopAdsenceEnabled',
160                 widget = BooleanWidget(format = 'select',
161                           label = 'Turn top Adsence block',
162                           label_msgid = "label_enable_top_adsence",
163                           description_msgid = "help_enable_top_adsence",
164                           i18n_domain = "SimpleBlog",
165                           description = None,
166                           condition="python:%s" % ENABLE_ADSENSE)),
167    StringField('topAdsence',
168                schemata = 'interface',
169                vocabulary = 'listAdsenseTemplates',
170                widget = SelectionWidget(format = 'select',
171                        label = 'Select top adsence template',
172                        label_msgid = "label_top_adsence",
173                        description_msgid = "help_top_adsence",
174                        i18n_domain = "SimpleBlog",
175                        description = None,
176                        condition="python:%s" % ENABLE_ADSENSE)),
177    BooleanField('enableBottomAdsence',
178                 schemata = 'interface',
179                 default=0,
180                 accessor = 'isBottomAdsenceEnabled',
181                 widget = BooleanWidget(format = 'select',
182                           label = 'Turn bottom adsence block',
183                           label_msgid = "label_enable_bottom_adsence",
184                           description_msgid = "help_enable_bottom_adsence",
185                           i18n_domain = "SimpleBlog",
186                           description = None,
187                           condition="python:%s" % ENABLE_ADSENSE)),
188    StringField('bottomAdsence',
189                schemata = 'interface', 
190                vocabulary = 'listAdsenseTemplates',
191                widget = SelectionWidget(format = 'select',
192                        label = 'Select bottom adsence',
193                        label_msgid = "label_bottom_adsence",
194                        description_msgid = "help_bottom_adsence",
195                        i18n_domain = "SimpleBlog",
196                        description = None,
197                           condition="python:%s" % ENABLE_ADSENSE)),
198        ))
199
200# hide relatedItems
201schema['relatedItems'].widget.visible={'view' : 'invisible', 'edit':'invisible'}
202
203class Blog(ATCTFolder):
204    """Blog"""
205
206    portal_type = meta_type = 'Blog'
207    archetype_name = 'Blog'
208    content_icon='simpleblog_icon.gif'
209    schema = schema
210    global_allow=1
211
212    default_view = 'simpleblog_view_title_description'
213    immediate_view = 'simpleblog_view_title_description'
214    suppl_views = ('simpleblog_view_title_description', 'simpleblog_view_title', 'simpleblog_view_title_description_body')
215
216    filter_content_types=1   
217    allowed_content_types=('BlogEntry', 'BlogFolder', 'Link', 'Image', 'File', 'Portlet')
218
219    blogger = None
220    metaWeblog = None
221
222    def initializeArchetype(self, **kwargs):
223        BaseFolder.initializeArchetype(self, **kwargs)
224        RPCAuth = self.simpleblog_tool.findRPCAuth(self)
225
226        # Setup the MetaWeblog API
227        self.metaWeblog = MetaWeblogAPI.MetaWeblogAPI().__of__(self)
228        self.metaWeblog.setupRPCAuth(RPCAuth)
229
230        # Setup the Blogger API
231        self.blogger = BloggerAPI.BloggerAPI().__of__(self)
232        self.blogger.setupRPCAuth(RPCAuth)
233
234        # Setup the MovableTypeAPI API
235        self.mt = MovableTypeAPI.MovableTypeAPI().__of__(self)
236        self.mt.setupRPCAuth(RPCAuth)   
237
238    def canSetDefaultPage(self):
239        return False
240
241    def manage_afterAdd(self, item, container):
242        BaseFolder.manage_afterAdd(self, item, container)
243        if self.simpleblog_tool.getCreatePortletOnBlogCreation():
244            if not hasattr(item.aq_base, 'right_slots'):
245                item._setProperty('right_slots', ['here/portlet_simpleblog/macros/portlet'], 'lines')
246
247    def synContentValues(self):
248        # get brains for items that are published within the context of this blog.
249        entries = self.simpleblog_tool.searchForEntries(self, maxResults=0)
250
251        # convert to objects
252        objs = [e.getObject() for e in entries]
253        return objs
254
255    def listCategories(self):
256        cats=self.getCategories()
257
258        # add the global categories
259        for c in self.simpleblog_tool.getGlobalCategories():
260            if not c in cats:
261                cats.append(c)
262        cats = list(cats)
263        cats.sort()
264        return tuple(cats)
265
266    def getForeignEntries(self):
267        """ Returns all entries from other blogs that are published into this blog using the remoteBlogs value """
268        return [f for f in self.getBRefs('AppearsIn') if self.portal_membership.checkPermission('View', f)]
269
270    def getEntries(self, category=None, maxResults=None, fromHere=0, filterState=1, sort=1, join=0, addCrossPostInfo=0, skipOnTop=0, **kwargs):
271        """ Return all the contained published entries, real objects, not the brains """
272        # see simpleblog_tool.searchForEntries for API description
273        query=kwargs
274        publishedState = self.simpleblog_tool.getPublishedState()
275        if category!=None:
276            query['EntryCategory']=category
277        if filterState:
278            query['review_state']=publishedState
279        if maxResults:
280            query['sort_limit'] = maxResults
281        query['path'] = {'query':self.simpleblog_tool.getObjectPath(self),'level':0}
282        query['sort_order'] = 'reverse'
283        query['sort_on'] = 'effective'
284        query['meta_type'] = 'BlogEntry'
285        if not skipOnTop:
286            query['getAlwaysOnTop']=1
287            # first the items that need to be listed on top
288            localOnTop = self.portal_catalog.searchResults(query)
289            localOnTop = [r.getObject() for r in localOnTop ]
290            # then the other items
291            query['getAlwaysOnTop']=0
292        else:
293            localOnTop = []
294        localNoTop = self.portal_catalog.searchResults(query)
295        localNoTop= [r.getObject() for r in localNoTop]
296
297        onTop = localOnTop
298        onBottom = localNoTop
299        if join:
300            results = onTop+onBottom
301            if maxResults==0:
302                return results
303            elif maxResults==None:
304                return results[:self.simpleblog_tool.getMaxItemsInPortlet()]
305            else:
306                return results[:maxResults]
307        else:
308            return (onTop, onBottom)
309
310    def getAdminEmail(self):
311        """ return blog admin email or root email """
312        val = self.getField('adminEmail').get(self)
313        if not val:
314            purl = getToolByName(self, 'portal_url')
315            val = purl.getPortalObject().getProperty("trackback_notification_email", "")
316        return val
317
318    def setTags(self, value, **kwargs):
319        """ Save tags in lower case """
320        value = unique(value)
321        value.sort(lambda x, y: cmp(x,y))
322        self.getField('tags').set(self, value, **kwargs)
323
324    def listAdsenseTemplates(self):
325        """ Return DisplayList of available adsence blocks """
326        try:
327            from Products.adsenseproduct.util import getAdsenseMap
328        except:
329            return ()
330        templates = [(key, value['title']) for key, value in getAdsenseMap().items()]
331        return DisplayList(templates)
332
333registerType(Blog)
334
Note: See TracBrowser for help on using the repository browser.