annotate pylons_app/controllers/changelog.py @ 291:a61b19036de0

code cleanups
author Marcin Kuzminski <marcin@python-works.com>
date Wed, 23 Jun 2010 00:40:04 +0200
parents ab1afe7444f3
children fdf9f6ee5217
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
252
3782a6d698af licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents: 245
diff changeset
1 #!/usr/bin/env python
3782a6d698af licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents: 245
diff changeset
2 # encoding: utf-8
3782a6d698af licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents: 245
diff changeset
3 # changelog controller for pylons
3782a6d698af licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents: 245
diff changeset
4 # Copyright (C) 2009-2010 Marcin Kuzminski <marcin@python-works.com>
288
ab1afe7444f3 Initial graph release.
Marcin Kuzminski <marcin@python-works.com>
parents: 282
diff changeset
5 from json import dumps
291
a61b19036de0 code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents: 288
diff changeset
6 from mercurial.graphmod import colored, CHANGESET, revisions as graph_rev
288
ab1afe7444f3 Initial graph release.
Marcin Kuzminski <marcin@python-works.com>
parents: 282
diff changeset
7 from pylons import request, session, tmpl_context as c
ab1afe7444f3 Initial graph release.
Marcin Kuzminski <marcin@python-works.com>
parents: 282
diff changeset
8 from pylons_app.lib.auth import LoginRequired
ab1afe7444f3 Initial graph release.
Marcin Kuzminski <marcin@python-works.com>
parents: 282
diff changeset
9 from pylons_app.lib.base import BaseController, render
ab1afe7444f3 Initial graph release.
Marcin Kuzminski <marcin@python-works.com>
parents: 282
diff changeset
10 from pylons_app.model.hg_model import HgModel
ab1afe7444f3 Initial graph release.
Marcin Kuzminski <marcin@python-works.com>
parents: 282
diff changeset
11 from webhelpers.paginate import Page
ab1afe7444f3 Initial graph release.
Marcin Kuzminski <marcin@python-works.com>
parents: 282
diff changeset
12 import logging
252
3782a6d698af licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents: 245
diff changeset
13
3782a6d698af licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents: 245
diff changeset
14 # This program is free software; you can redistribute it and/or
3782a6d698af licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents: 245
diff changeset
15 # modify it under the terms of the GNU General Public License
3782a6d698af licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents: 245
diff changeset
16 # as published by the Free Software Foundation; version 2
3782a6d698af licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents: 245
diff changeset
17 # of the License or (at your opinion) any later version of the license.
3782a6d698af licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents: 245
diff changeset
18 #
3782a6d698af licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents: 245
diff changeset
19 # This program is distributed in the hope that it will be useful,
3782a6d698af licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents: 245
diff changeset
20 # but WITHOUT ANY WARRANTY; without even the implied warranty of
3782a6d698af licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents: 245
diff changeset
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3782a6d698af licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents: 245
diff changeset
22 # GNU General Public License for more details.
3782a6d698af licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents: 245
diff changeset
23 #
3782a6d698af licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents: 245
diff changeset
24 # You should have received a copy of the GNU General Public License
3782a6d698af licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents: 245
diff changeset
25 # along with this program; if not, write to the Free Software
3782a6d698af licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents: 245
diff changeset
26 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
3782a6d698af licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents: 245
diff changeset
27 # MA 02110-1301, USA.
3782a6d698af licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents: 245
diff changeset
28 """
3782a6d698af licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents: 245
diff changeset
29 Created on April 21, 2010
3782a6d698af licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents: 245
diff changeset
30 changelog controller for pylons
3782a6d698af licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents: 245
diff changeset
31 @author: marcink
3782a6d698af licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents: 245
diff changeset
32 """
196
568f95056716 moved all cache function out to Base Controller for easier maintainance
Marcin Kuzminski <marcin@python-works.com>
parents: 192
diff changeset
33 log = logging.getLogger(__name__)
93
aec4c0071cb3 added empty controllers for branches tags files graph, routing and test for them
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
34
aec4c0071cb3 added empty controllers for branches tags files graph, routing and test for them
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
35 class ChangelogController(BaseController):
192
f191f99f59c9 full changelog caching, secured changelog with LoginRequired, some minor changes in graph
Marcin Kuzminski <marcin@python-works.com>
parents: 142
diff changeset
36
f191f99f59c9 full changelog caching, secured changelog with LoginRequired, some minor changes in graph
Marcin Kuzminski <marcin@python-works.com>
parents: 142
diff changeset
37 @LoginRequired()
93
aec4c0071cb3 added empty controllers for branches tags files graph, routing and test for them
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
38 def __before__(self):
192
f191f99f59c9 full changelog caching, secured changelog with LoginRequired, some minor changes in graph
Marcin Kuzminski <marcin@python-works.com>
parents: 142
diff changeset
39 super(ChangelogController, self).__before__()
f191f99f59c9 full changelog caching, secured changelog with LoginRequired, some minor changes in graph
Marcin Kuzminski <marcin@python-works.com>
parents: 142
diff changeset
40
93
aec4c0071cb3 added empty controllers for branches tags files graph, routing and test for them
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
41 def index(self):
213
Marcin Kuzminski <marcin@python-works.com>
parents: 211
diff changeset
42 limit = 100
Marcin Kuzminski <marcin@python-works.com>
parents: 211
diff changeset
43 default = 20
192
f191f99f59c9 full changelog caching, secured changelog with LoginRequired, some minor changes in graph
Marcin Kuzminski <marcin@python-works.com>
parents: 142
diff changeset
44 if request.params.get('size'):
211
a3a7c3e03b76 version bump. Bugfix when changelog parameter was not an int. Added limit for 100 changelogs to view at once.
Marcin Kuzminski <marcin@python-works.com>
parents: 196
diff changeset
45 try:
a3a7c3e03b76 version bump. Bugfix when changelog parameter was not an int. Added limit for 100 changelogs to view at once.
Marcin Kuzminski <marcin@python-works.com>
parents: 196
diff changeset
46 int_size = int(request.params.get('size'))
a3a7c3e03b76 version bump. Bugfix when changelog parameter was not an int. Added limit for 100 changelogs to view at once.
Marcin Kuzminski <marcin@python-works.com>
parents: 196
diff changeset
47 except ValueError:
a3a7c3e03b76 version bump. Bugfix when changelog parameter was not an int. Added limit for 100 changelogs to view at once.
Marcin Kuzminski <marcin@python-works.com>
parents: 196
diff changeset
48 int_size = default
a3a7c3e03b76 version bump. Bugfix when changelog parameter was not an int. Added limit for 100 changelogs to view at once.
Marcin Kuzminski <marcin@python-works.com>
parents: 196
diff changeset
49 int_size = int_size if int_size <= limit else limit
a3a7c3e03b76 version bump. Bugfix when changelog parameter was not an int. Added limit for 100 changelogs to view at once.
Marcin Kuzminski <marcin@python-works.com>
parents: 196
diff changeset
50 c.size = int_size
192
f191f99f59c9 full changelog caching, secured changelog with LoginRequired, some minor changes in graph
Marcin Kuzminski <marcin@python-works.com>
parents: 142
diff changeset
51 session['changelog_size'] = c.size
f191f99f59c9 full changelog caching, secured changelog with LoginRequired, some minor changes in graph
Marcin Kuzminski <marcin@python-works.com>
parents: 142
diff changeset
52 session.save()
142
f7218849798a Changeg graph to changelog, and changelog to shortlog
Marcin Kuzminski <marcin@python-works.com>
parents: 127
diff changeset
53 else:
282
237470e64bb8 switched filters into webhelpers for easy of usage.
Marcin Kuzminski <marcin@python-works.com>
parents: 276
diff changeset
54 c.size = int(session.get('changelog_size', default))
192
f191f99f59c9 full changelog caching, secured changelog with LoginRequired, some minor changes in graph
Marcin Kuzminski <marcin@python-works.com>
parents: 142
diff changeset
55
276
5e59f29edf66 changed way of getting repository in changelog. + some code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents: 252
diff changeset
56 changesets = HgModel().get_repo(c.repo_name)
192
f191f99f59c9 full changelog caching, secured changelog with LoginRequired, some minor changes in graph
Marcin Kuzminski <marcin@python-works.com>
parents: 142
diff changeset
57
f191f99f59c9 full changelog caching, secured changelog with LoginRequired, some minor changes in graph
Marcin Kuzminski <marcin@python-works.com>
parents: 142
diff changeset
58 p = int(request.params.get('page', 1))
282
237470e64bb8 switched filters into webhelpers for easy of usage.
Marcin Kuzminski <marcin@python-works.com>
parents: 276
diff changeset
59 c.total_cs = len(changesets)
237470e64bb8 switched filters into webhelpers for easy of usage.
Marcin Kuzminski <marcin@python-works.com>
parents: 276
diff changeset
60 c.pagination = Page(changesets, page=p, item_count=c.total_cs,
192
f191f99f59c9 full changelog caching, secured changelog with LoginRequired, some minor changes in graph
Marcin Kuzminski <marcin@python-works.com>
parents: 142
diff changeset
61 items_per_page=c.size)
f191f99f59c9 full changelog caching, secured changelog with LoginRequired, some minor changes in graph
Marcin Kuzminski <marcin@python-works.com>
parents: 142
diff changeset
62
288
ab1afe7444f3 Initial graph release.
Marcin Kuzminski <marcin@python-works.com>
parents: 282
diff changeset
63 self._graph(changesets, c.size, p)
142
f7218849798a Changeg graph to changelog, and changelog to shortlog
Marcin Kuzminski <marcin@python-works.com>
parents: 127
diff changeset
64
f7218849798a Changeg graph to changelog, and changelog to shortlog
Marcin Kuzminski <marcin@python-works.com>
parents: 127
diff changeset
65 return render('changelog/changelog.html')
f7218849798a Changeg graph to changelog, and changelog to shortlog
Marcin Kuzminski <marcin@python-works.com>
parents: 127
diff changeset
66
f7218849798a Changeg graph to changelog, and changelog to shortlog
Marcin Kuzminski <marcin@python-works.com>
parents: 127
diff changeset
67
192
f191f99f59c9 full changelog caching, secured changelog with LoginRequired, some minor changes in graph
Marcin Kuzminski <marcin@python-works.com>
parents: 142
diff changeset
68 def _graph(self, repo, size, p):
288
ab1afe7444f3 Initial graph release.
Marcin Kuzminski <marcin@python-works.com>
parents: 282
diff changeset
69 revcount = size
ab1afe7444f3 Initial graph release.
Marcin Kuzminski <marcin@python-works.com>
parents: 282
diff changeset
70 if not repo.revisions:return dumps([]), 0
ab1afe7444f3 Initial graph release.
Marcin Kuzminski <marcin@python-works.com>
parents: 282
diff changeset
71
ab1afe7444f3 Initial graph release.
Marcin Kuzminski <marcin@python-works.com>
parents: 282
diff changeset
72 max_rev = repo.revisions[-1]
ab1afe7444f3 Initial graph release.
Marcin Kuzminski <marcin@python-works.com>
parents: 282
diff changeset
73 offset = 1 if p == 1 else ((p - 1) * revcount)
ab1afe7444f3 Initial graph release.
Marcin Kuzminski <marcin@python-works.com>
parents: 282
diff changeset
74 rev_start = repo.revisions[(-1 * offset)]
ab1afe7444f3 Initial graph release.
Marcin Kuzminski <marcin@python-works.com>
parents: 282
diff changeset
75
ab1afe7444f3 Initial graph release.
Marcin Kuzminski <marcin@python-works.com>
parents: 282
diff changeset
76 revcount = min(max_rev, revcount)
ab1afe7444f3 Initial graph release.
Marcin Kuzminski <marcin@python-works.com>
parents: 282
diff changeset
77 rev_end = max(0, rev_start - revcount)
ab1afe7444f3 Initial graph release.
Marcin Kuzminski <marcin@python-works.com>
parents: 282
diff changeset
78 dag = graph_rev(repo.repo, rev_start, rev_end)
ab1afe7444f3 Initial graph release.
Marcin Kuzminski <marcin@python-works.com>
parents: 282
diff changeset
79
ab1afe7444f3 Initial graph release.
Marcin Kuzminski <marcin@python-works.com>
parents: 282
diff changeset
80 c.dag = tree = list(colored(dag))
ab1afe7444f3 Initial graph release.
Marcin Kuzminski <marcin@python-works.com>
parents: 282
diff changeset
81 data = []
ab1afe7444f3 Initial graph release.
Marcin Kuzminski <marcin@python-works.com>
parents: 282
diff changeset
82 for (id, type, ctx, vtx, edges) in tree:
ab1afe7444f3 Initial graph release.
Marcin Kuzminski <marcin@python-works.com>
parents: 282
diff changeset
83 if type != CHANGESET:
ab1afe7444f3 Initial graph release.
Marcin Kuzminski <marcin@python-works.com>
parents: 282
diff changeset
84 continue
291
a61b19036de0 code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents: 288
diff changeset
85 data.append(('', vtx, edges))
288
ab1afe7444f3 Initial graph release.
Marcin Kuzminski <marcin@python-works.com>
parents: 282
diff changeset
86
ab1afe7444f3 Initial graph release.
Marcin Kuzminski <marcin@python-works.com>
parents: 282
diff changeset
87 c.jsdata = dumps(data)
142
f7218849798a Changeg graph to changelog, and changelog to shortlog
Marcin Kuzminski <marcin@python-works.com>
parents: 127
diff changeset
88