annotate rhodecode/controllers/journal.py @ 832:634596f81cfd beta

new improved models with helper functions for easier data fetching
author Marcin Kuzminski <marcin@python-works.com>
date Sat, 11 Dec 2010 00:36:33 +0100
parents 3025b0f9475e
children fd2ea6ceadc8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
734
49eb69d78988 implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1 #!/usr/bin/env python
49eb69d78988 implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
2 # encoding: utf-8
49eb69d78988 implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
3 # journal controller for pylons
49eb69d78988 implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
4 # Copyright (C) 2009-2010 Marcin Kuzminski <marcin@python-works.com>
49eb69d78988 implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
5 #
49eb69d78988 implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
6 # 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
7 # 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
8 # 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
9 # 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
10 #
49eb69d78988 implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
11 # 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
12 # 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
13 # 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
14 # GNU General Public License for more details.
49eb69d78988 implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
15 #
49eb69d78988 implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
16 # 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
17 # 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
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
49eb69d78988 implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
19 # MA 02110-1301, USA.
49eb69d78988 implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
20 """
49eb69d78988 implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
21 Created on November 21, 2010
49eb69d78988 implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
22 journal controller for pylons
49eb69d78988 implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
23 @author: marcink
49eb69d78988 implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
24 """
49eb69d78988 implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
25
49eb69d78988 implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
26 from pylons import request, response, session, tmpl_context as c, url
49eb69d78988 implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
27 from pylons.controllers.util import abort, redirect
793
3025b0f9475e disabled journal for anonymous users
Marcin Kuzminski <marcin@python-works.com>
parents: 738
diff changeset
28 from rhodecode.lib.auth import LoginRequired, NotAnonymous
734
49eb69d78988 implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
29 from rhodecode.lib.base import BaseController, render
49eb69d78988 implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
30 from rhodecode.lib.helpers import get_token
49eb69d78988 implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
31 from rhodecode.model.db import UserLog, UserFollowing
49eb69d78988 implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
32 from rhodecode.model.scm import ScmModel
736
ab5c09715127 activated filters for user journal for following users/repos only
Marcin Kuzminski <marcin@python-works.com>
parents: 735
diff changeset
33 from sqlalchemy import or_
734
49eb69d78988 implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
34 import logging
49eb69d78988 implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
35 from paste.httpexceptions import HTTPInternalServerError, HTTPNotFound
49eb69d78988 implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
36
49eb69d78988 implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
37 log = logging.getLogger(__name__)
49eb69d78988 implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
38
49eb69d78988 implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
39 class JournalController(BaseController):
49eb69d78988 implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
40
49eb69d78988 implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
41
49eb69d78988 implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
42 @LoginRequired()
793
3025b0f9475e disabled journal for anonymous users
Marcin Kuzminski <marcin@python-works.com>
parents: 738
diff changeset
43 @NotAnonymous()
734
49eb69d78988 implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
44 def __before__(self):
49eb69d78988 implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
45 super(JournalController, self).__before__()
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 def index(self):
49eb69d78988 implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
48 # Return a rendered template
49eb69d78988 implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
49
49eb69d78988 implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
50 c.following = self.sa.query(UserFollowing)\
49eb69d78988 implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
51 .filter(UserFollowing.user_id == c.rhodecode_user.user_id).all()
793
3025b0f9475e disabled journal for anonymous users
Marcin Kuzminski <marcin@python-works.com>
parents: 738
diff changeset
52
3025b0f9475e disabled journal for anonymous users
Marcin Kuzminski <marcin@python-works.com>
parents: 738
diff changeset
53 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
54 if x.follows_repository is not None]
793
3025b0f9475e disabled journal for anonymous users
Marcin Kuzminski <marcin@python-works.com>
parents: 738
diff changeset
55 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
56 if x.follows_user is not None]
793
3025b0f9475e disabled journal for anonymous users
Marcin Kuzminski <marcin@python-works.com>
parents: 738
diff changeset
57
734
49eb69d78988 implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
58 c.journal = self.sa.query(UserLog)\
736
ab5c09715127 activated filters for user journal for following users/repos only
Marcin Kuzminski <marcin@python-works.com>
parents: 735
diff changeset
59 .filter(or_(
ab5c09715127 activated filters for user journal for following users/repos only
Marcin Kuzminski <marcin@python-works.com>
parents: 735
diff changeset
60 UserLog.repository_id.in_(repo_ids),
ab5c09715127 activated filters for user journal for following users/repos only
Marcin Kuzminski <marcin@python-works.com>
parents: 735
diff changeset
61 UserLog.user_id.in_(user_ids),
ab5c09715127 activated filters for user journal for following users/repos only
Marcin Kuzminski <marcin@python-works.com>
parents: 735
diff changeset
62 ))\
734
49eb69d78988 implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
63 .order_by(UserLog.action_date.desc())\
738
ddaddffcb7dc added limit for user journal
Marcin Kuzminski <marcin@python-works.com>
parents: 736
diff changeset
64 .limit(20)\
734
49eb69d78988 implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
65 .all()
49eb69d78988 implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
66 return render('/journal.html')
49eb69d78988 implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
67
49eb69d78988 implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
68 def toggle_following(self):
49eb69d78988 implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
69
49eb69d78988 implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
70 if request.POST.get('auth_token') == get_token():
49eb69d78988 implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
71 scm_model = ScmModel()
49eb69d78988 implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
72
49eb69d78988 implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
73 user_id = request.POST.get('follows_user_id')
49eb69d78988 implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
74 if user_id:
49eb69d78988 implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
75 try:
49eb69d78988 implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
76 scm_model.toggle_following_user(user_id,
49eb69d78988 implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
77 c.rhodecode_user.user_id)
49eb69d78988 implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
78 return 'ok'
49eb69d78988 implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
79 except:
49eb69d78988 implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
80 raise HTTPInternalServerError()
49eb69d78988 implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
81
49eb69d78988 implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
82 repo_id = request.POST.get('follows_repo_id')
49eb69d78988 implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
83 if repo_id:
49eb69d78988 implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
84 try:
49eb69d78988 implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
85 scm_model.toggle_following_repo(repo_id,
49eb69d78988 implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
86 c.rhodecode_user.user_id)
49eb69d78988 implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
87 return 'ok'
49eb69d78988 implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
88 except:
49eb69d78988 implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
89 raise HTTPInternalServerError()
49eb69d78988 implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
90
49eb69d78988 implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
91
49eb69d78988 implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
92
49eb69d78988 implemented user dashboards, and following system.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
93 raise HTTPInternalServerError()