1 | from Products.CMFDefault.DiscussionItem import DiscussionItemContainer, DiscussionItem |
---|
2 | from AccessControl import getSecurityManager, Unauthorized |
---|
3 | from DateTime import DateTime |
---|
4 | from Products.CMFCore.utils import getToolByName |
---|
5 | from AccessControl import ClassSecurityInfo |
---|
6 | from Globals import InitializeClass |
---|
7 | |
---|
8 | from utils import * |
---|
9 | |
---|
10 | # Patching createReply method of |
---|
11 | # Products.CMFDefault.DiscussionItem.DiscussionItemContainer |
---|
12 | def createReply( self, title, text, Creator=None, email=''): |
---|
13 | """ |
---|
14 | Create a reply in the proper place |
---|
15 | """ |
---|
16 | container = self._container |
---|
17 | |
---|
18 | id = int(DateTime().timeTime()) |
---|
19 | while self._container.get( str(id), None ) is not None: |
---|
20 | id = id + 1 |
---|
21 | id = str( id ) |
---|
22 | |
---|
23 | item = DiscussionItem( id, title=title, description=title ) |
---|
24 | item._edit( text_format='structured-text', text=text ) |
---|
25 | |
---|
26 | if Creator: |
---|
27 | if hasattr(item, 'addCreator'): |
---|
28 | item.addCreator(Creator) |
---|
29 | else: |
---|
30 | item.creator = Creator |
---|
31 | |
---|
32 | pm = getToolByName(self, 'portal_membership') |
---|
33 | |
---|
34 | if pm.isAnonymousUser(): |
---|
35 | item.manage_addProperty(id='email', value=email, type='string') |
---|
36 | |
---|
37 | item.review_state="private" |
---|
38 | |
---|
39 | item.setReplyTo( self._getDiscussable() ) |
---|
40 | self._container[ id ] = item |
---|
41 | |
---|
42 | # Control of performing moderation |
---|
43 | ifModerate = getProp(self, "enable_moderation", marker=False) |
---|
44 | if ifModerate: |
---|
45 | roles = [role['name'] for role in self.acl_users.rolesOfPermission('Moderate Discussion') |
---|
46 | if role['selected']== 'SELECTED'] |
---|
47 | roles.append('DiscussionManager') |
---|
48 | item.manage_permission('Delete objects', roles, acquire=1) |
---|
49 | item.manage_permission('View', roles, acquire=0) |
---|
50 | else: |
---|
51 | item.review_state = "published" |
---|
52 | |
---|
53 | item.__of__( self ).indexObject() |
---|
54 | return id |
---|
55 | |
---|
56 | def getReplies( self ): |
---|
57 | """ |
---|
58 | Return a sequence of the DiscussionResponse objects which are |
---|
59 | associated with this Discussable |
---|
60 | """ |
---|
61 | |
---|
62 | objects = [] |
---|
63 | a = objects.append |
---|
64 | validate = getSecurityManager().validate |
---|
65 | |
---|
66 | result_ids = self._getReplyResults() |
---|
67 | for id in result_ids: |
---|
68 | object = self._container.get( id ).__of__( self ) |
---|
69 | try: |
---|
70 | if validate(self, self, id, object): |
---|
71 | a( object ) |
---|
72 | except Unauthorized: |
---|
73 | pass |
---|
74 | return objects |
---|
75 | |
---|
76 | perms = DiscussionItemContainer.__ac_permissions__ |
---|
77 | new_perms = [] |
---|
78 | for item in perms: |
---|
79 | perm_name = item[0] |
---|
80 | funcs = item[1] |
---|
81 | if 'deleteReply' in funcs: |
---|
82 | new_perms.append( (perm_name, [f for f in funcs if f != 'deleteReply']) ) |
---|
83 | new_perms.append( ('Moderate Discussion', ('deleteReply', )) ) |
---|
84 | else: |
---|
85 | new_perms.append(item) |
---|
86 | |
---|
87 | DiscussionItemContainer.__ac_permissions__ = new_perms |
---|
88 | InitializeClass(DiscussionItemContainer) |
---|
89 | |
---|
90 | DiscussionItemContainer.createReply = createReply |
---|
91 | DiscussionItemContainer.getReplies = getReplies |
---|