1 | from zope.component import getMultiAdapter |
---|
2 | from Products.Five import BrowserView |
---|
3 | from Products.CMFCore.utils import getToolByName |
---|
4 | from Products.PythonScripts.standard import url_quote_plus |
---|
5 | from Products.CMFPlone import PloneMessageFactory as _ |
---|
6 | from Products.CMFPlone.utils import transaction_note |
---|
7 | from Products.CMFPlone import MessageFactory |
---|
8 | |
---|
9 | from quills.core.interfaces import IWeblog |
---|
10 | from quills.core.interfaces import IWeblogEntry |
---|
11 | |
---|
12 | try: |
---|
13 | from Products.qPloneComments.utils import manage_mails |
---|
14 | except ImportError: |
---|
15 | def manage_mails(reply, context, step): |
---|
16 | pass |
---|
17 | |
---|
18 | qpc_mf = MessageFactory('plonecomments') |
---|
19 | |
---|
20 | class DiscussionReply(BrowserView): |
---|
21 | """ |
---|
22 | """ |
---|
23 | |
---|
24 | def __call__(self, |
---|
25 | subject, |
---|
26 | body_text, |
---|
27 | text_format='plain', |
---|
28 | username=None, |
---|
29 | password=None): |
---|
30 | """This method is lifted almost directly from CMFPlone's |
---|
31 | discussion_reply.cpy skin script. Modifications start at the point where |
---|
32 | we try to adapt to IWeblogEntry. |
---|
33 | """ |
---|
34 | req = self.request |
---|
35 | # Get properties |
---|
36 | pp = getToolByName(self.context,'portal_properties') |
---|
37 | qPC = getattr(pp,'qPloneComments', None) |
---|
38 | |
---|
39 | if username or password: |
---|
40 | # The user username/password inputs on on the comment form were used, |
---|
41 | # which might happen when anonymous commenting is enabled. If they typed |
---|
42 | # something in to either of the inputs, we send them to 'logged_in'. |
---|
43 | # 'logged_in' will redirect them back to this script if authentication |
---|
44 | # succeeds with a query string which will post the message appropriately |
---|
45 | # and show them the result. if 'logged_in' fails, the user will be |
---|
46 | # presented with the stock login failure page. This all depends |
---|
47 | # heavily on cookiecrumbler, but I believe that is a Plone requirement. |
---|
48 | came_from = '%s?subject=%s&body_text=%s' % (req['URL'], subject, body_text) |
---|
49 | came_from = url_quote_plus(came_from) |
---|
50 | portal_url = self.context.portal_url() |
---|
51 | return req.RESPONSE.redirect( |
---|
52 | '%s/logged_in?__ac_name=%s' |
---|
53 | '&__ac_password=%s' |
---|
54 | '&came_from=%s' % (portal_url, |
---|
55 | url_quote_plus(username), |
---|
56 | url_quote_plus(password), |
---|
57 | came_from, |
---|
58 | ) |
---|
59 | ) |
---|
60 | # if (the user is already logged in) or (if anonymous commenting is enabled and |
---|
61 | # they posted without typing a username or password into the form), we do |
---|
62 | # the following |
---|
63 | mtool = getToolByName(self.context, 'portal_membership') |
---|
64 | creator = mtool.getAuthenticatedMember().getId() |
---|
65 | |
---|
66 | ############################## |
---|
67 | # qPloneComments INJECTION |
---|
68 | ############################## |
---|
69 | requireEmail = False |
---|
70 | if qPC: |
---|
71 | requireEmail = qPC.getProperty('require_email', False) |
---|
72 | if requireEmail: |
---|
73 | if mtool.isAnonymousUser(): |
---|
74 | email = self.request.get('email', '') |
---|
75 | else: |
---|
76 | email = mtool.getAuthenticatedMember().getProperty('email') |
---|
77 | |
---|
78 | isForAnonymous = pp['qPloneComments'].getProperty('enable_anonymous_commenting', False) |
---|
79 | comment_creator = req.get('Creator', None) |
---|
80 | if isForAnonymous and comment_creator: |
---|
81 | # Get entered anonymous name |
---|
82 | creator = comment_creator |
---|
83 | |
---|
84 | dtool = getToolByName(self.context, 'portal_discussion') |
---|
85 | tb = dtool.getDiscussionFor(self.context) |
---|
86 | if requireEmail: |
---|
87 | id = tb.createReply(title=subject, text=body_text, Creator=creator, email=email) |
---|
88 | else: |
---|
89 | id = tb.createReply(title=subject, text=body_text, Creator=creator) |
---|
90 | reply = tb.getReply(id) |
---|
91 | |
---|
92 | # Add website property to reply |
---|
93 | website = req.get('website', '').strip() |
---|
94 | if website: |
---|
95 | if not website.startswith('http://'): |
---|
96 | website = 'http://' + website |
---|
97 | reply.manage_addProperty(id='website', value=website, type='string') |
---|
98 | |
---|
99 | # TODO THIS NEEDS TO GO AWAY! |
---|
100 | if hasattr(dtool.aq_explicit, 'cookReply'): |
---|
101 | dtool.cookReply(reply, text_format='plain') |
---|
102 | parent = tb.aq_parent |
---|
103 | |
---|
104 | ############################## |
---|
105 | # qPloneComments INJECTION |
---|
106 | ############################## |
---|
107 | if qPC: |
---|
108 | # Send notification e-mail |
---|
109 | ifModerate = pp['qPloneComments'].getProperty('enable_moderation', False) |
---|
110 | manage_mails(reply, self.context, 'aproving') |
---|
111 | if not ifModerate: |
---|
112 | manage_mails(reply, self.context, 'publishing') |
---|
113 | |
---|
114 | # Inform user about awaiting moderation |
---|
115 | portal_status_message=qpc_mf(u'Comment published.') |
---|
116 | if ifModerate and reply: |
---|
117 | portal_status_message=qpc_mf(u'Currently, all comments require ' \ |
---|
118 | 'approval before being published. Please check back later.') |
---|
119 | self.context.plone_utils.addPortalMessage(portal_status_message) |
---|
120 | |
---|
121 | # return to the discussable object. |
---|
122 | obj = self.context.plone_utils.getDiscussionThread(tb)[0] |
---|
123 | try: |
---|
124 | entry = IWeblogEntry(obj).__of__(self.context.aq_inner.aq_parent) |
---|
125 | # Check for the existence of a parent weblog to see if `obj' should |
---|
126 | # be treated as having an archive url. |
---|
127 | if IWeblog.providedBy(entry.getWeblog()): |
---|
128 | weview = getMultiAdapter((entry, self.request), |
---|
129 | name=u'weblogentry_view') |
---|
130 | base = weview.getArchiveURLFor(entry) |
---|
131 | except TypeError: |
---|
132 | base = obj.getTypeInfo().getActionInfo('object/view', |
---|
133 | obj)['url'] |
---|
134 | anchor = reply.getId() |
---|
135 | from Products.CMFPlone.utils import transaction_note |
---|
136 | transaction_note('Added comment to %s at %s' % (parent.title_or_id(), |
---|
137 | reply.absolute_url())) |
---|
138 | self.context.plone_utils.addPortalMessage(_(u'Comment added.')) |
---|
139 | target = '%s#%s' % (base, anchor) |
---|
140 | return req.RESPONSE.redirect(target) |
---|