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