Attachment 'RecommendPage-1.3.5-8.py'
Download 1 # -*- coding: iso-8859-1 -*-
2 """
3 MoinMoin - RecommendPage Action Macro
4
5 PURPOSE:
6 This macro is used to recommend a page to an other wiki user.
7
8 CALLING SEQUENCE:
9 http://localhost:8080/WikiName?action=RecommendPage
10
11 PROCEDURE:
12 You get an input mask to enter the username of the one where you like to send the recommendation. This is then stored on a page named WikiName/RecommendedPage as an wiki itemlist with the link and the first five lines in plain text. At the end your user SIG is written.
13 A new entry is always written on top of the page.
14 The person is informed by an email notification about the changes
15
16 If you don't enter a name the recommendation is done to your name
17
18 Please remove the version number from the filename.
19
20 MODIFICATION HISTORY:
21 @copyright: 2005 by Reimar Bauer (R.Bauer@fz-juelich.de)
22 @license: GNU GPL, see COPYING for details.
23 Version: 1.3.4-1
24
25 2005-04-19 : Version 1.3.4-2
26 acl line on top of recommendation is set for a valid user/RecommendedPage to
27 WikiName:admin,read,write,delete All:read,write
28 otherwise for All:read,write
29 If the page user/RecommendedPage already exist acl right given on that page are used.
30 output changed similiar to the search output.
31
32 2005-04-21 : Version 1.3.4-3
33 output changed into calling ShortText()
34 e.g. * ["RulesForUnzip"] [[ShortText(RulesForUnzip)]] ... @SIG@
35 it is also checked by now if acls are enabled in the config
36 2005-09-02 : Version 1.3.5-4
37 from SubscribeTo by Raphael Bossek learned to add an subscription for the email notification for a user before the new recommendation is written to the file. This means the user gots informed by an email.
38 2005-09-05 : Version 1.3.5-5
39 isSubscribedTo from user replaced because it could not destinguish between subpage and page
40 my implementation does not understand regular expressions at the moment so this gives by using regular expressions
41 one extraline in the config.
42 2005-09-07 : Version 1.3.5-6
43 text box changed to a selection menu for the user names (sorted by name)
44 2005-09-08 : Version 1.3.5-7
45 bug fix: spaces in html are different in browsers
46 in some browser no selction did not give the user name
47 2005-10-31 : Version 1.3.5-8
48 multiple selection added
49 no acl rights are set on a new recomendation page acls on an existing recommendation page not overwritten
50 non admin users could use this function too.
51 code changed by a version request to be useable with 1.5.0 too
52
53 """
54
55 import string
56 from MoinMoin import config, wikiutil, user, wikiacl, search, version
57 from MoinMoin.Page import Page
58 from MoinMoin.PageEditor import PageEditor
59
60
61 def ACLparse(request, body):
62 """
63 taken from wikiacl and simply changed to return acl text and body without acl definition
64 renamed from parseACL to ACLparse
65 """
66
67 if not request.cfg.acl_enabled:
68 return AccessControlList(request),body
69
70
71 acl_lines = []
72 while body and body[0] == '#':
73 # extract first line
74 try:
75 line, body = body.split('\n', 1)
76 except ValueError:
77 line = body
78 body = ''
79
80 # end parsing on empty (invalid) PI
81 if line == "#":
82 break
83
84 # skip comments (lines with two hash marks)
85 if line[1] == '#':
86 continue
87
88 tokens = line.split(None, 1)
89 if tokens[0].lower() == "#acl":
90 if len(tokens) == 2:
91 args = tokens[1].rstrip()
92 else:
93 args = ""
94 acl_lines.append(args)
95 return acl_lines,body
96
97 def RecommendPage(request,pagename,username):
98
99 if version.release < '1.5.0' :
100 #delimiter = "/" 1.5
101 if config.allow_subpages:
102 delimiter = "/"
103 else:
104 delimiter = ""
105 else:
106 delimiter = "/"
107
108 err = ""
109 name = "%(username)s%(delimiter)sRecommendedPage" % {"username": username, "delimiter": delimiter}
110 page = PageEditor(request,name)
111 if request.user.may.write(name):
112
113 if user.getUserId(request, username) != None:
114 uid = user.getUserId(request, username)
115 recom_user = user.User (request, id = uid)
116
117 subscription_list = recom_user.getSubscriptionList()
118 isSubscribedTo = 0
119 for test in subscription_list :
120 if test == name :
121 isSubsribedTo = 1
122
123 if isSubscribedTo == 0:
124 if version.release < '1.5.0' :
125 recom_user.subscribePage(name)
126 else:
127 recom_user.subscribe(name)
128 recom_user.save()
129
130 newtext = u" * %(pagename)s %(about)s ...\n %(username)s\n" % {
131 "pagename": '["'+pagename+'"]',
132 "about":"[[ShortText(%(pagename)s)]]" % {
133 "pagename": pagename},
134 "username":"@SIG@"}
135
136 rev = page.current_rev()
137 if not page.exists() :
138
139 #if request.cfg.acl_enabled:
140 # if user.getUserId(request, username) != None :
141 # acl="#acl %(username)s:read,write,delete All:read,write\n" % {
142 # "username":username}
143 # else:
144 # acl="#acl All:read,write\n"
145 #else:
146 # acl=""
147 PageEditor.saveText(page,newtext,rev)
148
149 else:
150 body = page.get_raw_body()
151 given_acl,body = ACLparse(request, body)
152
153 if len(string.join(given_acl,"")) > 0:
154 acl = "#acl %(given_acl)s \n" % {
155 "given_acl":string.join(given_acl,"\n")}
156 else:
157 acl=""
158
159 PageEditor.saveText(page,acl+newtext+body,rev)
160
161
162 else:
163 err = "Can not write"
164 return err
165
166
167
168
169 def execute(pagename, request):
170
171
172 _ = request.getText
173 actname = __name__.split('.')[-1]
174
175 if request.user.may.read(pagename) :
176
177 thispage = Page(request,pagename)
178
179 if request.form.has_key('button') and request.form.has_key('ticket'):
180 # check whether this is a valid recommention request (make outside
181 # attacks harder by requiring two full HTTP transactions)
182 if not wikiutil.checkTicket(request.form['ticket'][0]) :
183 return thispage.send_page(request,
184 msg = _('Please use the interactive user interface to recommend pages!'))
185
186 selected_users = request.form.get('username', [u''])
187 good = []
188 bad = []
189 for username in selected_users:
190 # username = request.form.get('username', [u''])[0]
191 i = 0
192 if len(username.strip()) == 0 :
193 username = request.user.name
194 selected_users[i] = username
195 i += 1
196
197 err = RecommendPage(request,pagename,username)
198 if err == "" :
199 good.append(username)
200 else :
201 bad.append(username)
202
203 msg = "recommended to read %(pagename)s to %(username)s" % {
204 "pagename": pagename,
205 "username":string.join(good)}
206
207 msg_bad=''
208 if len(bad) > 0 :
209 msg_bad = "\nCAN NOTrecommend page to read to %(username)s" % {
210 "username":string.join(bad)}
211
212
213
214 Page(request, pagename).send_page(request, msg=msg+msg_bad)
215 return
216 users = user.getUserList(request)
217 html=[]
218 for uid in users:
219 name = user.User(request, id=uid).name
220 html.append("<OPTION>%(name)s</OPTION>" % { "name":name})
221
222 html.sort()
223 n=len(html)
224 if n > 3 :
225 n = 3
226
227 formhtml = '''
228 <form method="post" >
229 <strong>%(querytext)s</strong><BR>
230 <select name="username" size="%(len)s" multiple>
231 %(option)s
232 </select>
233 <input type="hidden" name="action" value="%(actname)s">
234 <input type="submit" name="button" value="%(button)s">
235
236 <BR>
237 (no selection recommends to: %(user)s)
238
239 <input type="hidden" name="ticket" value="%(ticket)s">
240 <p>
241 </form>''' % {
242 'querytext': 'Recommend page to',
243 'actname': 'RecommendPage',
244 'ticket' : wikiutil.createTicket(),
245 'option': string.join(html),
246 'user' : request.user.name,
247 'len': n,
248 'button': 'Recommend'}
249
250
251 return thispage.send_page(request, msg=formhtml)
Attached Files
To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.You are not allowed to attach a file to this page.