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