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