##// END OF EJS Templates
action-parser: fixed filter call
super-admin -
r4970:5ee395cb default
parent child Browse files
Show More
@@ -1,357 +1,357 b''
1 1 # -*- coding: utf-8 -*-
2 2
3 3 # Copyright (C) 2010-2020 RhodeCode GmbH
4 4 #
5 5 # This program is free software: you can redistribute it and/or modify
6 6 # it under the terms of the GNU Affero General Public License, version 3
7 7 # (only), as published by the Free Software Foundation.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU Affero General Public License
15 15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 16 #
17 17 # This program is dual-licensed. If you wish to learn more about the
18 18 # RhodeCode Enterprise Edition, including its added features, Support services,
19 19 # and proprietary license terms, please see https://rhodecode.com/licenses/
20 20
21 21 import logging
22 22
23 23 from webhelpers2.html.builder import literal
24 24 from webhelpers2.html.tags import link_to
25 25
26 26 from rhodecode.lib.utils2 import AttributeDict
27 27 from rhodecode.lib.vcs.backends.base import BaseCommit
28 28 from rhodecode.lib.vcs.exceptions import CommitDoesNotExistError
29 29
30 30
31 31 log = logging.getLogger(__name__)
32 32
33 33
34 34 def action_parser(request, user_log, feed=False, parse_cs=False):
35 35 """
36 36 This helper will action_map the specified string action into translated
37 37 fancy names with icons and links
38 38
39 39 :param user_log: user log instance
40 40 :param feed: use output for feeds (no html and fancy icons)
41 41 :param parse_cs: parse Changesets into VCS instances
42 42 """
43 43 if user_log.version == 'v2':
44 44 ap = AuditLogParser(request, user_log)
45 45 return ap.callbacks()
46 46 else:
47 47 # old style
48 48 ap = ActionParser(request, user_log, feed=False, parse_commits=False)
49 49 return ap.callbacks()
50 50
51 51
52 52 class ActionParser(object):
53 53
54 54 commits_limit = 3 # display this amount always
55 55 commits_top_limit = 50 # show up to this amount of commits hidden
56 56
57 57 def __init__(self, request, user_log, feed=False, parse_commits=False):
58 58 self.user_log = user_log
59 59 self.feed = feed
60 60 self.parse_commits = parse_commits
61 61 self.request = request
62 62
63 63 self.action = user_log.action
64 64 self.action_params = ' '
65 65 x = self.action.split(':', 1)
66 66 if len(x) > 1:
67 67 self.action, self.action_params = x
68 68
69 69 def callbacks(self):
70 70 action_str = self.action_map.get(self.action, self.action)
71 71 if self.feed:
72 72 action = action_str[0].replace('[', '').replace(']', '')
73 73 else:
74 74 action = action_str[0]\
75 75 .replace('[', '<span class="journal_highlight">')\
76 76 .replace(']', '</span>')
77 77
78 78 action_params_func = _no_params_func
79 79 if callable(action_str[1]):
80 80 action_params_func = action_str[1]
81 81
82 82 # returned callbacks we need to call to get
83 83 return [
84 84 lambda: literal(action), action_params_func,
85 85 self.action_parser_icon]
86 86
87 87 @property
88 88 def action_map(self):
89 89 _ = self.request.translate
90 90 # action : translated str, callback(extractor), icon
91 91 action_map = {
92 92 'user_deleted_repo': (
93 93 _('[deleted] repository'),
94 94 None, 'icon-trash'),
95 95 'user_created_repo': (
96 96 _('[created] repository'),
97 97 None, 'icon-plus icon-plus-colored'),
98 98 'user_created_fork': (
99 99 _('[created] repository as fork'),
100 100 None, 'icon-code-fork'),
101 101 'user_forked_repo': (
102 102 _('[forked] repository'),
103 103 self.get_fork_name, 'icon-code-fork'),
104 104 'user_updated_repo': (
105 105 _('[updated] repository'),
106 106 None, 'icon-pencil icon-pencil-colored'),
107 107 'user_downloaded_archive': (
108 108 _('[downloaded] archive from repository'),
109 109 self.get_archive_name, 'icon-download-alt'),
110 110 'admin_deleted_repo': (
111 111 _('[delete] repository'),
112 112 None, 'icon-trash'),
113 113 'admin_created_repo': (
114 114 _('[created] repository'),
115 115 None, 'icon-plus icon-plus-colored'),
116 116 'admin_forked_repo': (
117 117 _('[forked] repository'),
118 118 None, 'icon-code-fork icon-fork-colored'),
119 119 'admin_updated_repo': (
120 120 _('[updated] repository'),
121 121 None, 'icon-pencil icon-pencil-colored'),
122 122 'admin_created_user': (
123 123 _('[created] user'),
124 124 self.get_user_name, 'icon-user icon-user-colored'),
125 125 'admin_updated_user': (
126 126 _('[updated] user'),
127 127 self.get_user_name, 'icon-user icon-user-colored'),
128 128 'admin_created_users_group': (
129 129 _('[created] user group'),
130 130 self.get_users_group, 'icon-pencil icon-pencil-colored'),
131 131 'admin_updated_users_group': (
132 132 _('[updated] user group'),
133 133 self.get_users_group, 'icon-pencil icon-pencil-colored'),
134 134 'user_commented_revision': (
135 135 _('[commented] on commit in repository'),
136 136 self.get_cs_links, 'icon-comment icon-comment-colored'),
137 137 'user_commented_pull_request': (
138 138 _('[commented] on pull request for'),
139 139 self.get_pull_request, 'icon-comment icon-comment-colored'),
140 140 'user_closed_pull_request': (
141 141 _('[closed] pull request for'),
142 142 self.get_pull_request, 'icon-check'),
143 143 'user_merged_pull_request': (
144 144 _('[merged] pull request for'),
145 145 self.get_pull_request, 'icon-check'),
146 146 'push': (
147 147 _('[pushed] into'),
148 148 self.get_cs_links, 'icon-arrow-up'),
149 149 'push_local': (
150 150 _('[committed via RhodeCode] into repository'),
151 151 self.get_cs_links, 'icon-pencil icon-pencil-colored'),
152 152 'push_remote': (
153 153 _('[pulled from remote] into repository'),
154 154 self.get_cs_links, 'icon-arrow-up'),
155 155 'pull': (
156 156 _('[pulled] from'),
157 157 None, 'icon-arrow-down'),
158 158 'started_following_repo': (
159 159 _('[started following] repository'),
160 160 None, 'icon-heart icon-heart-colored'),
161 161 'stopped_following_repo': (
162 162 _('[stopped following] repository'),
163 163 None, 'icon-heart-empty icon-heart-colored'),
164 164 }
165 165 return action_map
166 166
167 167 def get_fork_name(self):
168 168 from rhodecode.lib import helpers as h
169 169 _ = self.request.translate
170 170 repo_name = self.action_params
171 171 _url = h.route_path('repo_summary', repo_name=repo_name)
172 172 return _('fork name %s') % link_to(self.action_params, _url)
173 173
174 174 def get_user_name(self):
175 175 user_name = self.action_params
176 176 return user_name
177 177
178 178 def get_users_group(self):
179 179 group_name = self.action_params
180 180 return group_name
181 181
182 182 def get_pull_request(self):
183 183 from rhodecode.lib import helpers as h
184 184 _ = self.request.translate
185 185 pull_request_id = self.action_params
186 186 if self.is_deleted():
187 187 repo_name = self.user_log.repository_name
188 188 else:
189 189 repo_name = self.user_log.repository.repo_name
190 190 return link_to(
191 191 _('Pull request #%s') % pull_request_id,
192 192 h.route_path('pullrequest_show', repo_name=repo_name,
193 193 pull_request_id=pull_request_id))
194 194
195 195 def get_archive_name(self):
196 196 archive_name = self.action_params
197 197 return archive_name
198 198
199 199 def action_parser_icon(self):
200 200 tmpl = """<i class="%s" alt="%s"></i>"""
201 201 ico = self.action_map.get(self.action, ['', '', ''])[2]
202 202 return literal(tmpl % (ico, self.action))
203 203
204 204 def get_cs_links(self):
205 205 from rhodecode.lib import helpers as h
206 206 _ = self.request.translate
207 207 if self.is_deleted():
208 208 return self.action_params
209 209
210 210 repo_name = self.user_log.repository.repo_name
211 211 commit_ids = self.action_params.split(',')
212 212 commits = self.get_commits(commit_ids)
213 213
214 214 link_generator = (
215 215 self.lnk(commit, repo_name)
216 216 for commit in commits[:self.commits_limit])
217 217 commit_links = [" " + ', '.join(link_generator)]
218 218 _op1, _name1 = _get_op(commit_ids[0])
219 219 _op2, _name2 = _get_op(commit_ids[-1])
220 220
221 221 commit_id_range = '%s...%s' % (_name1, _name2)
222 222
223 223 compare_view = (
224 224 ' <div class="compare_view tooltip" title="%s">'
225 225 '<a href="%s">%s</a> </div>' % (
226 226 _('Show all combined commits %s->%s') % (
227 227 commit_ids[0][:12], commit_ids[-1][:12]
228 228 ),
229 229 h.route_path(
230 230 'repo_commit', repo_name=repo_name,
231 231 commit_id=commit_id_range), _('compare view')
232 232 )
233 233 )
234 234
235 235 if len(commit_ids) > self.commits_limit:
236 236 more_count = len(commit_ids) - self.commits_limit
237 237 commit_links.append(
238 238 _(' and %(num)s more commits') % {'num': more_count}
239 239 )
240 240
241 241 if len(commits) > 1:
242 242 commit_links.append(compare_view)
243 243 return ''.join(commit_links)
244 244
245 245 def get_commits(self, commit_ids):
246 246 commits = []
247 if not filter(lambda v: v != '', commit_ids):
247 if not [v for v in commit_ids if v != '']:
248 248 return commits
249 249
250 250 repo = None
251 251 if self.parse_commits:
252 252 repo = self.user_log.repository.scm_instance()
253 253
254 254 for commit_id in commit_ids[:self.commits_top_limit]:
255 255 _op, _name = _get_op(commit_id)
256 256
257 257 # we want parsed commits, or new log store format is bad
258 258 if self.parse_commits:
259 259 try:
260 260 commit = repo.get_commit(commit_id=commit_id)
261 261 commits.append(commit)
262 262 except CommitDoesNotExistError:
263 263 log.error(
264 264 'cannot find commit id %s in this repository',
265 265 commit_id)
266 266 commits.append(commit_id)
267 267 continue
268 268 else:
269 269 fake_commit = AttributeDict({
270 270 'short_id': commit_id[:12],
271 271 'raw_id': commit_id,
272 272 'message': '',
273 273 'op': _op,
274 274 'ref_name': _name
275 275 })
276 276 commits.append(fake_commit)
277 277
278 278 return commits
279 279
280 280 def lnk(self, commit_or_id, repo_name):
281 281 from rhodecode.lib.helpers import tooltip
282 282 from rhodecode.lib import helpers as h
283 283 _ = self.request.translate
284 284 title = ''
285 285 lazy_cs = True
286 286 if isinstance(commit_or_id, (BaseCommit, AttributeDict)):
287 287 lazy_cs = True
288 288 if (getattr(commit_or_id, 'op', None) and
289 289 getattr(commit_or_id, 'ref_name', None)):
290 290 lazy_cs = False
291 291 lbl = '?'
292 292 if commit_or_id.op == 'delete_branch':
293 293 lbl = '%s' % _('Deleted branch: %s') % commit_or_id.ref_name
294 294 title = ''
295 295 elif commit_or_id.op == 'tag':
296 296 lbl = '%s' % _('Created tag: %s') % commit_or_id.ref_name
297 297 title = ''
298 298 _url = '#'
299 299
300 300 else:
301 301 lbl = '%s' % (commit_or_id.short_id[:8])
302 302 _url = h.route_path('repo_commit', repo_name=repo_name,
303 303 commit_id=commit_or_id.raw_id)
304 304 title = tooltip(commit_or_id.message)
305 305 else:
306 306 # commit cannot be found/striped/removed etc.
307 307 lbl = ('%s' % commit_or_id)[:12]
308 308 _url = '#'
309 309 title = _('Commit not found')
310 310 if self.parse_commits:
311 311 return link_to(lbl, _url, title=title, class_='tooltip')
312 312 return link_to(lbl, _url, raw_id=commit_or_id.raw_id, repo_name=repo_name,
313 313 class_='lazy-cs' if lazy_cs else '')
314 314
315 315 def is_deleted(self):
316 316 return self.user_log.repository is None
317 317
318 318
319 319 class AuditLogParser(object):
320 320 def __init__(self, request, audit_log_entry):
321 321 self.audit_log_entry = audit_log_entry
322 322 self.request = request
323 323
324 324 def get_icon(self, action):
325 325 return 'icon-rhodecode'
326 326
327 327 def callbacks(self):
328 328 action_str = self.audit_log_entry.action
329 329
330 330 def callback():
331 331 # returned callbacks we need to call to get
332 332 action = action_str \
333 333 .replace('[', '<span class="journal_highlight">')\
334 334 .replace(']', '</span>')
335 335 return literal(action)
336 336
337 337 def icon():
338 338 tmpl = """<i class="%s" alt="%s"></i>"""
339 339 ico = self.get_icon(action_str)
340 340 return literal(tmpl % (ico, action_str))
341 341
342 342 action_params_func = _no_params_func
343 343
344 344 return [
345 345 callback, action_params_func, icon]
346 346
347 347
348 348 def _no_params_func():
349 349 return ""
350 350
351 351
352 352 def _get_op(commit_id):
353 353 _op = None
354 354 _name = commit_id
355 355 if len(commit_id.split('=>')) == 2:
356 356 _op, _name = commit_id.split('=>')
357 357 return _op, _name
General Comments 0
You need to be logged in to leave comments. Login now