source: trunk/BlogPost.py @ 450

Revision 450, 7.3 KB checked in by jukka, 13 years ago (diff)

Fixed #511, spent 2h.

Line 
1# Copyright 2006 by the LeMill Team (see AUTHORS)
2#
3# This file is part of LeMill.
4#
5# LeMill is free software; you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation; either version 2 of the License, or
8# (at your option) any later version.
9#
10# LeMill is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with LeMill; if not, write to the Free Software
17# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
19from SharedMetadata import age_group, description, tags
20from Products.Archetypes.public import *
21from Products.ATReferenceBrowserWidget.ATReferenceBrowserWidget import ReferenceBrowserWidget
22from Products.CMFCore.permissions import ModifyPortalContent
23from Products.Archetypes.public import BaseSchema, BaseContent, registerType
24from Products.Archetypes.atapi import DisplayList
25from Globals import InitializeClass
26from Products.CMFCore.utils import getToolByName
27from AccessControl import ClassSecurityInfo, Unauthorized
28from config import PROJECTNAME, MODIFY_CONTENT, VIEW
29from SharedMetadata import *
30from Products.Archetypes.atapi import DisplayList
31
32
33# Same thing as with MemberFolder but easier: MemberBlogs contain the values of groups itself in indexable storage.
34
35schema = BaseSchema + bodytext + tags + coverImage + Schema((
36    StringField('category',
37        vocabulary='getCategories',
38        index = 'FieldIndex:schema',
39        enforceVocabulary=True,
40        default='blog post',
41        widget=SelectionWidget(
42             label = 'Category',
43             label_msgid = 'label_post_category',
44             description = "Choose category for post. Collaboration proposals are invitations that  are visible for anyone in Community page.",
45             description_msgid = 'help_post_category',
46             i18n_domain = "lemill"
47             ),
48    ),           
49
50    TagsField('tags',
51        schemata='default'
52        ),
53))
54
55#    StringField('description',
56#        default_method=None,
57#        widget=TextAreaWidget(
58#           visible={'view':'invisible','edit':'invisible'}       
59#        )
60#    ),
61#
62
63
64
65class BlogPost(BaseContent):
66
67    meta_type = "BlogPost"
68    archetype_name = "BlogPost" 
69    allow_discussion = True
70    _at_rename_after_creation = True
71
72    security = ClassSecurityInfo()
73    security.declareObjectPublic()
74    schema = schema
75
76    def manage_afterAdd(self, item, container):
77        """1) Replaces the left side portlets with the content type's own action portlet. 2) add obj id to list of recent entries in parent GroupBlog. 3) add to list of collaboration proposals if such"""
78        BaseContent.manage_afterAdd(self, item, container)
79        self.content_status_modify(workflow_action='publish')
80        if not hasattr(item.aq_base, 'left_slots'):
81            self._setProperty('left_slots', ['here/portlet_groupblog_actions/macros/portlet',], 'lines')
82        if hasattr(container, 'recent_posts'):
83            container.addRecent_post(self.getId())
84        cat=self.getCategory()
85        uid=self.UID()
86        my_id=self.getId()
87        try:
88            proposals = self.community.getCollaboration_proposals()
89        except:
90            proposals = [uid]
91        if cat.lower()=='collaboration proposal' and uid not in proposals and not my_id.startswith('blogpost.'):
92            self.community.addCollaboration_proposal(uid)
93        mtool = getToolByName(self, 'portal_membership')
94        memberfolder=mtool.getHomeFolder()
95        if memberfolder!=None:
96            memberfolder.note_action(item.UID(), item.portal_type, 'afterAdd')
97
98
99
100    def at_post_edit_script(self):
101        """ Rename id to resemble title, add to list of collaboration proposals of community if such, and add to recent posts of blog. """
102        old=self.getId()
103        cat=self.getCategory()
104        uid=self.UID()
105        self._renameAfterCreation()
106        new=self.getId()
107
108        try:
109            proposals = self.community.getCollaboration_proposals()
110        except:
111            proposals = [uid]
112        if cat.lower()=='collaboration proposal' and uid not in proposals and not new.startswith('blogpost.'):
113            self.community.addCollaboration_proposal(uid)
114        if old != new: # new id is updated to recent posts list
115            try: # if for some reason posts are not inside blogs
116                blog=self.getBlog()
117                blog.replaceRecent_post(old, new)
118            except:
119                pass
120        mtool = getToolByName(self, 'portal_membership')
121        memberfolder=mtool.getHomeFolder()
122        if memberfolder!=None:
123            memberfolder.note_action(self.UID(), self.portal_type, 'post_edit')
124
125               
126
127    def getCategories(self):
128        blog=self.getBlog()
129        return blog.getCategories()
130               
131
132    def getCoverImageURL(self):
133        """BlogPosts shouldn't have their own cover images -- returns authors portrait."""
134        mtool   = getToolByName(self, 'portal_membership')
135        portrait = mtool.getPersonalPortrait(self.Creator)
136        return portrait.absolute_url()
137       
138    def NiceName(self):
139        """ I don't think this will get called but just to make sure. """
140        return self.getTitle()
141
142    def isPost(self):
143        return True
144
145    def delete_post(self):
146        """ Hide, remove from recent posts and remove from Collaboration proposals """
147        container=self.getBlog()
148        uid=self.UID()
149        self.content_status_modify(workflow_action='hide')
150        if hasattr(container, 'recent_posts'):
151            container.removeRecent_post(self.getId())
152        try:
153            proposals = self.community.getCollaboration_proposals()
154        except:
155            proposals = [uid]
156        if self.getCategory().lower()=='collaboration proposal' and uid not in proposals:
157            self.community.addCollaboration_proposal(uid)
158
159    def undelete_post(self):
160        """ Bring back post but currently doesn't add to collaboration proposals or recent posts """
161        self.content_status_modify(workflow_action='publish')
162
163   
164#    def setProperties(self, newprops=None, **kw):
165#        """ just a helper method """
166#        if newprops is None:
167#            newprops = kw
168#        for (key, value) in newprops.items():
169#            accessor='get'+str(key.capitalize())
170#            mutator='set'+str(key.capitalize())
171#
172#            if hasattr(self, accessor):
173#                if eval('self.'+accessor+'()') != value:
174#                    eval('self.'+mutator+'(value)')
175
176    def setCoverImage(self, value, **kwargs):
177        """ This modification for a mutator flags object to have a coverImage """
178        cover=self.getField('coverImage')
179        cover.set(self,value,**kwargs)
180        has_cover=self.getField('hasCoverImage')
181        has_cover.set(self,True)
182
183    def setBodyText(self, value, **kwargs):
184        bt=self.getField('bodyText')
185        bt.set(self,value,**kwargs)
186        desc=self.getField('description')
187        if len(value)>200:
188            desc.set(self, '--More--'.join((value[:500],'')), **kwargs)
189        else:
190            desc.set(self, value, **kwargs)
191   
192
193
194
195
196registerType(BlogPost, PROJECTNAME)
Note: See TracBrowser for help on using the repository browser.