annotate pylons_app/controllers/hg.py @ 93:aec4c0071cb3

added empty controllers for branches tags files graph, routing and test for them
author Marcin Kuzminski <marcin@python-works.com>
date Wed, 21 Apr 2010 00:26:11 +0200
parents 670713507d03
children 8b06c420491d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
1 #!/usr/bin/python
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
2 # -*- coding: utf-8 -*-
93
aec4c0071cb3 added empty controllers for branches tags files graph, routing and test for them
Marcin Kuzminski <marcin@python-works.com>
parents: 82
diff changeset
3 from mako.template import Template
aec4c0071cb3 added empty controllers for branches tags files graph, routing and test for them
Marcin Kuzminski <marcin@python-works.com>
parents: 82
diff changeset
4 from mercurial.hg import repository
aec4c0071cb3 added empty controllers for branches tags files graph, routing and test for them
Marcin Kuzminski <marcin@python-works.com>
parents: 82
diff changeset
5 from mercurial.hgweb import hgweb
aec4c0071cb3 added empty controllers for branches tags files graph, routing and test for them
Marcin Kuzminski <marcin@python-works.com>
parents: 82
diff changeset
6 from mercurial.hgweb.request import wsgiapplication
aec4c0071cb3 added empty controllers for branches tags files graph, routing and test for them
Marcin Kuzminski <marcin@python-works.com>
parents: 82
diff changeset
7 from mercurial.localrepo import localrepository
aec4c0071cb3 added empty controllers for branches tags files graph, routing and test for them
Marcin Kuzminski <marcin@python-works.com>
parents: 82
diff changeset
8 from operator import itemgetter
41
71ffa932799d Added app basic auth.
Marcin Kuzminski <marcin@python-blog.com>
parents: 37
diff changeset
9 from pylons import tmpl_context as c, app_globals as g, session, request, config
93
aec4c0071cb3 added empty controllers for branches tags files graph, routing and test for them
Marcin Kuzminski <marcin@python-works.com>
parents: 82
diff changeset
10 from pylons.controllers.util import abort
22
3142616771cd Removed default contact name
Marcin Kuzminski
parents: 21
diff changeset
11 from pylons_app.lib import helpers as h
76
71401840ed86 refactoring update
Marcin Kuzminski <marcin@python-blog.com>
parents: 74
diff changeset
12 from pylons_app.lib.base import BaseController, render
82
670713507d03 Moved summary to seperate controller,
Marcin Kuzminski <marcin@python-blog.com>
parents: 80
diff changeset
13 from pylons_app.lib.utils import get_repo_slug
58
8fb1abd4178a Added hg model,implemented removal of repos, added HgModel for fetching repos(with generator)
Marcin Kuzminski <marcin@python-blog.com>
parents: 57
diff changeset
14 from pylons_app.model.hg_model import HgModel
93
aec4c0071cb3 added empty controllers for branches tags files graph, routing and test for them
Marcin Kuzminski <marcin@python-works.com>
parents: 82
diff changeset
15 import logging
aec4c0071cb3 added empty controllers for branches tags files graph, routing and test for them
Marcin Kuzminski <marcin@python-works.com>
parents: 82
diff changeset
16 import os
aec4c0071cb3 added empty controllers for branches tags files graph, routing and test for them
Marcin Kuzminski <marcin@python-works.com>
parents: 82
diff changeset
17 from beaker.cache import cache_region
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
18 log = logging.getLogger(__name__)
0
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
19
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
20 class HgController(BaseController):
21
fac1f62a1d71 Wrapped into mako templates,
Marcin Kuzminski
parents: 20
diff changeset
21
fac1f62a1d71 Wrapped into mako templates,
Marcin Kuzminski
parents: 20
diff changeset
22 def __before__(self):
41
71ffa932799d Added app basic auth.
Marcin Kuzminski <marcin@python-blog.com>
parents: 37
diff changeset
23 c.repos_prefix = config['repos_name']
55
e00dccb6f211 Implemented index page using vcs
Marcin Kuzminski <marcin@python-blog.com>
parents: 43
diff changeset
24 c.staticurl = g.statics
82
670713507d03 Moved summary to seperate controller,
Marcin Kuzminski <marcin@python-blog.com>
parents: 80
diff changeset
25 c.repo_name = get_repo_slug(request)
670713507d03 Moved summary to seperate controller,
Marcin Kuzminski <marcin@python-blog.com>
parents: 80
diff changeset
26
55
e00dccb6f211 Implemented index page using vcs
Marcin Kuzminski <marcin@python-blog.com>
parents: 43
diff changeset
27 def index(self):
93
aec4c0071cb3 added empty controllers for branches tags files graph, routing and test for them
Marcin Kuzminski <marcin@python-works.com>
parents: 82
diff changeset
28
aec4c0071cb3 added empty controllers for branches tags files graph, routing and test for them
Marcin Kuzminski <marcin@python-works.com>
parents: 82
diff changeset
29
58
8fb1abd4178a Added hg model,implemented removal of repos, added HgModel for fetching repos(with generator)
Marcin Kuzminski <marcin@python-blog.com>
parents: 57
diff changeset
30 hg_model = HgModel()
93
aec4c0071cb3 added empty controllers for branches tags files graph, routing and test for them
Marcin Kuzminski <marcin@python-works.com>
parents: 82
diff changeset
31 @cache_region('short_term', 'repo_list')
aec4c0071cb3 added empty controllers for branches tags files graph, routing and test for them
Marcin Kuzminski <marcin@python-works.com>
parents: 82
diff changeset
32 def _list():
aec4c0071cb3 added empty controllers for branches tags files graph, routing and test for them
Marcin Kuzminski <marcin@python-works.com>
parents: 82
diff changeset
33 return list(hg_model.get_repos())
aec4c0071cb3 added empty controllers for branches tags files graph, routing and test for them
Marcin Kuzminski <marcin@python-works.com>
parents: 82
diff changeset
34
aec4c0071cb3 added empty controllers for branches tags files graph, routing and test for them
Marcin Kuzminski <marcin@python-works.com>
parents: 82
diff changeset
35 c.repos_list = _list()
57
e96bc5a01490 Implemented main page sorting
Marcin Kuzminski <marcin@python-blog.com>
parents: 56
diff changeset
36 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
37
57
e96bc5a01490 Implemented main page sorting
Marcin Kuzminski <marcin@python-blog.com>
parents: 56
diff changeset
38 cs = c.current_sort
e96bc5a01490 Implemented main page sorting
Marcin Kuzminski <marcin@python-blog.com>
parents: 56
diff changeset
39 c.cs_slug = cs.replace('-', '')
e96bc5a01490 Implemented main page sorting
Marcin Kuzminski <marcin@python-blog.com>
parents: 56
diff changeset
40 sortables = ['name', 'description', 'last_change', 'tip', 'contact']
e96bc5a01490 Implemented main page sorting
Marcin Kuzminski <marcin@python-blog.com>
parents: 56
diff changeset
41
e96bc5a01490 Implemented main page sorting
Marcin Kuzminski <marcin@python-blog.com>
parents: 56
diff changeset
42 if cs and c.cs_slug in sortables:
e96bc5a01490 Implemented main page sorting
Marcin Kuzminski <marcin@python-blog.com>
parents: 56
diff changeset
43 sort_key = c.cs_slug + '_sort'
e96bc5a01490 Implemented main page sorting
Marcin Kuzminski <marcin@python-blog.com>
parents: 56
diff changeset
44 if cs.startswith('-'):
e96bc5a01490 Implemented main page sorting
Marcin Kuzminski <marcin@python-blog.com>
parents: 56
diff changeset
45 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
46 else:
e96bc5a01490 Implemented main page sorting
Marcin Kuzminski <marcin@python-blog.com>
parents: 56
diff changeset
47 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
48
55
e00dccb6f211 Implemented index page using vcs
Marcin Kuzminski <marcin@python-blog.com>
parents: 43
diff changeset
49 return render('/index.html')
21
fac1f62a1d71 Wrapped into mako templates,
Marcin Kuzminski
parents: 20
diff changeset
50
93
aec4c0071cb3 added empty controllers for branches tags files graph, routing and test for them
Marcin Kuzminski <marcin@python-works.com>
parents: 82
diff changeset
51 def view(self, environ, start_response, path_info):
aec4c0071cb3 added empty controllers for branches tags files graph, routing and test for them
Marcin Kuzminski <marcin@python-works.com>
parents: 82
diff changeset
52 print path_info
37
707dfdb1c7a8 Bugfix when client is using old mercurial version and not setting http accept
marcink
parents: 32
diff changeset
53
93
aec4c0071cb3 added empty controllers for branches tags files graph, routing and test for them
Marcin Kuzminski <marcin@python-works.com>
parents: 82
diff changeset
54 def app_maker():
aec4c0071cb3 added empty controllers for branches tags files graph, routing and test for them
Marcin Kuzminski <marcin@python-works.com>
parents: 82
diff changeset
55
aec4c0071cb3 added empty controllers for branches tags files graph, routing and test for them
Marcin Kuzminski <marcin@python-works.com>
parents: 82
diff changeset
56 path = os.path.join(g.base_path, c.repo_name)
aec4c0071cb3 added empty controllers for branches tags files graph, routing and test for them
Marcin Kuzminski <marcin@python-works.com>
parents: 82
diff changeset
57 repo = repository(g.baseui, path)
aec4c0071cb3 added empty controllers for branches tags files graph, routing and test for them
Marcin Kuzminski <marcin@python-works.com>
parents: 82
diff changeset
58 hgwebapp = hgweb(repo, c.repo_name)
aec4c0071cb3 added empty controllers for branches tags files graph, routing and test for them
Marcin Kuzminski <marcin@python-works.com>
parents: 82
diff changeset
59 return hgwebapp
aec4c0071cb3 added empty controllers for branches tags files graph, routing and test for them
Marcin Kuzminski <marcin@python-works.com>
parents: 82
diff changeset
60
aec4c0071cb3 added empty controllers for branches tags files graph, routing and test for them
Marcin Kuzminski <marcin@python-works.com>
parents: 82
diff changeset
61 a = wsgiapplication(app_maker)
aec4c0071cb3 added empty controllers for branches tags files graph, routing and test for them
Marcin Kuzminski <marcin@python-works.com>
parents: 82
diff changeset
62 resp = a(environ, start_response)
aec4c0071cb3 added empty controllers for branches tags files graph, routing and test for them
Marcin Kuzminski <marcin@python-works.com>
parents: 82
diff changeset
63
37
707dfdb1c7a8 Bugfix when client is using old mercurial version and not setting http accept
marcink
parents: 32
diff changeset
64 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
65 if not http_accept:
707dfdb1c7a8 Bugfix when client is using old mercurial version and not setting http accept
marcink
parents: 32
diff changeset
66 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
67
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
68 #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
69 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
70 request.environ['PATH_INFO'].find('raw-file') != -1:
93
aec4c0071cb3 added empty controllers for branches tags files graph, routing and test for them
Marcin Kuzminski <marcin@python-works.com>
parents: 82
diff changeset
71 return resp
32
f93b523c0be3 dirty fix for multiple file encodings,
Marcin Kuzminski <marcin@python-blog.com>
parents: 31
diff changeset
72 try:
93
aec4c0071cb3 added empty controllers for branches tags files graph, routing and test for them
Marcin Kuzminski <marcin@python-works.com>
parents: 82
diff changeset
73 tmpl = u''.join(resp)
32
f93b523c0be3 dirty fix for multiple file encodings,
Marcin Kuzminski <marcin@python-blog.com>
parents: 31
diff changeset
74 template = Template(tmpl, lookup=request.environ['pylons.pylons']\
41
71ffa932799d Added app basic auth.
Marcin Kuzminski <marcin@python-blog.com>
parents: 37
diff changeset
75 .config['pylons.app_globals'].mako_lookup)
32
f93b523c0be3 dirty fix for multiple file encodings,
Marcin Kuzminski <marcin@python-blog.com>
parents: 31
diff changeset
76
f93b523c0be3 dirty fix for multiple file encodings,
Marcin Kuzminski <marcin@python-blog.com>
parents: 31
diff changeset
77 except (RuntimeError, UnicodeDecodeError):
f93b523c0be3 dirty fix for multiple file encodings,
Marcin Kuzminski <marcin@python-blog.com>
parents: 31
diff changeset
78 log.info('disabling unicode due to encoding error')
93
aec4c0071cb3 added empty controllers for branches tags files graph, routing and test for them
Marcin Kuzminski <marcin@python-works.com>
parents: 82
diff changeset
79 resp = g.hgapp(request.environ, self.start_response)
aec4c0071cb3 added empty controllers for branches tags files graph, routing and test for them
Marcin Kuzminski <marcin@python-works.com>
parents: 82
diff changeset
80 tmpl = ''.join(resp)
32
f93b523c0be3 dirty fix for multiple file encodings,
Marcin Kuzminski <marcin@python-blog.com>
parents: 31
diff changeset
81 template = Template(tmpl, lookup=request.environ['pylons.pylons']\
41
71ffa932799d Added app basic auth.
Marcin Kuzminski <marcin@python-blog.com>
parents: 37
diff changeset
82 .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
83
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
84 return template.render(g=g, c=c, session=session, h=h)