journal.py
133 lines
| 4.6 KiB
| text/x-python
|
PythonLexer
r861 | # -*- coding: utf-8 -*- | |||
""" | ||||
rhodecode.controllers.journal | ||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||||
Journal controller for pylons | ||||
:created_on: Nov 21, 2010 | ||||
:author: marcink | ||||
r902 | :copyright: (C) 2009-2011 Marcin Kuzminski <marcin@python-works.com> | |||
r861 | :license: GPLv3, see COPYING for more details. | |||
""" | ||||
r734 | # This program is free software; you can redistribute it and/or | |||
# modify it under the terms of the GNU General Public License | ||||
# as published by the Free Software Foundation; version 2 | ||||
# of the License or (at your opinion) any later version of the license. | ||||
# | ||||
# This program is distributed in the hope that it will be useful, | ||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
# GNU General Public License for more details. | ||||
# | ||||
# You should have received a copy of the GNU General Public License | ||||
# along with this program; if not, write to the Free Software | ||||
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, | ||||
# MA 02110-1301, USA. | ||||
r861 | import logging | |||
r734 | ||||
r1041 | from sqlalchemy import or_ | |||
from sqlalchemy.orm import joinedload, make_transient | ||||
from webhelpers.paginate import Page | ||||
r1045 | from itertools import groupby | |||
r861 | ||||
r1041 | from paste.httpexceptions import HTTPInternalServerError | |||
r1045 | from pylons import request, tmpl_context as c | |||
r995 | ||||
r793 | from rhodecode.lib.auth import LoginRequired, NotAnonymous | |||
r734 | from rhodecode.lib.base import BaseController, render | |||
from rhodecode.lib.helpers import get_token | ||||
from rhodecode.model.db import UserLog, UserFollowing | ||||
r861 | ||||
r734 | log = logging.getLogger(__name__) | |||
class JournalController(BaseController): | ||||
@LoginRequired() | ||||
r793 | @NotAnonymous() | |||
r734 | def __before__(self): | |||
super(JournalController, self).__before__() | ||||
def index(self): | ||||
# Return a rendered template | ||||
r1045 | ||||
r734 | c.following = self.sa.query(UserFollowing)\ | |||
r1000 | .filter(UserFollowing.user_id == c.rhodecode_user.user_id)\ | |||
.options(joinedload(UserFollowing.follows_repository))\ | ||||
.all() | ||||
r793 | ||||
r1041 | ||||
r793 | repo_ids = [x.follows_repository.repo_id for x in c.following | |||
r736 | if x.follows_repository is not None] | |||
r793 | user_ids = [x.follows_user.user_id for x in c.following | |||
r736 | if x.follows_user is not None] | |||
r793 | ||||
r1019 | filtering_criterion = None | |||
r995 | ||||
r1019 | if repo_ids and user_ids: | |||
filtering_criterion = or_(UserLog.repository_id.in_(repo_ids), | ||||
UserLog.user_id.in_(user_ids)) | ||||
if repo_ids and not user_ids: | ||||
filtering_criterion = UserLog.repository_id.in_(repo_ids) | ||||
if not repo_ids and user_ids: | ||||
filtering_criterion = UserLog.user_id.in_(user_ids) | ||||
if filtering_criterion is not None: | ||||
journal = self.sa.query(UserLog)\ | ||||
r1041 | .options(joinedload(UserLog.user))\ | |||
.options(joinedload(UserLog.repository))\ | ||||
r1019 | .filter(filtering_criterion)\ | |||
.order_by(UserLog.action_date.desc()) | ||||
else: | ||||
journal = [] | ||||
r1045 | p = int(request.params.get('page', 1)) | |||
r994 | ||||
r1045 | c.journal_pager = Page(journal, page=p, items_per_page=20) | |||
r995 | c.journal_day_aggreagate = self._get_daily_aggregate(c.journal_pager) | |||
r1045 | ||||
r995 | c.journal_data = render('journal/journal_data.html') | |||
if request.params.get('partial'): | ||||
return c.journal_data | ||||
return render('journal/journal.html') | ||||
r734 | ||||
r994 | ||||
def _get_daily_aggregate(self, journal): | ||||
groups = [] | ||||
for k, g in groupby(journal, lambda x:x.action_as_day): | ||||
r1041 | user_group = [] | |||
for k2, g2 in groupby(list(g), lambda x:x.user.email): | ||||
l = list(g2) | ||||
user_group.append((l[0].user, l)) | ||||
groups.append((k, user_group,)) | ||||
r994 | ||||
return groups | ||||
r734 | def toggle_following(self): | |||
r875 | cur_token = request.POST.get('auth_token') | |||
token = get_token() | ||||
if cur_token == token: | ||||
r734 | ||||
user_id = request.POST.get('follows_user_id') | ||||
if user_id: | ||||
try: | ||||
r1045 | self.scm_model.toggle_following_user(user_id, | |||
r734 | c.rhodecode_user.user_id) | |||
return 'ok' | ||||
except: | ||||
raise HTTPInternalServerError() | ||||
repo_id = request.POST.get('follows_repo_id') | ||||
if repo_id: | ||||
try: | ||||
r1045 | self.scm_model.toggle_following_repo(repo_id, | |||
r734 | c.rhodecode_user.user_id) | |||
return 'ok' | ||||
except: | ||||
raise HTTPInternalServerError() | ||||
r875 | log.debug('token mismatch %s vs %s', cur_token, token) | |||
r734 | raise HTTPInternalServerError() | |||