annotate pylons_app/controllers/admin.py @ 75:99afa4d28e2b

Changed order of user actions log
author Marcin Kuzminski <marcin@python-blog.com>
date Tue, 13 Apr 2010 22:30:52 +0200
parents 4df4c0eac619
children 6f524697f79d
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
75
99afa4d28e2b Changed order of user actions log
Marcin Kuzminski <marcin@python-blog.com>
parents: 62
diff changeset
56 c.users_log = sa.query(UserLogs)\
99afa4d28e2b Changed order of user actions log
Marcin Kuzminski <marcin@python-blog.com>
parents: 62
diff changeset
57 .order_by(UserLogs.action_date.desc()).limit(10).all()
43
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
58 return render('/admin.html')
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
59
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
60 def hgrc(self, dirname):
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
61 filename = os.path.join(dirname, '.hg', 'hgrc')
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
62 return filename
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
63
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
64 def add_repo(self, new_repo):
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
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
67 #extra check it can be add since it's the command
44
d924b931b488 Added managment pages.
marcink
parents: 43
diff changeset
68 if new_repo == '_admin':
d924b931b488 Added managment pages.
marcink
parents: 43
diff changeset
69 c.msg = 'DENIED'
43
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
70 c.new_repo = ''
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
71 return render('add.html')
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
72
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 new_repo = new_repo.replace("-", "_")
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
75
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
76 try:
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
77 self._create_repo(new_repo)
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
78 c.new_repo = new_repo
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
79 c.msg = 'added repo'
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
80 except Exception as e:
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
81 c.new_repo = 'Exception when adding: %s' % new_repo
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
82 c.msg = str(e)
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
83
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
84 return render('add.html')
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
85
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
86 def _check_repo(self, repo_name):
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
87 p = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
88 config_path = os.path.join(p, 'hgwebdir.config')
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 cp = ConfigParser()
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
91
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
92 cp.read(config_path)
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
93 repos_path = cp.get('paths', '/').replace("**", '')
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 if not repos_path:
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
96 raise Exception('Could not read config !')
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
97
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
98 self.repo_path = os.path.join(repos_path, repo_name)
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
99
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
100 try:
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
101 r = hg.repository(ui.ui(), self.repo_path)
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
102 hg.verify(r)
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
103 #here we hnow that repo exists it was verified
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
104 log.info('%s repo is already created', repo_name)
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
105 raise Exception('Repo exists')
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
106 except RepoError:
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
107 log.info('%s repo is free for creation', repo_name)
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
108 #it means that there is no valid repo there...
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
109 return True
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
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
112 def _create_repo(self, repo_name):
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
113 if repo_name in [None, '', 'add']:
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
114 raise Exception('undefined repo_name of repo')
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
115
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
116 if self._check_repo(repo_name):
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
117 log.info('creating repo %s in %s', repo_name, self.repo_path)
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
118 cmd = """mkdir %s && hg init %s""" \
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
119 % (self.repo_path, self.repo_path)
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
120 os.popen(cmd)