source: products/SimpleBlog/branches/optimizations/content/blogentry.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: 10.6 KB
Line 
1from Products.Archetypes.public import BaseSchema, BaseFolderSchema, Schema
2from Products.Archetypes.public import StringField, TextField, LinesField, BooleanField, ReferenceField
3from Products.Archetypes.public import TextAreaWidget, VisualWidget,  MultiSelectionWidget, StringWidget, IdWidget, LinesWidget, KeywordWidget, SelectionWidget
4from Products.Archetypes.public import RichWidget, BooleanWidget
5from Products.Archetypes.public import BaseContent, registerType, BaseFolder
6from Products.CMFCore import CMFCorePermissions
7
8from Products.CMFCore.permissions import View
9from Products.CMFCore.permissions import ModifyPortalContent
10from Products.CMFCore.permissions import ManageProperties
11
12from Products.ATReferenceBrowserWidget.ATReferenceBrowserWidget import *
13from DateTime import DateTime
14from Products.SimpleBlog.Permissions import CROSS_POST_PERMISSION
15from Products.SimpleBlog.config import ENTRY_IS_FOLDERISH
16from Products.SimpleBlog.util import post_trackback
17from Products.ATContentTypes.content.document import finalizeATCTSchema
18from Products.ATContentTypes.content.base import ATCTContent, ATCTFolder
19
20from Products.CMFCore.utils import getToolByName
21import string,os,urllib,httplib,urlparse,re
22import sys
23from Products.SimpleBlog.util import *
24from Products.SimpleBlog.config import DIGG_TOPICS
25
26if ENTRY_IS_FOLDERISH:
27    schema = ATCTFolder.schema.copy()
28    parentClass = ATCTFolder
29else:
30    schema = ATCTContent.schema.copy()
31    parentClass = ATCTContent
32
33schema = schema +  Schema((
34    StringField('description',
35                searchable=1,
36                isMetadata=1,
37                accessor='Description',
38                widget=TextAreaWidget(label='Description', 
39                                    label_msgid="label_entry_description",
40                                    description_msgid="help_entry_description",
41                                    i18n_domain="SimpleBlog",
42                                    description='Give a description for this entry.')),
43    TextField('body',
44              searchable=1,
45              required=0,
46              primary=1,
47              default_content_type='text/html',
48              default_output_type='text/html',
49              allowable_content_types=('text/plain','text/structured', 'text/html', 'text/restructured'),
50              widget=RichWidget(label='Body',
51                                label_msgid="label_entry_body",
52                                description_msgid="help_entry_body",
53                                i18n_domain="SimpleBlog",
54                                description="")),
55   
56    ReferenceField('crossPosts',
57                   multiValued=1,
58                   required=0,
59                   relationship='AppearsIn',
60                   write_permission=CROSS_POST_PERMISSION,
61                   allowed_types=('Blog',),
62                   widget=ReferenceBrowserWidget(condition="python:0", # this line have to be removed in order to be visible/editable
63                            force_close_on_insert=1, 
64                            i18n_domain="SimpleBlog",
65                            label_msgid="label_crosspost",
66                            description_msgid="help_crosspost",
67                            label='Cross posts', 
68                            description='Select one or more other blogs where this entry will appear in when published additionally to this blog.')),
69    BooleanField('alwaysOnTop', 
70             default=0,
71             index='FieldIndex:schema',
72             widget=BooleanWidget(label='Entry is always listed on top', 
73                           label_msgid="label_always_top",
74                           description_msgid="help_always_top",
75                           i18n_domain="SimpleBlog",
76                           description='Controls if the Entry (when published) shown as the first Entry. If not checked, the effective date is used.')),
77    LinesField('categories',
78                    accessor='EntryCategory', 
79                    edit_accessor='EntryCategory', 
80                    index='KeywordIndex:schema', 
81                    vocabulary='listCategories',
82                    widget=MultiSelectionWidget(format='select', 
83                           label_msgid="label_entry_categories",
84                           description_msgid="help_entry_categories",
85                           i18n_domain="SimpleBlog",
86                           label='Categories', 
87                           description='Select to which categories this Entry belongs to')),
88    LinesField('tags',
89                    accessor = 'EntryTag', 
90                    mutator = 'setEntryTag',   
91                    index = 'KeywordIndex', 
92                    enforseVocabulary = 1,     
93                    vocabulary = 'listTags',   
94                    widget = KeywordWidget(label_msgid = "label_entry_tags",
95                           description_msgid = "help_entry_tags",
96                           i18n_domain = "SimpleBlog",
97                           label = 'Tags', 
98                           description = 'Select tags for technorati.com')),
99
100    BooleanField('alwaysOnTop', 
101             default = 0,
102             index = 'FieldIndex:schema',
103             widget = BooleanWidget(label = 'Entry is always listed on top.', 
104                           label_msgid = "label_always_top",
105                           description_msgid = "help_always_top",
106                           i18n_domain = "SimpleBlog",
107                           description = 'Controls if the Entry (when published) shown as the first Entry. If not checked, the effective date is used.')),
108    LinesField('sendTrackBackURLs',
109               languageIndependent = True,
110               searchable = True,
111               widget = LinesWidget(label = "sendTrackBackURLs",
112                                  label_msgid = "label_sendTrackBackURLs",
113                                  description = ("URL for sending trackbacks"),
114                                  description_msgid = "help_event_attendees",                 
115                                  i18n_domain = "plone")),
116    StringField('diggTopic',
117                default='offbeat_news',
118                vocabulary=DIGG_TOPICS,
119                widget=SelectionWidget(label='Digg topic',
120                        label_msgid="label_digg_topic",
121                        description_msgid="help_digg_topic",
122                        i18n_domain="SimpleBlog",
123                        description='Choose the digg topic.')),
124     ))
125
126finalizeATCTSchema(schema)
127
128class BlogEntry(parentClass):
129    """
130    A BlogEntry can exist inside a SimpleBlog Folder or an EntryFolder
131    """
132    portal_type = meta_type = 'BlogEntry'
133    archetype_name = 'Blog Entry'
134    content_icon='entry_icon.gif'
135    schema = schema
136    global_allow=0
137    allow_discussion = 1
138
139    default_view = 'blogentry_view'
140    immediate_view = 'blogentry_view'
141
142    _at_rename_after_creation = True
143
144    if ENTRY_IS_FOLDERISH:
145        filter_content_types=1
146        allowed_content_types=('TrackBack')
147
148    def canSetDefaultPage(self):
149        return False
150
151    def getAlwaysOnTop(self):
152        if hasattr(self, 'alwaysOnTop'):
153            if self.alwaysOnTop==None or self.alwaysOnTop==0:
154                return 0
155            else:
156                return 1
157        else:
158            return 0
159
160    def getIcon(self, relative_to_portal=0):
161        try:
162            if self.getAlwaysOnTop()==0:
163                return 'entry_icon.gif'
164            else:
165                return 'entry_pin.gif'
166        except:
167            return 'entry_icon.gif'
168
169    def listCategories(self):
170        """ Traverse upwards in the tree to collect all the available categories."""
171        cats=[]
172        parent=self.aq_parent
173        portal=self.portal_url.getPortalObject()
174
175        while parent!=portal:
176           if parent.portal_type=='Blog' or parent.portal_type=='BlogFolder':
177               pcats=parent.getCategories()
178               for c in pcats:
179                   if c not in cats:
180                       cats.append(c)
181               if parent.portal_type=='Blog':
182                   break
183           parent=parent.aq_parent
184
185        for c in self.simpleblog_tool.getGlobalCategories():
186            if not c in cats:
187                cats.append(c)
188        cats.sort()
189        return tuple(cats)
190
191    def start(self):
192        return self.getEffectiveDate()
193
194    def end(self):
195        """ Return the same data as start() since an entry is not an event but an item that is
196            published on a specific date. We want the entries in the calendar to appear on only one day.
197        """
198        return self.getEffectiveDate()
199
200    def sendTrackBack(self):
201        message = "TrackBack sent"
202        title = self.title
203        src_url = self.absolute_url()
204        blog = self.simpleblog_tool.getFrontPage(self)
205        blog_name = blog.Title()
206        excerpt=self.description
207        agent = "SimpleBlog"
208        result=[]
209        for i in self.getSendTrackBackURLs():
210            ping_url=i
211            err,mes = post_trackback(self,
212                                     ping_url=ping_url, 
213                                     title = title,
214                                     src_url = src_url,
215                                     blog_name = blog_name,
216                                     excerpt=self.description,
217                                     agent = "SimpleBlog",
218                                     charset = "utf-8")
219            result.append((err,mes))
220        result.append(("excerpt",self.description))
221        return result
222
223    def getTrackbacks(self):
224        return self.listFolderContents(spec="TrackBack")
225
226    def setEntryTag(self, value, **kwargs):
227        """ Update tags in the Entry in parent Blog """
228        value = list(value)
229        value.sort()
230        self.getField('tags').set(self, value, **kwargs)
231
232        tags = self.listTags()
233        newEntries = [v for v in value if not v in tags]
234        if not newEntries:
235            return
236        newTagsList = list(tags)+ list(newEntries)
237        parent = self.aq_parent
238        portal = self.portal_url.getPortalObject()
239        while parent != portal:
240           if parent.portal_type == 'Blog':
241               break
242           parent = parent.aq_parent
243        parent.setTags(newTagsList, **kwargs)
244
245    def listTags(self):
246        """ Get the list of Tags from parent Blog """
247        tags = []
248        parent = self.aq_parent
249        portal = self.portal_url.getPortalObject()
250        while parent != portal:
251           if parent.portal_type == 'Blog':
252               tags = parent.getTags()
253               break
254           parent = parent.aq_parent
255        return tuple(tags)
256
257    def getBody(self):
258        return self.getField('body').get(self)
259
260registerType(BlogEntry)
Note: See TracBrowser for help on using the repository browser.