##// END OF EJS Templates
optimized filtering on journal
marcink -
r1019:e4b7cfeb beta
parent child Browse files
Show More
@@ -1,120 +1,129
1 1 # -*- coding: utf-8 -*-
2 2 """
3 3 rhodecode.controllers.journal
4 4 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 5
6 6 Journal controller for pylons
7 7
8 8 :created_on: Nov 21, 2010
9 9 :author: marcink
10 10 :copyright: (C) 2009-2011 Marcin Kuzminski <marcin@python-works.com>
11 11 :license: GPLv3, see COPYING for more details.
12 12 """
13 13 # This program is free software; you can redistribute it and/or
14 14 # modify it under the terms of the GNU General Public License
15 15 # as published by the Free Software Foundation; version 2
16 16 # of the License or (at your opinion) any later version of the license.
17 17 #
18 18 # This program is distributed in the hope that it will be useful,
19 19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
20 20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 21 # GNU General Public License for more details.
22 22 #
23 23 # You should have received a copy of the GNU General Public License
24 24 # along with this program; if not, write to the Free Software
25 25 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
26 26 # MA 02110-1301, USA.
27 27
28 28 import logging
29 29 from sqlalchemy import or_
30 30
31 31 from pylons import request, response, session, tmpl_context as c, url
32 32
33 33 from webhelpers.paginate import Page
34 34
35 35 from rhodecode.lib.auth import LoginRequired, NotAnonymous
36 36 from rhodecode.lib.base import BaseController, render
37 37 from rhodecode.lib.helpers import get_token
38 38 from rhodecode.model.db import UserLog, UserFollowing
39 39 from rhodecode.model.scm import ScmModel
40 40
41 41 from paste.httpexceptions import HTTPInternalServerError
42 42 from sqlalchemy.orm import joinedload
43 43
44 44 log = logging.getLogger(__name__)
45 45
46 46 class JournalController(BaseController):
47 47
48 48
49 49 @LoginRequired()
50 50 @NotAnonymous()
51 51 def __before__(self):
52 52 super(JournalController, self).__before__()
53 53
54 54 def index(self):
55 55 # Return a rendered template
56 56
57 57 c.following = self.sa.query(UserFollowing)\
58 58 .filter(UserFollowing.user_id == c.rhodecode_user.user_id)\
59 59 .options(joinedload(UserFollowing.follows_repository))\
60 60 .all()
61 61
62 62 repo_ids = [x.follows_repository.repo_id for x in c.following
63 63 if x.follows_repository is not None]
64 64 user_ids = [x.follows_user.user_id for x in c.following
65 65 if x.follows_user is not None]
66 66
67 journal = self.sa.query(UserLog)\
68 .filter(or_(
69 UserLog.repository_id.in_(repo_ids),
70 UserLog.user_id.in_(user_ids),
71 ))\
72 .order_by(UserLog.action_date.desc())
67 filtering_criterion = None
73 68
69 if repo_ids and user_ids:
70 filtering_criterion = or_(UserLog.repository_id.in_(repo_ids),
71 UserLog.user_id.in_(user_ids))
72 if repo_ids and not user_ids:
73 filtering_criterion = UserLog.repository_id.in_(repo_ids)
74 if not repo_ids and user_ids:
75 filtering_criterion = UserLog.user_id.in_(user_ids)
76
77 if filtering_criterion is not None:
78 journal = self.sa.query(UserLog)\
79 .filter(filtering_criterion)\
80 .order_by(UserLog.action_date.desc())
81 else:
82 journal = []
74 83
75 84 p = int(request.params.get('page', 1))
76 85 c.journal_pager = Page(journal, page=p, items_per_page=10)
77 86 c.journal_day_aggreagate = self._get_daily_aggregate(c.journal_pager)
78 87 c.journal_data = render('journal/journal_data.html')
79 88 if request.params.get('partial'):
80 89 return c.journal_data
81 90
82 91 return render('journal/journal.html')
83 92
84 93
85 94 def _get_daily_aggregate(self, journal):
86 95 from itertools import groupby
87 96 groups = []
88 97 for k, g in groupby(journal, lambda x:x.action_as_day):
89 98 groups.append((k, list(g),)) # Store group iterator as a list
90 99
91 100 return groups
92 101
93 102
94 103 def toggle_following(self):
95 104 cur_token = request.POST.get('auth_token')
96 105 token = get_token()
97 106 if cur_token == token:
98 107 scm_model = ScmModel()
99 108
100 109 user_id = request.POST.get('follows_user_id')
101 110 if user_id:
102 111 try:
103 112 scm_model.toggle_following_user(user_id,
104 113 c.rhodecode_user.user_id)
105 114 return 'ok'
106 115 except:
107 116 raise HTTPInternalServerError()
108 117
109 118 repo_id = request.POST.get('follows_repo_id')
110 119 if repo_id:
111 120 try:
112 121 scm_model.toggle_following_repo(repo_id,
113 122 c.rhodecode_user.user_id)
114 123 return 'ok'
115 124 except:
116 125 raise HTTPInternalServerError()
117 126
118 127
119 128 log.debug('token mismatch %s vs %s', cur_token, token)
120 129 raise HTTPInternalServerError()
General Comments 0
You need to be logged in to leave comments. Login now