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