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