[822] | 1 | from DateTime import DateTime |
---|
[1625] | 2 | try: |
---|
| 3 | from App.class_init import InitializeClass |
---|
[3107] | 4 | InitializeClass |
---|
[1625] | 5 | except ImportError: |
---|
| 6 | from Globals import InitializeClass |
---|
[822] | 7 | from AccessControl import Unauthorized |
---|
| 8 | from AccessControl import getSecurityManager |
---|
| 9 | from Products.CMFCore.utils import getToolByName |
---|
| 10 | from Products.CMFDefault.DiscussionItem import DiscussionItem |
---|
| 11 | from Products.CMFDefault.DiscussionItem import DiscussionItemContainer |
---|
| 12 | from quintagroup.plonecomments.utils import getProp |
---|
| 13 | |
---|
[2137] | 14 | |
---|
[822] | 15 | def 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()) |
---|
[3102] | 21 | while container.get(str(id), None) is not None: |
---|
[822] | 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 | |
---|
[3102] | 33 | container[id] = item |
---|
[822] | 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): |
---|
[2137] | 47 | roles = [role['name'] |
---|
| 48 | for role in self.acl_users.rolesOfPermission('Moderate Discussion') |
---|
| 49 | if role['selected'] == 'SELECTED'] |
---|
[822] | 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 | |
---|
[2137] | 61 | |
---|
| 62 | def getReplies(self): |
---|
[822] | 63 | """Return a sequence of the DiscussionResponse objects which are |
---|
| 64 | associated with this Discussable. |
---|
| 65 | """ |
---|
| 66 | objects = [] |
---|
| 67 | validate = getSecurityManager().validate |
---|
| 68 | |
---|
| 69 | result_ids = self._getReplyResults() |
---|
| 70 | for id in result_ids: |
---|
| 71 | comment = self._container.get(id).__of__(self) |
---|
| 72 | try: |
---|
| 73 | if validate(self, self, id, comment): |
---|
| 74 | objects.append(comment) |
---|
| 75 | except Unauthorized: |
---|
| 76 | pass |
---|
| 77 | return objects |
---|
| 78 | |
---|
| 79 | perms = DiscussionItemContainer.__ac_permissions__ |
---|
| 80 | new_perms = [] |
---|
| 81 | for item in perms: |
---|
| 82 | perm_name = item[0] |
---|
| 83 | funcs = item[1] |
---|
| 84 | if 'deleteReply' in funcs: |
---|
| 85 | new_perms.append((perm_name, [f for f in funcs if f != 'deleteReply'])) |
---|
| 86 | new_perms.append(('Moderate Discussion', ('deleteReply',))) |
---|
| 87 | else: |
---|
| 88 | new_perms.append(item) |
---|
| 89 | |
---|
| 90 | DiscussionItemContainer.__ac_permissions__ = new_perms |
---|
| 91 | InitializeClass(DiscussionItemContainer) |
---|
| 92 | |
---|
| 93 | DiscussionItemContainer.createReply = createReply |
---|
| 94 | DiscussionItemContainer.getReplies = getReplies |
---|