source: products/quintagroup.plonecomments/trunk/quintagroup/plonecomments/patch.py @ 3116

Last change on this file since 3116 was 3116, checked in by kroman0, 13 years ago

Pyflakes and pep8 fixes #3

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