Attachment 'RecommendPage-1.8.2-12.py'
Download 1 # -*- coding: utf-8 -*-
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 Syntax:
9 http://localhost:8080/WikiName?action=RecommendPage
10
11 PROCEDURE:
12 You get two lists, one with groups and one with users of those where you can
13 send the recommendation.
14 This is then stored on a page named WikiName/RecommendedPage as an wiki
15 itemlist with the link and the first five lines in plain text.
16 At the end your user SIG is written.
17 A new entry is always written on top of the page.
18 The persons are informed by an email notification about the changes.
19 If you don't select anything the recommendation is done to your name.
20 Please remove the version number from the filename.
21
22 MODIFICATION HISTORY:
23 @copyright: 2005 by MoinMoin:ReimarBauer
24 2009 by Anita Ludermann [anita(at)ludermann(dot)de]
25 2005-04-21 : Version 1.3.4-3
26 output changed into calling ShortText()
27 e.g. * ["RulesForUnzip"] [[ShortText(RulesForUnzip)]] ... @SIG@
28 it is also checked by now if acls are enabled in the config
29 2005-09-02 : Version 1.3.5-4
30 from SubscribeTo by Raphael Bossek learned to add an
31 subscription for the email notification for a user before
32 the new recommendation is written to the file.
33 This means the user gots informed by an email.
34 2005-09-05 : Version 1.3.5-5
35 isSubscribedTo from user replaced because it could not
36 destinguish between subpage and page. My implementation
37 does not understand regular expressions at the moment so
38 this gives by using regular expressions one extraline
39 in the config.
40 2005-09-07 : Version 1.3.5-6
41 text box changed to a selection menu for the user names (sorted by name)
42 2005-09-08 : Version 1.3.5-7
43 bug fix: spaces in html are different in browsers
44 in some browser no selction did not give the user name
45 2005-10-31 : Version 1.3.5-8
46 multiple selection added
47 no acl rights are set on a new recomendation page acls on
48 an existing recommendation page not overwritten
49 non admin users could use this function too.
50 code changed by a version request to be useable
51 with 1.5.0, too
52 2006-02-03 : Version 1.5.1-9 1.3 compatible code removed
53 bug with wrong counted bad user fixed
54 2008-01-11 : Version 1.6.0-10 refactored for 1.6
55 2009-04-01 : Extended for groups by Anita Ludermann
56
57 """
58 # Import the required modules
59 from MoinMoin import wikidicts, wikiutil, user
60 from MoinMoin.Page import Page
61 from MoinMoin.PageEditor import PageEditor
62
63 def RecommendPage(request, pagename, username):
64 '''
65 Function: to produce the recommended pages
66 @param request: the request object
67 @param pagename: name of the page which should be recommended
68 @param username: name of the user who gets the recommend
69 '''
70 err = None
71 name = "%(username)s/RecommendPage" % {"username": username}
72 page = PageEditor(request, name)
73 # If the current user may write on this page, he can recommend it
74 if request.user.may.write(name):
75 if user.getUserId(request, username) is not None:
76 uid = user.getUserId(request, username)
77 recom_user = user.User (request, id=uid)
78
79 subscription_list = recom_user.getSubscriptionList()
80 isSubscribedTo = 0
81 for test in subscription_list:
82 if test == name:
83 isSubsribedTo = 1
84
85 if isSubscribedTo == 0:
86 recom_user.subscribe(name)
87 recom_user.save()
88
89 newtext = u" * %(pagename)s]] <<ShortText(%(pagename)s)>> ...\n %(username)s\n" % {
90 "pagename": pagename,
91 "username": "@SIG@"}
92 # If the page does not exist yet, the user may make it.
93 if not page.exists():
94 PageEditor.saveText(page, newtext, 0)
95 # If it already exists:
96 else:
97 body = page.get_data()
98 meta = page.get_meta()
99 text_meta = ''
100 for command, attrib in meta:
101 text_meta = '#%s %s\n%s' % (command, attrib, text_meta)
102
103 text = "%s\n%s\n%s\n" % (text_meta, newtext, body)
104 PageEditor.saveText(page, text, 0)
105 # The user is not allowed to write on this page:
106 else:
107 err = "Can not write"
108 return err
109
110 def select_users(request, pagename, selected_groups, selected_users):
111 '''
112 Function: to get a list of all users who get the RecommendPage without doublication
113 @param request: the request object
114 @param pagename: name of the page which should be recommended
115 @param selected_groups: all selected groups
116 @param selected_users: all individually selected users
117 '''
118 # Search for all users in all selected groups
119 for groupnames in selected_groups:
120 user_group = wikidicts.Group(request, groupnames)
121
122 # Saving users in list "selected_users"
123 for name, valid in user_group.items():
124 # Every user gets only ONE recommend
125 uid = user.getUserId(request, name)
126 user_object = user.User(request, uid)
127 if user_object.exists():
128 if valid and not name in selected_users:
129 selected_users.append(name)
130 selected_users = [name for name in selected_users if name != u'']
131 return selected_users
132
133 def execute(pagename, request):
134 '''
135 action dispatcher.
136 @param request: the request object
137 @param pagename: name of the page which should be recommended
138 '''
139 _ = request.getText
140 actname = __name__.split('.')[-1]
141 # Is the user registered and may read the page?
142 if request.user.valid and request.user.may.read(pagename):
143 thispage = Page(request, pagename)
144 if request.form.has_key('button') and request.form.has_key('ticket'):
145 if not wikiutil.checkTicket(request, request.form['ticket'][0]):
146
147 return thispage.send_page(msg=_('Please use the interactive user interface to recommend pages!'))
148
149 # The selected users are shown
150 selected_groups = request.form.get('groupname', [u''])
151 good = []
152
153 selected_users = []
154 selected_users = request.form.get('username', [u''])
155 good = []
156
157 if selected_users[0] != u'':
158 for username in selected_users:RecentChanges
159 i = 0
160 if len(username.strip()) == 0:
161 username = request.user.name
162 selected_users[i] = username
163 i += 1
164 elif selected_groups[0] == u'' and selected_users[0] == u'':
165 selected_users = [request.user.name]
166
167 # Members of each group will be stored and reviewed on duplication
168 if selected_groups[0] != u'':
169 addressees = select_users(request, pagename, selected_groups, selected_users)
170 else:
171 addressees = selected_users
172
173 for username in addressees:
174 uid = user.getUserId(request, username)
175 user_object = user.User(request, uid)
176 if user_object.may.read(pagename):
177 err = RecommendPage(request, pagename, username)
178 if err is None:
179 good.append(username)
180
181 # "Good List" -> page is sent to this users
182 msg = "recommended to read %(pagename)s to %(this_user)s" % {
183 "pagename": pagename,
184 "this_user": ' '.join(good)}
185
186 request.theme.add_msg(msg, "info")
187 thispage.send_page()
188 return
189
190 # All groups are shown
191 isgroup = request.cfg.cache.page_group_regexact.search
192 groupname = request.rootpage.getPageList(user='', filter=isgroup)
193 html_group = []
194
195 for group in groupname:
196 html_group.append("<OPTION>%(name)s</OPTION>" % {"name": group})
197 # The list with groups depents on the group's strength
198 html_group.sort()
199 n = min([3, len(html_group)])
200
201 # All users are shwon
202 users = user.getUserList(request)
203 html_user = []
204 for uid in users:
205 name = user.User(request, id=uid).name
206 uid = user.getUserId(request, name)
207 user_object = user.User(request, uid)
208 if user_object.may.read(pagename):
209 html_user.append("<OPTION>%(name)s</OPTION>" % {"name": name})
210
211 # The list with users depents on the user's strength
212 html_user.sort()
213 n = min([3, len(html_user)])
214
215 formhtml = '''
216
217 <table class="dialog">
218 <tr><td><form method="post" >
219 <strong>%(querytext)s</strong>
220 <BR>
221 </td></tr><tr><td>
222 <select name="groupname" size="%(len)s" multiple>has_key
223 %(option_group)s
224 </select>
225 </td><td>
226 <select name="username" size="%(len)s" multiple>
227 %(option_user)s
228 </select>
229 </td><td>
230 <input type="hidden" name="action" value="%(actname)s">
231 <input type="submit" name="button" value="%(button)s"></td></tr>
232 <tr><td><BR>
233 (no selection recommends to: %(user)s)
234 <input type="hidden" name="ticket" value="%(ticket)s">
235 <p></td></tr></table>
236 </form>''' % {
237 # The Variables are determined
238 'querytext': 'Recommend page to',
239 'actname': 'RecommendPage',
240 'ticket': wikiutil.createTicket(request),
241 'option_group': ' '.join(html_group),
242 'option_user': ' '.join(html_user),
243 'user': request.user.name,
244 'len': n,
245 'button': 'Recommend'}
246 # Page is sent
247 request.theme.add_msg(formhtml, "info")
248 thispage.send_page()
249 return
250
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.