annotate pylons_app/controllers/admin.py @ 74:cdf4fda66dd9

Started summary page. Added filters to templates. used by n,self.f.filtername prefixed by n to disable other filters. Few other fixes found
author Marcin Kuzminski <marcin@python-blog.com>
date Mon, 12 Apr 2010 10:29:18 +0200
parents 4df4c0eac619
children 99afa4d28e2b
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
62
4df4c0eac619 Updated admin to show last 5 actions + updated db model
Marcin Kuzminski <marcin@python-blog.com>
parents: 52
diff changeset
15 from pylons_app.model import meta
4df4c0eac619 Updated admin to show last 5 actions + updated db model
Marcin Kuzminski <marcin@python-blog.com>
parents: 52
diff changeset
16 from pylons_app.model.db import Users, UserLogs
43
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
17 log = logging.getLogger(__name__)
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 class AdminController(BaseController):
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
20
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
21 def __before__(self):
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
22 c.staticurl = g.statics
52
25e516447a33 implemented autentication
marcink
parents: 47
diff changeset
23 c.admin_user = session.get('admin_user', False)
45
a886f5eba757 implemented admin page login
marcink
parents: 44
diff changeset
24 c.admin_username = session.get('admin_username')
43
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
25
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
26 def index(self):
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
27 # Return a rendered template
45
a886f5eba757 implemented admin page login
marcink
parents: 44
diff changeset
28 if request.POST:
a886f5eba757 implemented admin page login
marcink
parents: 44
diff changeset
29 #import Login Form validator class
a886f5eba757 implemented admin page login
marcink
parents: 44
diff changeset
30 login_form = LoginForm()
a886f5eba757 implemented admin page login
marcink
parents: 44
diff changeset
31
a886f5eba757 implemented admin page login
marcink
parents: 44
diff changeset
32 try:
a886f5eba757 implemented admin page login
marcink
parents: 44
diff changeset
33 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
34 if auth.admin_auth(c.form_result['username'], c.form_result['password']):
45
a886f5eba757 implemented admin page login
marcink
parents: 44
diff changeset
35 session['admin_user'] = True
a886f5eba757 implemented admin page login
marcink
parents: 44
diff changeset
36 session['admin_username'] = c.form_result['username']
a886f5eba757 implemented admin page login
marcink
parents: 44
diff changeset
37 session.save()
a886f5eba757 implemented admin page login
marcink
parents: 44
diff changeset
38 return redirect(url('admin_home'))
a886f5eba757 implemented admin page login
marcink
parents: 44
diff changeset
39 else:
a886f5eba757 implemented admin page login
marcink
parents: 44
diff changeset
40 raise formencode.Invalid('Login Error', None, None,
a886f5eba757 implemented admin page login
marcink
parents: 44
diff changeset
41 error_dict={'username':'invalid login',
a886f5eba757 implemented admin page login
marcink
parents: 44
diff changeset
42 'password':'invalid password'})
a886f5eba757 implemented admin page login
marcink
parents: 44
diff changeset
43
a886f5eba757 implemented admin page login
marcink
parents: 44
diff changeset
44 except formencode.Invalid, error:
a886f5eba757 implemented admin page login
marcink
parents: 44
diff changeset
45 c.form_result = error.value
a886f5eba757 implemented admin page login
marcink
parents: 44
diff changeset
46 c.form_errors = error.error_dict or {}
a886f5eba757 implemented admin page login
marcink
parents: 44
diff changeset
47 html = render('/admin.html')
a886f5eba757 implemented admin page login
marcink
parents: 44
diff changeset
48
a886f5eba757 implemented admin page login
marcink
parents: 44
diff changeset
49 return htmlfill.render(
a886f5eba757 implemented admin page login
marcink
parents: 44
diff changeset
50 html,
a886f5eba757 implemented admin page login
marcink
parents: 44
diff changeset
51 defaults=c.form_result,
a886f5eba757 implemented admin page login
marcink
parents: 44
diff changeset
52 encoding="UTF-8"
a886f5eba757 implemented admin page login
marcink
parents: 44
diff changeset
53 )
62
4df4c0eac619 Updated admin to show last 5 actions + updated db model
Marcin Kuzminski <marcin@python-blog.com>
parents: 52
diff changeset
54 if c.admin_user:
4df4c0eac619 Updated admin to show last 5 actions + updated db model
Marcin Kuzminski <marcin@python-blog.com>
parents: 52
diff changeset
55 sa = meta.Session
4df4c0eac619 Updated admin to show last 5 actions + updated db model
Marcin Kuzminski <marcin@python-blog.com>
parents: 52
diff changeset
56 c.users_log = sa.query(UserLogs).limit(10).all()
43
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
57 return render('/admin.html')
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 hgrc(self, dirname):
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
60 filename = os.path.join(dirname, '.hg', 'hgrc')
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
61 return filename
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
62
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
63 def add_repo(self, new_repo):
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
64
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
65
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
66 #extra check it can be add since it's the command
44
d924b931b488 Added managment pages.
marcink
parents: 43
diff changeset
67 if new_repo == '_admin':
d924b931b488 Added managment pages.
marcink
parents: 43
diff changeset
68 c.msg = 'DENIED'
43
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
69 c.new_repo = ''
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
70 return render('add.html')
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
71
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
72 new_repo = new_repo.replace(" ", "_")
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
73 new_repo = new_repo.replace("-", "_")
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
74
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
75 try:
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
76 self._create_repo(new_repo)
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
77 c.new_repo = new_repo
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
78 c.msg = 'added repo'
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
79 except Exception as e:
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
80 c.new_repo = 'Exception when adding: %s' % new_repo
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
81 c.msg = str(e)
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
82
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
83 return render('add.html')
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 def _check_repo(self, repo_name):
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
86 p = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
87 config_path = os.path.join(p, 'hgwebdir.config')
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
88
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
89 cp = ConfigParser()
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
90
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
91 cp.read(config_path)
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
92 repos_path = cp.get('paths', '/').replace("**", '')
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
93
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
94 if not repos_path:
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
95 raise Exception('Could not read config !')
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
96
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
97 self.repo_path = os.path.join(repos_path, repo_name)
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
98
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
99 try:
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
100 r = hg.repository(ui.ui(), self.repo_path)
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
101 hg.verify(r)
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
102 #here we hnow that repo exists it was verified
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
103 log.info('%s repo is already created', repo_name)
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
104 raise Exception('Repo exists')
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
105 except RepoError:
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
106 log.info('%s repo is free for creation', repo_name)
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
107 #it means that there is no valid repo there...
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
108 return True
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
109
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 def _create_repo(self, repo_name):
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
112 if repo_name in [None, '', 'add']:
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
113 raise Exception('undefined repo_name of repo')
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
114
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
115 if self._check_repo(repo_name):
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
116 log.info('creating repo %s in %s', repo_name, self.repo_path)
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
117 cmd = """mkdir %s && hg init %s""" \
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
118 % (self.repo_path, self.repo_path)
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
119 os.popen(cmd)