Mercurial > kallithea
annotate pylons_app/controllers/admin.py @ 105:3ac4350b7848
Html updates and fixes
author | Marcin Kuzminski <marcin@python-works.com> |
---|---|
date | Sun, 25 Apr 2010 22:45:54 +0200 |
parents | 8b06c420491d |
children | 2811259dc12d |
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 |
78
6f524697f79d
Implemented paging to admin user acion log
Marcin Kuzminski <marcin@python-blog.com>
parents:
75
diff
changeset
|
17 from webhelpers.paginate import Page |
43 | 18 log = logging.getLogger(__name__) |
19 | |
20 class AdminController(BaseController): | |
21 | |
22 def __before__(self): | |
101
8b06c420491d
statics moved to pylons.
Marcin Kuzminski <marcin@python-works.com>
parents:
79
diff
changeset
|
23 |
52 | 24 c.admin_user = session.get('admin_user', False) |
45 | 25 c.admin_username = session.get('admin_username') |
43 | 26 |
27 def index(self): | |
28 # Return a rendered template | |
45 | 29 if request.POST: |
30 #import Login Form validator class | |
31 login_form = LoginForm() | |
32 | |
33 try: | |
34 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
|
35 if auth.admin_auth(c.form_result['username'], c.form_result['password']): |
45 | 36 session['admin_user'] = True |
37 session['admin_username'] = c.form_result['username'] | |
38 session.save() | |
39 return redirect(url('admin_home')) | |
40 else: | |
41 raise formencode.Invalid('Login Error', None, None, | |
42 error_dict={'username':'invalid login', | |
43 'password':'invalid password'}) | |
44 | |
45 except formencode.Invalid, error: | |
46 c.form_result = error.value | |
47 c.form_errors = error.error_dict or {} | |
48 html = render('/admin.html') | |
49 | |
50 return htmlfill.render( | |
51 html, | |
52 defaults=c.form_result, | |
53 encoding="UTF-8" | |
54 ) | |
62
4df4c0eac619
Updated admin to show last 5 actions + updated db model
Marcin Kuzminski <marcin@python-blog.com>
parents:
52
diff
changeset
|
55 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
|
56 sa = meta.Session |
78
6f524697f79d
Implemented paging to admin user acion log
Marcin Kuzminski <marcin@python-blog.com>
parents:
75
diff
changeset
|
57 |
6f524697f79d
Implemented paging to admin user acion log
Marcin Kuzminski <marcin@python-blog.com>
parents:
75
diff
changeset
|
58 users_log = sa.query(UserLogs)\ |
6f524697f79d
Implemented paging to admin user acion log
Marcin Kuzminski <marcin@python-blog.com>
parents:
75
diff
changeset
|
59 .order_by(UserLogs.action_date.desc()) |
6f524697f79d
Implemented paging to admin user acion log
Marcin Kuzminski <marcin@python-blog.com>
parents:
75
diff
changeset
|
60 p = int(request.params.get('page', 1)) |
6f524697f79d
Implemented paging to admin user acion log
Marcin Kuzminski <marcin@python-blog.com>
parents:
75
diff
changeset
|
61 c.users_log = Page(users_log, page=p, items_per_page=10) |
79
9fe23fdab9e9
Implemented AJAH paging
Marcin Kuzminski <marcin@python-blog.com>
parents:
78
diff
changeset
|
62 c.log_data = render('admin_log.html') |
9fe23fdab9e9
Implemented AJAH paging
Marcin Kuzminski <marcin@python-blog.com>
parents:
78
diff
changeset
|
63 if request.params.get('partial'): |
9fe23fdab9e9
Implemented AJAH paging
Marcin Kuzminski <marcin@python-blog.com>
parents:
78
diff
changeset
|
64 return c.log_data |
43 | 65 return render('/admin.html') |
66 | |
67 def hgrc(self, dirname): | |
68 filename = os.path.join(dirname, '.hg', 'hgrc') | |
69 return filename | |
70 | |
71 def add_repo(self, new_repo): | |
72 | |
73 | |
74 #extra check it can be add since it's the command | |
44 | 75 if new_repo == '_admin': |
76 c.msg = 'DENIED' | |
43 | 77 c.new_repo = '' |
78 return render('add.html') | |
79 | |
80 new_repo = new_repo.replace(" ", "_") | |
81 new_repo = new_repo.replace("-", "_") | |
82 | |
83 try: | |
84 self._create_repo(new_repo) | |
85 c.new_repo = new_repo | |
86 c.msg = 'added repo' | |
87 except Exception as e: | |
88 c.new_repo = 'Exception when adding: %s' % new_repo | |
89 c.msg = str(e) | |
90 | |
91 return render('add.html') | |
92 | |
93 def _check_repo(self, repo_name): | |
94 p = os.path.dirname(os.path.dirname(os.path.dirname(__file__))) | |
95 config_path = os.path.join(p, 'hgwebdir.config') | |
96 | |
97 cp = ConfigParser() | |
98 | |
99 cp.read(config_path) | |
100 repos_path = cp.get('paths', '/').replace("**", '') | |
101 | |
102 if not repos_path: | |
103 raise Exception('Could not read config !') | |
104 | |
105 self.repo_path = os.path.join(repos_path, repo_name) | |
106 | |
107 try: | |
108 r = hg.repository(ui.ui(), self.repo_path) | |
109 hg.verify(r) | |
110 #here we hnow that repo exists it was verified | |
111 log.info('%s repo is already created', repo_name) | |
112 raise Exception('Repo exists') | |
113 except RepoError: | |
114 log.info('%s repo is free for creation', repo_name) | |
115 #it means that there is no valid repo there... | |
116 return True | |
117 | |
118 | |
119 def _create_repo(self, repo_name): | |
120 if repo_name in [None, '', 'add']: | |
121 raise Exception('undefined repo_name of repo') | |
122 | |
123 if self._check_repo(repo_name): | |
124 log.info('creating repo %s in %s', repo_name, self.repo_path) | |
125 cmd = """mkdir %s && hg init %s""" \ | |
126 % (self.repo_path, self.repo_path) | |
127 os.popen(cmd) |