[822] | 1 | import urllib, md5 |
---|
| 2 | |
---|
| 3 | from Acquisition import aq_inner |
---|
| 4 | from AccessControl import getSecurityManager |
---|
[1201] | 5 | |
---|
| 6 | from Products.CMFPlone.utils import IndexIterator |
---|
[822] | 7 | from Products.CMFPlone.utils import getToolByName |
---|
[1201] | 8 | from Products.CMFFormController.ControllerState import ControllerState |
---|
| 9 | from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile |
---|
| 10 | from quintagroup.plonecomments.utils import manage_mails |
---|
[822] | 11 | |
---|
| 12 | from plone.app.layout.viewlets import comments |
---|
[1201] | 13 | from plone.app.kss.plonekssview import PloneKSSView |
---|
[822] | 14 | |
---|
| 15 | class CommentsViewlet(comments.CommentsViewlet): |
---|
| 16 | """A custom version of the comments viewlet |
---|
| 17 | """ |
---|
| 18 | render = ViewPageTemplateFile('comments.pt') |
---|
| 19 | |
---|
| 20 | def is_moderation_enabled(self): |
---|
| 21 | """ Returns a boolean indicating whether the user has enabled moderation |
---|
| 22 | in the qPloneComments configlet |
---|
| 23 | """ |
---|
| 24 | portal_properties = getToolByName(self.context, 'portal_properties') |
---|
| 25 | try: |
---|
| 26 | return portal_properties.qPloneComments.getProperty('enable_moderation', None) |
---|
| 27 | except AttributeError: |
---|
| 28 | return False |
---|
| 29 | |
---|
| 30 | def can_moderate(self): |
---|
| 31 | """ Returns a boolean indicating whether the user has the 'Moderate Discussion' |
---|
| 32 | permission |
---|
| 33 | """ |
---|
| 34 | return getSecurityManager().checkPermission('Moderate Discussion', aq_inner(self.context)) |
---|
| 35 | |
---|
| 36 | def getGravatar(self, reply): |
---|
| 37 | purl = getToolByName(self.context, 'portal_url') |
---|
| 38 | default = purl() + '/defaultUser.gif' |
---|
| 39 | email = '' |
---|
| 40 | |
---|
| 41 | creator = reply.Creator() |
---|
| 42 | if creator and not creator=='Anonymous User': |
---|
| 43 | mtool = getToolByName(self.context, "portal_membership") |
---|
| 44 | member = mtool.getMemberById(creator) |
---|
| 45 | email = member and member.getProperty('email','') or '' |
---|
| 46 | else: |
---|
| 47 | email = reply.getProperty('email',d='') |
---|
| 48 | if not email: |
---|
| 49 | return default |
---|
| 50 | |
---|
| 51 | size = 40 |
---|
| 52 | gravatar_url = "http://www.gravatar.com/avatar.php?" |
---|
| 53 | # construct the url |
---|
| 54 | gravatar_url += urllib.urlencode({'gravatar_id':md5.md5(email).hexdigest(), |
---|
| 55 | 'default':default, 'size':str(size)}) |
---|
| 56 | |
---|
[1201] | 57 | return gravatar_url |
---|
| 58 | |
---|
| 59 | def authenticated_report_abuse_enabled(self): |
---|
| 60 | """ """ |
---|
| 61 | portal_properties = getToolByName(self.context, 'portal_properties') |
---|
| 62 | prop_sheet = portal_properties['qPloneComments'] |
---|
| 63 | value = prop_sheet.getProperty('enable_authenticated_report_abuse', False) |
---|
| 64 | return value |
---|
| 65 | |
---|
| 66 | def anonymous_report_abuse_enabled(self): |
---|
| 67 | """ """ |
---|
| 68 | portal_properties = getToolByName(self.context, 'portal_properties') |
---|
| 69 | prop_sheet = portal_properties['qPloneComments'] |
---|
| 70 | value = prop_sheet.getProperty('enable_anonymous_report_abuse', False) |
---|
| 71 | return value |
---|
| 72 | |
---|
| 73 | def ajax_report_abuse_enabled(self): |
---|
| 74 | """ """ |
---|
| 75 | portal_properties = getToolByName(self.context, 'portal_properties') |
---|
| 76 | prop_sheet = portal_properties['qPloneComments'] |
---|
| 77 | value = prop_sheet.getProperty('enable_ajax_report_abuse', False) |
---|
| 78 | return value |
---|
| 79 | |
---|
| 80 | def email_from_address(self): |
---|
| 81 | """ """ |
---|
| 82 | portal_url = getToolByName(self.context, 'portal_url') |
---|
| 83 | portal = portal_url.getPortalObject() |
---|
| 84 | return portal.email_from_address |
---|
| 85 | |
---|
| 86 | def member(self): |
---|
| 87 | """ """ |
---|
| 88 | pm = getToolByName(self.context, 'portal_membership') |
---|
| 89 | return pm.getAuthenticatedMember() |
---|
| 90 | |
---|
| 91 | def tabindex(self): |
---|
| 92 | """ Needed for BBB, tabindex has been deprecated. |
---|
| 93 | """ |
---|
| 94 | return IndexIterator() |
---|
| 95 | |
---|
| 96 | def portal_url(self): |
---|
| 97 | """ """ |
---|
| 98 | return getToolByName(self.context, 'portal_url') |
---|
| 99 | |
---|
| 100 | |
---|
| 101 | |
---|
| 102 | class CommentsKSS(PloneKSSView): |
---|
| 103 | """ Operations on the report abuse form using KSS. |
---|
| 104 | """ |
---|
| 105 | |
---|
| 106 | def submit_abuse_report(self): |
---|
| 107 | """ Send an email with the abuse report message and hide abuse report form. |
---|
| 108 | """ |
---|
| 109 | errors = {} |
---|
| 110 | context = aq_inner(self.context) |
---|
| 111 | request = context.REQUEST |
---|
| 112 | portal = getToolByName(self.context, 'portal_url').getPortalObject() |
---|
| 113 | if hasattr(context, 'captcha_validator'): |
---|
| 114 | dummy_controller_state = ControllerState( |
---|
| 115 | id='comments.pt', |
---|
| 116 | context=context, |
---|
| 117 | button='submit', |
---|
| 118 | status='success', |
---|
| 119 | errors={}, |
---|
| 120 | next_action=None,) |
---|
| 121 | # get the form controller |
---|
| 122 | controller = portal.portal_form_controller |
---|
| 123 | # send the validate script to the form controller with the dummy state object |
---|
| 124 | controller_state = controller.validate(dummy_controller_state, request, ['captcha_validator',]) |
---|
| 125 | errors.update(controller_state.errors) |
---|
| 126 | |
---|
| 127 | message = request.get('message') |
---|
| 128 | if not message: |
---|
| 129 | errors.update({'message': 'Please provide a message'}) |
---|
| 130 | |
---|
| 131 | mtool = getToolByName(self.context, "portal_membership") |
---|
| 132 | member = mtool.getAuthenticatedMember() |
---|
| 133 | comment_id = self.context.request.get('comment_id') |
---|
| 134 | ksscore = self.getCommandSet('core') |
---|
| 135 | if errors: |
---|
| 136 | html = self.macroContent('context/report_abuse_form/macros/form', |
---|
| 137 | errors=errors, |
---|
| 138 | show_form=True, |
---|
| 139 | tabindex=IndexIterator(), |
---|
| 140 | member=member, |
---|
| 141 | **request.form) |
---|
| 142 | node = ksscore.getHtmlIdSelector('span-reply-form-holder-%s' % comment_id) |
---|
| 143 | ksscore.replaceInnerHTML(node, html) |
---|
| 144 | return self.render() |
---|
| 145 | |
---|
| 146 | # report_abuse(context, context, message, comment) |
---|
| 147 | manage_mails(context, self.context, 'report_abuse') |
---|
| 148 | |
---|
| 149 | html = self.macroContent('context/report_abuse_form/macros/form', |
---|
| 150 | tabindex=IndexIterator(), |
---|
| 151 | member=member, |
---|
| 152 | **request.form) |
---|
| 153 | node = ksscore.getHtmlIdSelector('span-reply-form-holder-%s' % comment_id) |
---|
| 154 | html = '<br/><span style="color:red">You have reported this comment for abuse.</span>' |
---|
| 155 | self.commands.addCommand('remove_abuse_report_form', |
---|
| 156 | node, |
---|
| 157 | comment_id=comment_id, |
---|
| 158 | html=html) |
---|
| 159 | |
---|
| 160 | node = ksscore.getHtmlIdSelector('div-captcha-%s' % comment_id) |
---|
| 161 | html = self.macroContent('context/report_abuse_form/macros/captcha', |
---|
| 162 | **request.form) |
---|
| 163 | ksscore.replaceInnerHTML(node, html) |
---|
| 164 | return self.render() |
---|