Mercurial > kallithea
annotate pylons_app/controllers/hg.py @ 57:e96bc5a01490
Implemented main page sorting
author | Marcin Kuzminski <marcin@python-blog.com> |
---|---|
date | Fri, 09 Apr 2010 01:42:48 +0200 |
parents | bf1b64046c79 |
children | 8fb1abd4178a |
rev | line source |
---|---|
0 | 1 #!/usr/bin/python |
2 # -*- coding: utf-8 -*- | |
3 import logging | |
55
e00dccb6f211
Implemented index page using vcs
Marcin Kuzminski <marcin@python-blog.com>
parents:
43
diff
changeset
|
4 import os |
43 | 5 from pylons_app.lib.base import BaseController |
41
71ffa932799d
Added app basic auth.
Marcin Kuzminski <marcin@python-blog.com>
parents:
37
diff
changeset
|
6 from pylons import tmpl_context as c, app_globals as g, session, request, config |
22 | 7 from pylons_app.lib import helpers as h |
0 | 8 from mako.template import Template |
37
707dfdb1c7a8
Bugfix when client is using old mercurial version and not setting http accept
marcink
parents:
32
diff
changeset
|
9 from pylons.controllers.util import abort |
55
e00dccb6f211
Implemented index page using vcs
Marcin Kuzminski <marcin@python-blog.com>
parents:
43
diff
changeset
|
10 from pylons_app.lib.base import BaseController, render |
e00dccb6f211
Implemented index page using vcs
Marcin Kuzminski <marcin@python-blog.com>
parents:
43
diff
changeset
|
11 try: |
e00dccb6f211
Implemented index page using vcs
Marcin Kuzminski <marcin@python-blog.com>
parents:
43
diff
changeset
|
12 from vcs.backends.hg import get_repositories |
e00dccb6f211
Implemented index page using vcs
Marcin Kuzminski <marcin@python-blog.com>
parents:
43
diff
changeset
|
13 except ImportError: |
e00dccb6f211
Implemented index page using vcs
Marcin Kuzminski <marcin@python-blog.com>
parents:
43
diff
changeset
|
14 print 'You have to import vcs module' |
e00dccb6f211
Implemented index page using vcs
Marcin Kuzminski <marcin@python-blog.com>
parents:
43
diff
changeset
|
15 from mercurial.util import matchdate, Abort, makedate |
e00dccb6f211
Implemented index page using vcs
Marcin Kuzminski <marcin@python-blog.com>
parents:
43
diff
changeset
|
16 from mercurial.hgweb.common import get_contact |
56
bf1b64046c79
Added last change translation to 'time ago', added generation of enabled zip archives
Marcin Kuzminski <marcin@python-blog.com>
parents:
55
diff
changeset
|
17 from mercurial.templatefilters import age |
57
e96bc5a01490
Implemented main page sorting
Marcin Kuzminski <marcin@python-blog.com>
parents:
56
diff
changeset
|
18 from operator import itemgetter |
10
525ed90e4577
major app speedup moved the wsgi creation to app globals, in order to make it run only once.
Marcin Kuzminski
parents:
8
diff
changeset
|
19 log = logging.getLogger(__name__) |
0 | 20 |
21 class HgController(BaseController): | |
21 | 22 |
23 def __before__(self): | |
41
71ffa932799d
Added app basic auth.
Marcin Kuzminski <marcin@python-blog.com>
parents:
37
diff
changeset
|
24 c.repos_prefix = config['repos_name'] |
55
e00dccb6f211
Implemented index page using vcs
Marcin Kuzminski <marcin@python-blog.com>
parents:
43
diff
changeset
|
25 c.staticurl = g.statics |
e00dccb6f211
Implemented index page using vcs
Marcin Kuzminski <marcin@python-blog.com>
parents:
43
diff
changeset
|
26 |
e00dccb6f211
Implemented index page using vcs
Marcin Kuzminski <marcin@python-blog.com>
parents:
43
diff
changeset
|
27 def index(self): |
e00dccb6f211
Implemented index page using vcs
Marcin Kuzminski <marcin@python-blog.com>
parents:
43
diff
changeset
|
28 c.repos_list = [] |
57
e96bc5a01490
Implemented main page sorting
Marcin Kuzminski <marcin@python-blog.com>
parents:
56
diff
changeset
|
29 c.current_sort = request.GET.get('sort', 'name') |
55
e00dccb6f211
Implemented index page using vcs
Marcin Kuzminski <marcin@python-blog.com>
parents:
43
diff
changeset
|
30 |
e00dccb6f211
Implemented index page using vcs
Marcin Kuzminski <marcin@python-blog.com>
parents:
43
diff
changeset
|
31 def get_mtime(spath): |
e00dccb6f211
Implemented index page using vcs
Marcin Kuzminski <marcin@python-blog.com>
parents:
43
diff
changeset
|
32 cl_path = os.path.join(spath, "00changelog.i") |
e00dccb6f211
Implemented index page using vcs
Marcin Kuzminski <marcin@python-blog.com>
parents:
43
diff
changeset
|
33 if os.path.exists(cl_path): |
e00dccb6f211
Implemented index page using vcs
Marcin Kuzminski <marcin@python-blog.com>
parents:
43
diff
changeset
|
34 return os.stat(cl_path).st_mtime |
e00dccb6f211
Implemented index page using vcs
Marcin Kuzminski <marcin@python-blog.com>
parents:
43
diff
changeset
|
35 else: |
e00dccb6f211
Implemented index page using vcs
Marcin Kuzminski <marcin@python-blog.com>
parents:
43
diff
changeset
|
36 return os.stat(spath).st_mtime |
56
bf1b64046c79
Added last change translation to 'time ago', added generation of enabled zip archives
Marcin Kuzminski <marcin@python-blog.com>
parents:
55
diff
changeset
|
37 |
bf1b64046c79
Added last change translation to 'time ago', added generation of enabled zip archives
Marcin Kuzminski <marcin@python-blog.com>
parents:
55
diff
changeset
|
38 def archivelist(ui, nodeid, url): |
bf1b64046c79
Added last change translation to 'time ago', added generation of enabled zip archives
Marcin Kuzminski <marcin@python-blog.com>
parents:
55
diff
changeset
|
39 allowed = g.baseui.configlist("web", "allow_archive", untrusted=True) |
bf1b64046c79
Added last change translation to 'time ago', added generation of enabled zip archives
Marcin Kuzminski <marcin@python-blog.com>
parents:
55
diff
changeset
|
40 for i in [('zip', '.zip'), ('gz', '.tar.gz'), ('bz2', '.tar.bz2')]: |
bf1b64046c79
Added last change translation to 'time ago', added generation of enabled zip archives
Marcin Kuzminski <marcin@python-blog.com>
parents:
55
diff
changeset
|
41 if i[0] in allowed or ui.configbool("web", "allow" + i[0], |
bf1b64046c79
Added last change translation to 'time ago', added generation of enabled zip archives
Marcin Kuzminski <marcin@python-blog.com>
parents:
55
diff
changeset
|
42 untrusted=True): |
bf1b64046c79
Added last change translation to 'time ago', added generation of enabled zip archives
Marcin Kuzminski <marcin@python-blog.com>
parents:
55
diff
changeset
|
43 yield {"type" : i[0], "extension": i[1], |
bf1b64046c79
Added last change translation to 'time ago', added generation of enabled zip archives
Marcin Kuzminski <marcin@python-blog.com>
parents:
55
diff
changeset
|
44 "node": nodeid, "url": url} |
57
e96bc5a01490
Implemented main page sorting
Marcin Kuzminski <marcin@python-blog.com>
parents:
56
diff
changeset
|
45 |
55
e00dccb6f211
Implemented index page using vcs
Marcin Kuzminski <marcin@python-blog.com>
parents:
43
diff
changeset
|
46 for name, r in get_repositories(g.paths[0][0], g.paths[0][1]).items(): |
e00dccb6f211
Implemented index page using vcs
Marcin Kuzminski <marcin@python-blog.com>
parents:
43
diff
changeset
|
47 last_change = (get_mtime(r.spath), makedate()[1]) |
e00dccb6f211
Implemented index page using vcs
Marcin Kuzminski <marcin@python-blog.com>
parents:
43
diff
changeset
|
48 tip = r.changectx('tip') |
56
bf1b64046c79
Added last change translation to 'time ago', added generation of enabled zip archives
Marcin Kuzminski <marcin@python-blog.com>
parents:
55
diff
changeset
|
49 tmp_d = {} |
bf1b64046c79
Added last change translation to 'time ago', added generation of enabled zip archives
Marcin Kuzminski <marcin@python-blog.com>
parents:
55
diff
changeset
|
50 tmp_d['name'] = name |
57
e96bc5a01490
Implemented main page sorting
Marcin Kuzminski <marcin@python-blog.com>
parents:
56
diff
changeset
|
51 tmp_d['name_sort'] = tmp_d['name'] |
e96bc5a01490
Implemented main page sorting
Marcin Kuzminski <marcin@python-blog.com>
parents:
56
diff
changeset
|
52 tmp_d['description'] = r.ui.config('web', 'description', 'Unknown', untrusted=True) |
e96bc5a01490
Implemented main page sorting
Marcin Kuzminski <marcin@python-blog.com>
parents:
56
diff
changeset
|
53 tmp_d['description_sort'] = tmp_d['description'] |
56
bf1b64046c79
Added last change translation to 'time ago', added generation of enabled zip archives
Marcin Kuzminski <marcin@python-blog.com>
parents:
55
diff
changeset
|
54 tmp_d['last_change'] = age(last_change) |
57
e96bc5a01490
Implemented main page sorting
Marcin Kuzminski <marcin@python-blog.com>
parents:
56
diff
changeset
|
55 tmp_d['last_change_sort'] = last_change[1] - last_change[0] |
56
bf1b64046c79
Added last change translation to 'time ago', added generation of enabled zip archives
Marcin Kuzminski <marcin@python-blog.com>
parents:
55
diff
changeset
|
56 tmp_d['tip'] = str(tip) |
57
e96bc5a01490
Implemented main page sorting
Marcin Kuzminski <marcin@python-blog.com>
parents:
56
diff
changeset
|
57 tmp_d['tip_sort'] = tip.rev() |
56
bf1b64046c79
Added last change translation to 'time ago', added generation of enabled zip archives
Marcin Kuzminski <marcin@python-blog.com>
parents:
55
diff
changeset
|
58 tmp_d['rev'] = tip.rev() |
bf1b64046c79
Added last change translation to 'time ago', added generation of enabled zip archives
Marcin Kuzminski <marcin@python-blog.com>
parents:
55
diff
changeset
|
59 tmp_d['contact'] = get_contact(r.ui.config) |
57
e96bc5a01490
Implemented main page sorting
Marcin Kuzminski <marcin@python-blog.com>
parents:
56
diff
changeset
|
60 tmp_d['contact_sort'] = get_contact(r.ui.config) |
56
bf1b64046c79
Added last change translation to 'time ago', added generation of enabled zip archives
Marcin Kuzminski <marcin@python-blog.com>
parents:
55
diff
changeset
|
61 tmp_d['repo_archives'] = archivelist(r.ui, "tip", 'sa') |
bf1b64046c79
Added last change translation to 'time ago', added generation of enabled zip archives
Marcin Kuzminski <marcin@python-blog.com>
parents:
55
diff
changeset
|
62 |
bf1b64046c79
Added last change translation to 'time ago', added generation of enabled zip archives
Marcin Kuzminski <marcin@python-blog.com>
parents:
55
diff
changeset
|
63 c.repos_list.append(tmp_d) |
57
e96bc5a01490
Implemented main page sorting
Marcin Kuzminski <marcin@python-blog.com>
parents:
56
diff
changeset
|
64 |
e96bc5a01490
Implemented main page sorting
Marcin Kuzminski <marcin@python-blog.com>
parents:
56
diff
changeset
|
65 cs = c.current_sort |
e96bc5a01490
Implemented main page sorting
Marcin Kuzminski <marcin@python-blog.com>
parents:
56
diff
changeset
|
66 c.cs_slug = cs.replace('-', '') |
e96bc5a01490
Implemented main page sorting
Marcin Kuzminski <marcin@python-blog.com>
parents:
56
diff
changeset
|
67 sortables = ['name', 'description', 'last_change', 'tip', 'contact'] |
e96bc5a01490
Implemented main page sorting
Marcin Kuzminski <marcin@python-blog.com>
parents:
56
diff
changeset
|
68 |
e96bc5a01490
Implemented main page sorting
Marcin Kuzminski <marcin@python-blog.com>
parents:
56
diff
changeset
|
69 if cs and c.cs_slug in sortables: |
e96bc5a01490
Implemented main page sorting
Marcin Kuzminski <marcin@python-blog.com>
parents:
56
diff
changeset
|
70 sort_key = c.cs_slug + '_sort' |
e96bc5a01490
Implemented main page sorting
Marcin Kuzminski <marcin@python-blog.com>
parents:
56
diff
changeset
|
71 if cs.startswith('-'): |
e96bc5a01490
Implemented main page sorting
Marcin Kuzminski <marcin@python-blog.com>
parents:
56
diff
changeset
|
72 c.repos_list.sort(key=itemgetter(sort_key), reverse=True) |
e96bc5a01490
Implemented main page sorting
Marcin Kuzminski <marcin@python-blog.com>
parents:
56
diff
changeset
|
73 else: |
e96bc5a01490
Implemented main page sorting
Marcin Kuzminski <marcin@python-blog.com>
parents:
56
diff
changeset
|
74 c.repos_list.sort(key=itemgetter(sort_key), reverse=False) |
e96bc5a01490
Implemented main page sorting
Marcin Kuzminski <marcin@python-blog.com>
parents:
56
diff
changeset
|
75 |
55
e00dccb6f211
Implemented index page using vcs
Marcin Kuzminski <marcin@python-blog.com>
parents:
43
diff
changeset
|
76 return render('/index.html') |
21 | 77 |
8 | 78 def view(self, *args, **kwargs): |
55
e00dccb6f211
Implemented index page using vcs
Marcin Kuzminski <marcin@python-blog.com>
parents:
43
diff
changeset
|
79 #TODO: reimplement this not tu use hgwebdir |
21 | 80 response = g.hgapp(request.environ, self.start_response) |
37
707dfdb1c7a8
Bugfix when client is using old mercurial version and not setting http accept
marcink
parents:
32
diff
changeset
|
81 |
707dfdb1c7a8
Bugfix when client is using old mercurial version and not setting http accept
marcink
parents:
32
diff
changeset
|
82 http_accept = request.environ.get('HTTP_ACCEPT', False) |
707dfdb1c7a8
Bugfix when client is using old mercurial version and not setting http accept
marcink
parents:
32
diff
changeset
|
83 if not http_accept: |
707dfdb1c7a8
Bugfix when client is using old mercurial version and not setting http accept
marcink
parents:
32
diff
changeset
|
84 return abort(status_code=400, detail='no http accept in header') |
707dfdb1c7a8
Bugfix when client is using old mercurial version and not setting http accept
marcink
parents:
32
diff
changeset
|
85 |
31
2963f2894a7a
Tempalting change, bugfix for serving raw files, and diffs. Now raw files are not parsed thruough mako, and diffs are mako safe (not parsed also)
Marcin Kuzminski <marcin@python-blog.com>
parents:
22
diff
changeset
|
86 #for mercurial protocols and raw files we can't wrap into mako |
37
707dfdb1c7a8
Bugfix when client is using old mercurial version and not setting http accept
marcink
parents:
32
diff
changeset
|
87 if http_accept.find("mercurial") != -1 or \ |
31
2963f2894a7a
Tempalting change, bugfix for serving raw files, and diffs. Now raw files are not parsed thruough mako, and diffs are mako safe (not parsed also)
Marcin Kuzminski <marcin@python-blog.com>
parents:
22
diff
changeset
|
88 request.environ['PATH_INFO'].find('raw-file') != -1: |
21 | 89 return response |
32
f93b523c0be3
dirty fix for multiple file encodings,
Marcin Kuzminski <marcin@python-blog.com>
parents:
31
diff
changeset
|
90 try: |
f93b523c0be3
dirty fix for multiple file encodings,
Marcin Kuzminski <marcin@python-blog.com>
parents:
31
diff
changeset
|
91 tmpl = u''.join(response) |
f93b523c0be3
dirty fix for multiple file encodings,
Marcin Kuzminski <marcin@python-blog.com>
parents:
31
diff
changeset
|
92 template = Template(tmpl, lookup=request.environ['pylons.pylons']\ |
41
71ffa932799d
Added app basic auth.
Marcin Kuzminski <marcin@python-blog.com>
parents:
37
diff
changeset
|
93 .config['pylons.app_globals'].mako_lookup) |
32
f93b523c0be3
dirty fix for multiple file encodings,
Marcin Kuzminski <marcin@python-blog.com>
parents:
31
diff
changeset
|
94 |
f93b523c0be3
dirty fix for multiple file encodings,
Marcin Kuzminski <marcin@python-blog.com>
parents:
31
diff
changeset
|
95 except (RuntimeError, UnicodeDecodeError): |
f93b523c0be3
dirty fix for multiple file encodings,
Marcin Kuzminski <marcin@python-blog.com>
parents:
31
diff
changeset
|
96 log.info('disabling unicode due to encoding error') |
f93b523c0be3
dirty fix for multiple file encodings,
Marcin Kuzminski <marcin@python-blog.com>
parents:
31
diff
changeset
|
97 response = g.hgapp(request.environ, self.start_response) |
f93b523c0be3
dirty fix for multiple file encodings,
Marcin Kuzminski <marcin@python-blog.com>
parents:
31
diff
changeset
|
98 tmpl = ''.join(response) |
f93b523c0be3
dirty fix for multiple file encodings,
Marcin Kuzminski <marcin@python-blog.com>
parents:
31
diff
changeset
|
99 template = Template(tmpl, lookup=request.environ['pylons.pylons']\ |
41
71ffa932799d
Added app basic auth.
Marcin Kuzminski <marcin@python-blog.com>
parents:
37
diff
changeset
|
100 .config['pylons.app_globals'].mako_lookup, disable_unicode=True) |
31
2963f2894a7a
Tempalting change, bugfix for serving raw files, and diffs. Now raw files are not parsed thruough mako, and diffs are mako safe (not parsed also)
Marcin Kuzminski <marcin@python-blog.com>
parents:
22
diff
changeset
|
101 |
21 | 102 |
31
2963f2894a7a
Tempalting change, bugfix for serving raw files, and diffs. Now raw files are not parsed thruough mako, and diffs are mako safe (not parsed also)
Marcin Kuzminski <marcin@python-blog.com>
parents:
22
diff
changeset
|
103 return template.render(g=g, c=c, session=session, h=h) |