Mercurial > kallithea
annotate rhodecode/controllers/journal.py @ 1060:97ed582ea86e beta
permission fixes for forking
author | Marcin Kuzminski <marcin@python-works.com> |
---|---|
date | Wed, 16 Feb 2011 12:18:18 +0100 |
parents | 3fc9183e05dd |
children | 3fe3285868d1 |
rev | line source |
---|---|
861
fd2ea6ceadc8
updated docs on every controller
Marcin Kuzminski <marcin@python-works.com>
parents:
793
diff
changeset
|
1 # -*- coding: utf-8 -*- |
fd2ea6ceadc8
updated docs on every controller
Marcin Kuzminski <marcin@python-works.com>
parents:
793
diff
changeset
|
2 """ |
fd2ea6ceadc8
updated docs on every controller
Marcin Kuzminski <marcin@python-works.com>
parents:
793
diff
changeset
|
3 rhodecode.controllers.journal |
fd2ea6ceadc8
updated docs on every controller
Marcin Kuzminski <marcin@python-works.com>
parents:
793
diff
changeset
|
4 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
fd2ea6ceadc8
updated docs on every controller
Marcin Kuzminski <marcin@python-works.com>
parents:
793
diff
changeset
|
5 |
fd2ea6ceadc8
updated docs on every controller
Marcin Kuzminski <marcin@python-works.com>
parents:
793
diff
changeset
|
6 Journal controller for pylons |
fd2ea6ceadc8
updated docs on every controller
Marcin Kuzminski <marcin@python-works.com>
parents:
793
diff
changeset
|
7 |
fd2ea6ceadc8
updated docs on every controller
Marcin Kuzminski <marcin@python-works.com>
parents:
793
diff
changeset
|
8 :created_on: Nov 21, 2010 |
fd2ea6ceadc8
updated docs on every controller
Marcin Kuzminski <marcin@python-works.com>
parents:
793
diff
changeset
|
9 :author: marcink |
902
07a6e8c65526
fixed copyright year to 2011
Marcin Kuzminski <marcin@python-works.com>
parents:
875
diff
changeset
|
10 :copyright: (C) 2009-2011 Marcin Kuzminski <marcin@python-works.com> |
861
fd2ea6ceadc8
updated docs on every controller
Marcin Kuzminski <marcin@python-works.com>
parents:
793
diff
changeset
|
11 :license: GPLv3, see COPYING for more details. |
fd2ea6ceadc8
updated docs on every controller
Marcin Kuzminski <marcin@python-works.com>
parents:
793
diff
changeset
|
12 """ |
734
49eb69d78988
implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
13 # This program is free software; you can redistribute it and/or |
49eb69d78988
implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
14 # modify it under the terms of the GNU General Public License |
49eb69d78988
implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
15 # as published by the Free Software Foundation; version 2 |
49eb69d78988
implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
16 # of the License or (at your opinion) any later version of the license. |
49eb69d78988
implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
17 # |
49eb69d78988
implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
18 # This program is distributed in the hope that it will be useful, |
49eb69d78988
implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
49eb69d78988
implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
49eb69d78988
implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
21 # GNU General Public License for more details. |
49eb69d78988
implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
22 # |
49eb69d78988
implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
23 # You should have received a copy of the GNU General Public License |
49eb69d78988
implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
24 # along with this program; if not, write to the Free Software |
49eb69d78988
implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
25 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
49eb69d78988
implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
26 # MA 02110-1301, USA. |
861
fd2ea6ceadc8
updated docs on every controller
Marcin Kuzminski <marcin@python-works.com>
parents:
793
diff
changeset
|
27 import logging |
734
49eb69d78988
implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
28 |
1041
98be43b888c4
Updated new Journal with users and dates aggregates
Marcin Kuzminski <marcin@python-works.com>
parents:
1019
diff
changeset
|
29 from sqlalchemy import or_ |
98be43b888c4
Updated new Journal with users and dates aggregates
Marcin Kuzminski <marcin@python-works.com>
parents:
1019
diff
changeset
|
30 from sqlalchemy.orm import joinedload, make_transient |
98be43b888c4
Updated new Journal with users and dates aggregates
Marcin Kuzminski <marcin@python-works.com>
parents:
1019
diff
changeset
|
31 from webhelpers.paginate import Page |
1045
3fc9183e05dd
another major codes rewrite:
Marcin Kuzminski <marcin@python-works.com>
parents:
1041
diff
changeset
|
32 from itertools import groupby |
861
fd2ea6ceadc8
updated docs on every controller
Marcin Kuzminski <marcin@python-works.com>
parents:
793
diff
changeset
|
33 |
1041
98be43b888c4
Updated new Journal with users and dates aggregates
Marcin Kuzminski <marcin@python-works.com>
parents:
1019
diff
changeset
|
34 from paste.httpexceptions import HTTPInternalServerError |
1045
3fc9183e05dd
another major codes rewrite:
Marcin Kuzminski <marcin@python-works.com>
parents:
1041
diff
changeset
|
35 from pylons import request, tmpl_context as c |
995
d14723711d17
fixes for journal, added paging now it's possible to view whole journal
Marcin Kuzminski <marcin@python-works.com>
parents:
994
diff
changeset
|
36 |
793
3025b0f9475e
disabled journal for anonymous users
Marcin Kuzminski <marcin@python-works.com>
parents:
738
diff
changeset
|
37 from rhodecode.lib.auth import LoginRequired, NotAnonymous |
734
49eb69d78988
implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
38 from rhodecode.lib.base import BaseController, render |
49eb69d78988
implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
39 from rhodecode.lib.helpers import get_token |
49eb69d78988
implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
40 from rhodecode.model.db import UserLog, UserFollowing |
861
fd2ea6ceadc8
updated docs on every controller
Marcin Kuzminski <marcin@python-works.com>
parents:
793
diff
changeset
|
41 |
734
49eb69d78988
implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
42 log = logging.getLogger(__name__) |
49eb69d78988
implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
43 |
49eb69d78988
implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
44 class JournalController(BaseController): |
49eb69d78988
implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
45 |
49eb69d78988
implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
46 |
49eb69d78988
implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
47 @LoginRequired() |
793
3025b0f9475e
disabled journal for anonymous users
Marcin Kuzminski <marcin@python-works.com>
parents:
738
diff
changeset
|
48 @NotAnonymous() |
734
49eb69d78988
implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
49 def __before__(self): |
49eb69d78988
implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
50 super(JournalController, self).__before__() |
49eb69d78988
implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
51 |
49eb69d78988
implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
52 def index(self): |
49eb69d78988
implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
53 # Return a rendered template |
1045
3fc9183e05dd
another major codes rewrite:
Marcin Kuzminski <marcin@python-works.com>
parents:
1041
diff
changeset
|
54 |
734
49eb69d78988
implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
55 c.following = self.sa.query(UserFollowing)\ |
1000
229437211bef
Optimized queries on journal, and added quick stop following action button in journal
Marcin Kuzminski <marcin@python-works.com>
parents:
995
diff
changeset
|
56 .filter(UserFollowing.user_id == c.rhodecode_user.user_id)\ |
229437211bef
Optimized queries on journal, and added quick stop following action button in journal
Marcin Kuzminski <marcin@python-works.com>
parents:
995
diff
changeset
|
57 .options(joinedload(UserFollowing.follows_repository))\ |
229437211bef
Optimized queries on journal, and added quick stop following action button in journal
Marcin Kuzminski <marcin@python-works.com>
parents:
995
diff
changeset
|
58 .all() |
793
3025b0f9475e
disabled journal for anonymous users
Marcin Kuzminski <marcin@python-works.com>
parents:
738
diff
changeset
|
59 |
1041
98be43b888c4
Updated new Journal with users and dates aggregates
Marcin Kuzminski <marcin@python-works.com>
parents:
1019
diff
changeset
|
60 |
793
3025b0f9475e
disabled journal for anonymous users
Marcin Kuzminski <marcin@python-works.com>
parents:
738
diff
changeset
|
61 repo_ids = [x.follows_repository.repo_id for x in c.following |
736
ab5c09715127
activated filters for user journal for following users/repos only
Marcin Kuzminski <marcin@python-works.com>
parents:
735
diff
changeset
|
62 if x.follows_repository is not None] |
793
3025b0f9475e
disabled journal for anonymous users
Marcin Kuzminski <marcin@python-works.com>
parents:
738
diff
changeset
|
63 user_ids = [x.follows_user.user_id for x in c.following |
736
ab5c09715127
activated filters for user journal for following users/repos only
Marcin Kuzminski <marcin@python-works.com>
parents:
735
diff
changeset
|
64 if x.follows_user is not None] |
793
3025b0f9475e
disabled journal for anonymous users
Marcin Kuzminski <marcin@python-works.com>
parents:
738
diff
changeset
|
65 |
1019
e4b7cfeb2eea
optimized filtering on journal
Marcin Kuzminski <marcin@python-works.com>
parents:
1000
diff
changeset
|
66 filtering_criterion = None |
995
d14723711d17
fixes for journal, added paging now it's possible to view whole journal
Marcin Kuzminski <marcin@python-works.com>
parents:
994
diff
changeset
|
67 |
1019
e4b7cfeb2eea
optimized filtering on journal
Marcin Kuzminski <marcin@python-works.com>
parents:
1000
diff
changeset
|
68 if repo_ids and user_ids: |
e4b7cfeb2eea
optimized filtering on journal
Marcin Kuzminski <marcin@python-works.com>
parents:
1000
diff
changeset
|
69 filtering_criterion = or_(UserLog.repository_id.in_(repo_ids), |
e4b7cfeb2eea
optimized filtering on journal
Marcin Kuzminski <marcin@python-works.com>
parents:
1000
diff
changeset
|
70 UserLog.user_id.in_(user_ids)) |
e4b7cfeb2eea
optimized filtering on journal
Marcin Kuzminski <marcin@python-works.com>
parents:
1000
diff
changeset
|
71 if repo_ids and not user_ids: |
e4b7cfeb2eea
optimized filtering on journal
Marcin Kuzminski <marcin@python-works.com>
parents:
1000
diff
changeset
|
72 filtering_criterion = UserLog.repository_id.in_(repo_ids) |
e4b7cfeb2eea
optimized filtering on journal
Marcin Kuzminski <marcin@python-works.com>
parents:
1000
diff
changeset
|
73 if not repo_ids and user_ids: |
e4b7cfeb2eea
optimized filtering on journal
Marcin Kuzminski <marcin@python-works.com>
parents:
1000
diff
changeset
|
74 filtering_criterion = UserLog.user_id.in_(user_ids) |
e4b7cfeb2eea
optimized filtering on journal
Marcin Kuzminski <marcin@python-works.com>
parents:
1000
diff
changeset
|
75 if filtering_criterion is not None: |
e4b7cfeb2eea
optimized filtering on journal
Marcin Kuzminski <marcin@python-works.com>
parents:
1000
diff
changeset
|
76 journal = self.sa.query(UserLog)\ |
1041
98be43b888c4
Updated new Journal with users and dates aggregates
Marcin Kuzminski <marcin@python-works.com>
parents:
1019
diff
changeset
|
77 .options(joinedload(UserLog.user))\ |
98be43b888c4
Updated new Journal with users and dates aggregates
Marcin Kuzminski <marcin@python-works.com>
parents:
1019
diff
changeset
|
78 .options(joinedload(UserLog.repository))\ |
1019
e4b7cfeb2eea
optimized filtering on journal
Marcin Kuzminski <marcin@python-works.com>
parents:
1000
diff
changeset
|
79 .filter(filtering_criterion)\ |
e4b7cfeb2eea
optimized filtering on journal
Marcin Kuzminski <marcin@python-works.com>
parents:
1000
diff
changeset
|
80 .order_by(UserLog.action_date.desc()) |
e4b7cfeb2eea
optimized filtering on journal
Marcin Kuzminski <marcin@python-works.com>
parents:
1000
diff
changeset
|
81 else: |
e4b7cfeb2eea
optimized filtering on journal
Marcin Kuzminski <marcin@python-works.com>
parents:
1000
diff
changeset
|
82 journal = [] |
1045
3fc9183e05dd
another major codes rewrite:
Marcin Kuzminski <marcin@python-works.com>
parents:
1041
diff
changeset
|
83 p = int(request.params.get('page', 1)) |
994
7f9d23f6a526
Added grouping by days in journal
Marcin Kuzminski <marcin@python-works.com>
parents:
902
diff
changeset
|
84 |
1045
3fc9183e05dd
another major codes rewrite:
Marcin Kuzminski <marcin@python-works.com>
parents:
1041
diff
changeset
|
85 c.journal_pager = Page(journal, page=p, items_per_page=20) |
3fc9183e05dd
another major codes rewrite:
Marcin Kuzminski <marcin@python-works.com>
parents:
1041
diff
changeset
|
86 |
995
d14723711d17
fixes for journal, added paging now it's possible to view whole journal
Marcin Kuzminski <marcin@python-works.com>
parents:
994
diff
changeset
|
87 c.journal_day_aggreagate = self._get_daily_aggregate(c.journal_pager) |
1045
3fc9183e05dd
another major codes rewrite:
Marcin Kuzminski <marcin@python-works.com>
parents:
1041
diff
changeset
|
88 |
995
d14723711d17
fixes for journal, added paging now it's possible to view whole journal
Marcin Kuzminski <marcin@python-works.com>
parents:
994
diff
changeset
|
89 c.journal_data = render('journal/journal_data.html') |
d14723711d17
fixes for journal, added paging now it's possible to view whole journal
Marcin Kuzminski <marcin@python-works.com>
parents:
994
diff
changeset
|
90 if request.params.get('partial'): |
d14723711d17
fixes for journal, added paging now it's possible to view whole journal
Marcin Kuzminski <marcin@python-works.com>
parents:
994
diff
changeset
|
91 return c.journal_data |
d14723711d17
fixes for journal, added paging now it's possible to view whole journal
Marcin Kuzminski <marcin@python-works.com>
parents:
994
diff
changeset
|
92 return render('journal/journal.html') |
734
49eb69d78988
implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
93 |
994
7f9d23f6a526
Added grouping by days in journal
Marcin Kuzminski <marcin@python-works.com>
parents:
902
diff
changeset
|
94 |
7f9d23f6a526
Added grouping by days in journal
Marcin Kuzminski <marcin@python-works.com>
parents:
902
diff
changeset
|
95 def _get_daily_aggregate(self, journal): |
7f9d23f6a526
Added grouping by days in journal
Marcin Kuzminski <marcin@python-works.com>
parents:
902
diff
changeset
|
96 groups = [] |
7f9d23f6a526
Added grouping by days in journal
Marcin Kuzminski <marcin@python-works.com>
parents:
902
diff
changeset
|
97 for k, g in groupby(journal, lambda x:x.action_as_day): |
1041
98be43b888c4
Updated new Journal with users and dates aggregates
Marcin Kuzminski <marcin@python-works.com>
parents:
1019
diff
changeset
|
98 user_group = [] |
98be43b888c4
Updated new Journal with users and dates aggregates
Marcin Kuzminski <marcin@python-works.com>
parents:
1019
diff
changeset
|
99 for k2, g2 in groupby(list(g), lambda x:x.user.email): |
98be43b888c4
Updated new Journal with users and dates aggregates
Marcin Kuzminski <marcin@python-works.com>
parents:
1019
diff
changeset
|
100 l = list(g2) |
98be43b888c4
Updated new Journal with users and dates aggregates
Marcin Kuzminski <marcin@python-works.com>
parents:
1019
diff
changeset
|
101 user_group.append((l[0].user, l)) |
98be43b888c4
Updated new Journal with users and dates aggregates
Marcin Kuzminski <marcin@python-works.com>
parents:
1019
diff
changeset
|
102 |
98be43b888c4
Updated new Journal with users and dates aggregates
Marcin Kuzminski <marcin@python-works.com>
parents:
1019
diff
changeset
|
103 groups.append((k, user_group,)) |
994
7f9d23f6a526
Added grouping by days in journal
Marcin Kuzminski <marcin@python-works.com>
parents:
902
diff
changeset
|
104 |
7f9d23f6a526
Added grouping by days in journal
Marcin Kuzminski <marcin@python-works.com>
parents:
902
diff
changeset
|
105 return groups |
7f9d23f6a526
Added grouping by days in journal
Marcin Kuzminski <marcin@python-works.com>
parents:
902
diff
changeset
|
106 |
7f9d23f6a526
Added grouping by days in journal
Marcin Kuzminski <marcin@python-works.com>
parents:
902
diff
changeset
|
107 |
734
49eb69d78988
implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
108 def toggle_following(self): |
875 | 109 cur_token = request.POST.get('auth_token') |
110 token = get_token() | |
111 if cur_token == token: | |
734
49eb69d78988
implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
112 |
49eb69d78988
implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
113 user_id = request.POST.get('follows_user_id') |
49eb69d78988
implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
114 if user_id: |
49eb69d78988
implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
115 try: |
1045
3fc9183e05dd
another major codes rewrite:
Marcin Kuzminski <marcin@python-works.com>
parents:
1041
diff
changeset
|
116 self.scm_model.toggle_following_user(user_id, |
734
49eb69d78988
implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
117 c.rhodecode_user.user_id) |
49eb69d78988
implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
118 return 'ok' |
49eb69d78988
implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
119 except: |
49eb69d78988
implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
120 raise HTTPInternalServerError() |
49eb69d78988
implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
121 |
49eb69d78988
implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
122 repo_id = request.POST.get('follows_repo_id') |
49eb69d78988
implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
123 if repo_id: |
49eb69d78988
implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
124 try: |
1045
3fc9183e05dd
another major codes rewrite:
Marcin Kuzminski <marcin@python-works.com>
parents:
1041
diff
changeset
|
125 self.scm_model.toggle_following_repo(repo_id, |
734
49eb69d78988
implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
126 c.rhodecode_user.user_id) |
49eb69d78988
implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
127 return 'ok' |
49eb69d78988
implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
128 except: |
49eb69d78988
implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
129 raise HTTPInternalServerError() |
49eb69d78988
implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
130 |
49eb69d78988
implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
131 |
875 | 132 log.debug('token mismatch %s vs %s', cur_token, token) |
734
49eb69d78988
implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
133 raise HTTPInternalServerError() |