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