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