annotate pylons_app/controllers/admin.py @ 47:f6ac79182600

Added rest controllers for repos and users, templating changes + css fixes
author Marcin Kuzminski <marcin@python-blog.com>
date Wed, 07 Apr 2010 20:19:25 +0200
parents 9db7782727b3
children 25e516447a33
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
43
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
1 import logging
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
2
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
3 from pylons import request, response, session, tmpl_context as c, url, app_globals as g
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
4 from pylons.controllers.util import abort, redirect
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
5
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
6 from pylons_app.lib.base import BaseController, render
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
7 import os
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
8 from mercurial import ui, hg
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
9 from mercurial.error import RepoError
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
10 from ConfigParser import ConfigParser
44
d924b931b488 Added managment pages.
marcink
parents: 43
diff changeset
11 from pylons_app.lib import auth
45
a886f5eba757 implemented admin page login
marcink
parents: 44
diff changeset
12 from pylons_app.model.forms import LoginForm
a886f5eba757 implemented admin page login
marcink
parents: 44
diff changeset
13 import formencode
a886f5eba757 implemented admin page login
marcink
parents: 44
diff changeset
14 import formencode.htmlfill as htmlfill
43
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
15 log = logging.getLogger(__name__)
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
16
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
17 class AdminController(BaseController):
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
18
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
19
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
20 def __before__(self):
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
21 c.staticurl = g.statics
45
a886f5eba757 implemented admin page login
marcink
parents: 44
diff changeset
22 c.admin_user = session.get('admin_user')
a886f5eba757 implemented admin page login
marcink
parents: 44
diff changeset
23 c.admin_username = session.get('admin_username')
43
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
24
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
25 def index(self):
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
26 # Return a rendered template
45
a886f5eba757 implemented admin page login
marcink
parents: 44
diff changeset
27 if request.POST:
a886f5eba757 implemented admin page login
marcink
parents: 44
diff changeset
28 #import Login Form validator class
a886f5eba757 implemented admin page login
marcink
parents: 44
diff changeset
29 login_form = LoginForm()
a886f5eba757 implemented admin page login
marcink
parents: 44
diff changeset
30
a886f5eba757 implemented admin page login
marcink
parents: 44
diff changeset
31 try:
a886f5eba757 implemented admin page login
marcink
parents: 44
diff changeset
32 c.form_result = login_form.to_python(dict(request.params))
46
9db7782727b3 Static files for production fixed
Marcin Kuzminski <marcin@python-blog.com>
parents: 45
diff changeset
33 if auth.admin_auth(c.form_result['username'], c.form_result['password']):
45
a886f5eba757 implemented admin page login
marcink
parents: 44
diff changeset
34 session['admin_user'] = True
a886f5eba757 implemented admin page login
marcink
parents: 44
diff changeset
35 session['admin_username'] = c.form_result['username']
a886f5eba757 implemented admin page login
marcink
parents: 44
diff changeset
36 session.save()
a886f5eba757 implemented admin page login
marcink
parents: 44
diff changeset
37 return redirect(url('admin_home'))
a886f5eba757 implemented admin page login
marcink
parents: 44
diff changeset
38 else:
a886f5eba757 implemented admin page login
marcink
parents: 44
diff changeset
39 raise formencode.Invalid('Login Error', None, None,
a886f5eba757 implemented admin page login
marcink
parents: 44
diff changeset
40 error_dict={'username':'invalid login',
a886f5eba757 implemented admin page login
marcink
parents: 44
diff changeset
41 'password':'invalid password'})
a886f5eba757 implemented admin page login
marcink
parents: 44
diff changeset
42
a886f5eba757 implemented admin page login
marcink
parents: 44
diff changeset
43 except formencode.Invalid, error:
a886f5eba757 implemented admin page login
marcink
parents: 44
diff changeset
44 c.form_result = error.value
a886f5eba757 implemented admin page login
marcink
parents: 44
diff changeset
45 c.form_errors = error.error_dict or {}
a886f5eba757 implemented admin page login
marcink
parents: 44
diff changeset
46 html = render('/admin.html')
a886f5eba757 implemented admin page login
marcink
parents: 44
diff changeset
47
a886f5eba757 implemented admin page login
marcink
parents: 44
diff changeset
48 return htmlfill.render(
a886f5eba757 implemented admin page login
marcink
parents: 44
diff changeset
49 html,
a886f5eba757 implemented admin page login
marcink
parents: 44
diff changeset
50 defaults=c.form_result,
a886f5eba757 implemented admin page login
marcink
parents: 44
diff changeset
51 encoding="UTF-8"
a886f5eba757 implemented admin page login
marcink
parents: 44
diff changeset
52 )
43
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
53 return render('/admin.html')
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
54
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
55 def hgrc(self, dirname):
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
56 filename = os.path.join(dirname, '.hg', 'hgrc')
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
57 return filename
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
58
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
59 def add_repo(self, new_repo):
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
60
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
61
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
62 #extra check it can be add since it's the command
44
d924b931b488 Added managment pages.
marcink
parents: 43
diff changeset
63 if new_repo == '_admin':
d924b931b488 Added managment pages.
marcink
parents: 43
diff changeset
64 c.msg = 'DENIED'
43
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
65 c.new_repo = ''
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
66 return render('add.html')
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
67
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
68 new_repo = new_repo.replace(" ", "_")
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
69 new_repo = new_repo.replace("-", "_")
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
70
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
71 try:
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
72 self._create_repo(new_repo)
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
73 c.new_repo = new_repo
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
74 c.msg = 'added repo'
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
75 except Exception as e:
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
76 c.new_repo = 'Exception when adding: %s' % new_repo
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
77 c.msg = str(e)
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
78
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
79 return render('add.html')
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
80
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
81 def _check_repo(self, repo_name):
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
82 p = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
83 config_path = os.path.join(p, 'hgwebdir.config')
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
84
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
85 cp = ConfigParser()
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
86
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
87 cp.read(config_path)
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
88 repos_path = cp.get('paths', '/').replace("**", '')
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
89
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
90 if not repos_path:
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
91 raise Exception('Could not read config !')
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
92
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
93 self.repo_path = os.path.join(repos_path, repo_name)
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
94
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
95 try:
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
96 r = hg.repository(ui.ui(), self.repo_path)
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
97 hg.verify(r)
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
98 #here we hnow that repo exists it was verified
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
99 log.info('%s repo is already created', repo_name)
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
100 raise Exception('Repo exists')
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
101 except RepoError:
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
102 log.info('%s repo is free for creation', repo_name)
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
103 #it means that there is no valid repo there...
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
104 return True
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
105
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
106
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
107 def _create_repo(self, repo_name):
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
108 if repo_name in [None, '', 'add']:
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
109 raise Exception('undefined repo_name of repo')
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
110
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
111 if self._check_repo(repo_name):
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
112 log.info('creating repo %s in %s', repo_name, self.repo_path)
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
113 cmd = """mkdir %s && hg init %s""" \
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
114 % (self.repo_path, self.repo_path)
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
115 os.popen(cmd)