##// END OF EJS Templates
fix missing c.user reference
marcink -
r2959:db40558e beta
parent child Browse files
Show More
@@ -1,300 +1,298 b''
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) 2010-2012 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 modify
14 14 # it under the terms of the GNU General Public License as published by
15 15 # the Free Software Foundation, either version 3 of the License, or
16 16 # (at your option) any later version.
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, see <http://www.gnu.org/licenses/>.
25 25 import logging
26 26 from itertools import groupby
27 27
28 28 from sqlalchemy import or_
29 29 from sqlalchemy.orm import joinedload
30 30 from webhelpers.paginate import Page
31 31 from webhelpers.feedgenerator import Atom1Feed, Rss201rev2Feed
32 32
33 33 from webob.exc import HTTPBadRequest
34 34 from pylons import request, tmpl_context as c, response, url
35 35 from pylons.i18n.translation import _
36 36
37 37 import rhodecode.lib.helpers as h
38 38 from rhodecode.lib.auth import LoginRequired, NotAnonymous
39 39 from rhodecode.lib.base import BaseController, render
40 40 from rhodecode.model.db import UserLog, UserFollowing, Repository, User
41 41 from rhodecode.model.meta import Session
42 42 from sqlalchemy.sql.expression import func
43 43 from rhodecode.model.scm import ScmModel
44 44 from rhodecode.lib.utils2 import safe_int
45 45
46 46 log = logging.getLogger(__name__)
47 47
48 48
49 49 class JournalController(BaseController):
50 50
51 51 def __before__(self):
52 52 super(JournalController, self).__before__()
53 53 self.language = 'en-us'
54 54 self.ttl = "5"
55 55 self.feed_nr = 20
56 56
57 57 @LoginRequired()
58 58 @NotAnonymous()
59 59 def index(self):
60 60 # Return a rendered template
61 61 p = safe_int(request.params.get('page', 1), 1)
62
63 62 c.user = User.get(self.rhodecode_user.user_id)
64
65 63 c.following = self.sa.query(UserFollowing)\
66 64 .filter(UserFollowing.user_id == self.rhodecode_user.user_id)\
67 65 .options(joinedload(UserFollowing.follows_repository))\
68 66 .all()
69 67
70 68 journal = self._get_journal_data(c.following)
71 69
72 70 c.journal_pager = Page(journal, page=p, items_per_page=20)
73
74 71 c.journal_day_aggreagate = self._get_daily_aggregate(c.journal_pager)
75 72
76 73 c.journal_data = render('journal/journal_data.html')
77 74 if request.environ.get('HTTP_X_PARTIAL_XHR'):
78 75 return c.journal_data
79 76 return render('journal/journal.html')
80 77
81 78 @LoginRequired()
82 79 @NotAnonymous()
83 80 def index_my_repos(self):
81 c.user = User.get(self.rhodecode_user.user_id)
84 82 if request.environ.get('HTTP_X_PARTIAL_XHR'):
85 83 all_repos = self.sa.query(Repository)\
86 84 .filter(Repository.user_id == c.user.user_id)\
87 85 .order_by(func.lower(Repository.repo_name)).all()
88 86 c.user_repos = ScmModel().get_repos(all_repos)
89 87 return render('journal/journal_page_repos.html')
90 88
91 89 @LoginRequired(api_access=True)
92 90 @NotAnonymous()
93 91 def journal_atom(self):
94 92 """
95 93 Produce an atom-1.0 feed via feedgenerator module
96 94 """
97 95 following = self.sa.query(UserFollowing)\
98 96 .filter(UserFollowing.user_id == self.rhodecode_user.user_id)\
99 97 .options(joinedload(UserFollowing.follows_repository))\
100 98 .all()
101 99 return self._atom_feed(following, public=False)
102 100
103 101 @LoginRequired(api_access=True)
104 102 @NotAnonymous()
105 103 def journal_rss(self):
106 104 """
107 105 Produce an rss feed via feedgenerator module
108 106 """
109 107 following = self.sa.query(UserFollowing)\
110 108 .filter(UserFollowing.user_id == self.rhodecode_user.user_id)\
111 109 .options(joinedload(UserFollowing.follows_repository))\
112 110 .all()
113 111 return self._rss_feed(following, public=False)
114 112
115 113 def _get_daily_aggregate(self, journal):
116 114 groups = []
117 115 for k, g in groupby(journal, lambda x: x.action_as_day):
118 116 user_group = []
119 117 for k2, g2 in groupby(list(g), lambda x: x.user.email):
120 118 l = list(g2)
121 119 user_group.append((l[0].user, l))
122 120
123 121 groups.append((k, user_group,))
124 122
125 123 return groups
126 124
127 125 def _get_journal_data(self, following_repos):
128 126 repo_ids = [x.follows_repository.repo_id for x in following_repos
129 127 if x.follows_repository is not None]
130 128 user_ids = [x.follows_user.user_id for x in following_repos
131 129 if x.follows_user is not None]
132 130
133 131 filtering_criterion = None
134 132
135 133 if repo_ids and user_ids:
136 134 filtering_criterion = or_(UserLog.repository_id.in_(repo_ids),
137 135 UserLog.user_id.in_(user_ids))
138 136 if repo_ids and not user_ids:
139 137 filtering_criterion = UserLog.repository_id.in_(repo_ids)
140 138 if not repo_ids and user_ids:
141 139 filtering_criterion = UserLog.user_id.in_(user_ids)
142 140 if filtering_criterion is not None:
143 141 journal = self.sa.query(UserLog)\
144 142 .options(joinedload(UserLog.user))\
145 143 .options(joinedload(UserLog.repository))\
146 144 .filter(filtering_criterion)\
147 145 .order_by(UserLog.action_date.desc())
148 146 else:
149 147 journal = []
150 148
151 149 return journal
152 150
153 151 @LoginRequired()
154 152 @NotAnonymous()
155 153 def toggle_following(self):
156 154 cur_token = request.POST.get('auth_token')
157 155 token = h.get_token()
158 156 if cur_token == token:
159 157
160 158 user_id = request.POST.get('follows_user_id')
161 159 if user_id:
162 160 try:
163 161 self.scm_model.toggle_following_user(user_id,
164 162 self.rhodecode_user.user_id)
165 163 Session.commit()
166 164 return 'ok'
167 165 except:
168 166 raise HTTPBadRequest()
169 167
170 168 repo_id = request.POST.get('follows_repo_id')
171 169 if repo_id:
172 170 try:
173 171 self.scm_model.toggle_following_repo(repo_id,
174 172 self.rhodecode_user.user_id)
175 173 Session.commit()
176 174 return 'ok'
177 175 except:
178 176 raise HTTPBadRequest()
179 177
180 178 log.debug('token mismatch %s vs %s' % (cur_token, token))
181 179 raise HTTPBadRequest()
182 180
183 181 @LoginRequired()
184 182 def public_journal(self):
185 183 # Return a rendered template
186 184 p = safe_int(request.params.get('page', 1), 1)
187 185
188 186 c.following = self.sa.query(UserFollowing)\
189 187 .filter(UserFollowing.user_id == self.rhodecode_user.user_id)\
190 188 .options(joinedload(UserFollowing.follows_repository))\
191 189 .all()
192 190
193 191 journal = self._get_journal_data(c.following)
194 192
195 193 c.journal_pager = Page(journal, page=p, items_per_page=20)
196 194
197 195 c.journal_day_aggreagate = self._get_daily_aggregate(c.journal_pager)
198 196
199 197 c.journal_data = render('journal/journal_data.html')
200 198 if request.environ.get('HTTP_X_PARTIAL_XHR'):
201 199 return c.journal_data
202 200 return render('journal/public_journal.html')
203 201
204 202 def _atom_feed(self, repos, public=True):
205 203 journal = self._get_journal_data(repos)
206 204 if public:
207 205 _link = url('public_journal_atom', qualified=True)
208 206 _desc = '%s %s %s' % (c.rhodecode_name, _('public journal'),
209 207 'atom feed')
210 208 else:
211 209 _link = url('journal_atom', qualified=True)
212 210 _desc = '%s %s %s' % (c.rhodecode_name, _('journal'), 'atom feed')
213 211
214 212 feed = Atom1Feed(title=_desc,
215 213 link=_link,
216 214 description=_desc,
217 215 language=self.language,
218 216 ttl=self.ttl)
219 217
220 218 for entry in journal[:self.feed_nr]:
221 219 action, action_extra, ico = h.action_parser(entry, feed=True)
222 220 title = "%s - %s %s" % (entry.user.short_contact, action(),
223 221 entry.repository.repo_name)
224 222 desc = action_extra()
225 223 _url = None
226 224 if entry.repository is not None:
227 225 _url = url('changelog_home',
228 226 repo_name=entry.repository.repo_name,
229 227 qualified=True)
230 228
231 229 feed.add_item(title=title,
232 230 pubdate=entry.action_date,
233 231 link=_url or url('', qualified=True),
234 232 author_email=entry.user.email,
235 233 author_name=entry.user.full_contact,
236 234 description=desc)
237 235
238 236 response.content_type = feed.mime_type
239 237 return feed.writeString('utf-8')
240 238
241 239 def _rss_feed(self, repos, public=True):
242 240 journal = self._get_journal_data(repos)
243 241 if public:
244 242 _link = url('public_journal_atom', qualified=True)
245 243 _desc = '%s %s %s' % (c.rhodecode_name, _('public journal'),
246 244 'rss feed')
247 245 else:
248 246 _link = url('journal_atom', qualified=True)
249 247 _desc = '%s %s %s' % (c.rhodecode_name, _('journal'), 'rss feed')
250 248
251 249 feed = Rss201rev2Feed(title=_desc,
252 250 link=_link,
253 251 description=_desc,
254 252 language=self.language,
255 253 ttl=self.ttl)
256 254
257 255 for entry in journal[:self.feed_nr]:
258 256 action, action_extra, ico = h.action_parser(entry, feed=True)
259 257 title = "%s - %s %s" % (entry.user.short_contact, action(),
260 258 entry.repository.repo_name)
261 259 desc = action_extra()
262 260 _url = None
263 261 if entry.repository is not None:
264 262 _url = url('changelog_home',
265 263 repo_name=entry.repository.repo_name,
266 264 qualified=True)
267 265
268 266 feed.add_item(title=title,
269 267 pubdate=entry.action_date,
270 268 link=_url or url('', qualified=True),
271 269 author_email=entry.user.email,
272 270 author_name=entry.user.full_contact,
273 271 description=desc)
274 272
275 273 response.content_type = feed.mime_type
276 274 return feed.writeString('utf-8')
277 275
278 276 @LoginRequired(api_access=True)
279 277 def public_journal_atom(self):
280 278 """
281 279 Produce an atom-1.0 feed via feedgenerator module
282 280 """
283 281 c.following = self.sa.query(UserFollowing)\
284 282 .filter(UserFollowing.user_id == self.rhodecode_user.user_id)\
285 283 .options(joinedload(UserFollowing.follows_repository))\
286 284 .all()
287 285
288 286 return self._atom_feed(c.following)
289 287
290 288 @LoginRequired(api_access=True)
291 289 def public_journal_rss(self):
292 290 """
293 291 Produce an rss2 feed via feedgenerator module
294 292 """
295 293 c.following = self.sa.query(UserFollowing)\
296 294 .filter(UserFollowing.user_id == self.rhodecode_user.user_id)\
297 295 .options(joinedload(UserFollowing.follows_repository))\
298 296 .all()
299 297
300 298 return self._rss_feed(c.following)
General Comments 0
You need to be logged in to leave comments. Login now