source: products/qPloneComments/branches/plone3_extend/patch.py @ 1591

Last change on this file since 1591 was 88, checked in by chervol, 14 years ago

added message string in configlet

  • Property svn:eol-style set to native
File size: 2.8 KB
Line 
1from DateTime import DateTime
2from Globals import InitializeClass
3from AccessControl import Unauthorized
4from AccessControl import getSecurityManager
5from Products.CMFCore.utils import getToolByName
6from Products.CMFDefault.DiscussionItem import DiscussionItem
7from Products.CMFDefault.DiscussionItem import DiscussionItemContainer
8
9from Products.qPloneComments.utils import getProp
10
11# Patching createReply method of
12# Products.CMFDefault.DiscussionItem.DiscussionItemContainer
13def createReply(self, title, text, Creator=None, email=''):
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 += 1
21    id = str(id)
22
23    item = DiscussionItem(id, title=title, description=title)
24
25    if Creator:
26        if getattr(item, 'addCreator', None) is not None:
27            item.addCreator(Creator)
28        else:
29            item.creator = Creator
30
31    self._container[id] = item
32    item = item.__of__(self)
33
34    item.setFormat('structured-text')
35    item._edit(text)
36
37    pm = getToolByName(self, 'portal_membership')
38    if pm.isAnonymousUser():
39        item.manage_addProperty(id='email', value=email, type='string')
40
41    item.review_state = 'private'
42
43    # Control of performing moderation
44    if getProp(self, 'enable_moderation', marker=False):
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.setReplyTo(self._getDiscussable())
54    item.indexObject()
55
56    return id
57
58def getReplies( self ):
59    """Return a sequence of the DiscussionResponse objects which are
60       associated with this Discussable.
61    """
62    objects = []
63    validate = getSecurityManager().validate
64
65    result_ids = self._getReplyResults()
66    for id in result_ids:
67        comment = self._container.get(id).__of__(self)
68        try:
69            if validate(self, self, id, comment):
70                objects.append(comment)
71        except Unauthorized:
72            pass
73    return objects
74
75perms = DiscussionItemContainer.__ac_permissions__
76new_perms = []
77for item in perms:
78    perm_name = item[0]
79    funcs = item[1]
80    if 'deleteReply' in funcs:
81        new_perms.append((perm_name, [f for f in funcs if f != 'deleteReply']))
82        new_perms.append(('Moderate Discussion', ('deleteReply',)))
83    else:
84        new_perms.append(item)
85
86DiscussionItemContainer.__ac_permissions__ = new_perms
87InitializeClass(DiscussionItemContainer)
88
89DiscussionItemContainer.createReply = createReply
90DiscussionItemContainer.getReplies = getReplies
Note: See TracBrowser for help on using the repository browser.