Show More
@@ -1,100 +1,102 b'' | |||||
1 | import logging |
|
1 | import logging | |
|
2 | import os | |||
2 |
|
3 | |||
3 | from pylons import request, response, session, tmpl_context as c, url, app_globals as g |
|
4 | from pylons import request, response, session, tmpl_context as c, url, app_globals as g | |
4 | from pylons.controllers.util import abort, redirect |
|
5 | from pylons.controllers.util import abort, redirect | |
5 |
|
6 | from beaker.cache import region_invalidate | ||
6 | from pylons_app.lib.base import BaseController, render |
|
7 | from pylons_app.lib.base import BaseController, render | |
7 | import os |
|
|||
8 | from pylons_app.lib import auth |
|
8 | from pylons_app.lib import auth | |
9 | from pylons_app.model.forms import LoginForm |
|
9 | from pylons_app.model.forms import LoginForm | |
10 | import formencode |
|
10 | import formencode | |
11 | import formencode.htmlfill as htmlfill |
|
11 | import formencode.htmlfill as htmlfill | |
12 | from pylons_app.model import meta |
|
12 | from pylons_app.model import meta | |
13 | from pylons_app.model.db import Users, UserLogs |
|
13 | from pylons_app.model.db import Users, UserLogs | |
14 | from webhelpers.paginate import Page |
|
14 | from webhelpers.paginate import Page | |
15 | from pylons_app.lib.utils import check_repo |
|
15 | from pylons_app.lib.utils import check_repo | |
16 | log = logging.getLogger(__name__) |
|
16 | log = logging.getLogger(__name__) | |
17 |
|
17 | |||
18 | class AdminController(BaseController): |
|
18 | class AdminController(BaseController): | |
19 |
|
19 | |||
20 | def __before__(self): |
|
20 | def __before__(self): | |
21 |
|
||||
22 | c.admin_user = session.get('admin_user', False) |
|
21 | c.admin_user = session.get('admin_user', False) | |
23 | c.admin_username = session.get('admin_username') |
|
22 | c.admin_username = session.get('admin_username') | |
24 |
|
23 | |||
25 | def index(self): |
|
24 | def index(self): | |
26 | # Return a rendered template |
|
25 | # Return a rendered template | |
27 | if request.POST: |
|
26 | if request.POST: | |
28 | #import Login Form validator class |
|
27 | #import Login Form validator class | |
29 | login_form = LoginForm() |
|
28 | login_form = LoginForm() | |
30 |
|
29 | |||
31 | try: |
|
30 | try: | |
32 | c.form_result = login_form.to_python(dict(request.params)) |
|
31 | c.form_result = login_form.to_python(dict(request.params)) | |
33 | if auth.admin_auth(c.form_result['username'], c.form_result['password']): |
|
32 | if auth.admin_auth(c.form_result['username'], c.form_result['password']): | |
34 | session['admin_user'] = True |
|
33 | session['admin_user'] = True | |
35 | session['admin_username'] = c.form_result['username'] |
|
34 | session['admin_username'] = c.form_result['username'] | |
36 | session.save() |
|
35 | session.save() | |
37 | return redirect(url('admin_home')) |
|
36 | return redirect(url('admin_home')) | |
38 | else: |
|
37 | else: | |
39 | raise formencode.Invalid('Login Error', None, None, |
|
38 | raise formencode.Invalid('Login Error', None, None, | |
40 | error_dict={'username':'invalid login', |
|
39 | error_dict={'username':'invalid login', | |
41 | 'password':'invalid password'}) |
|
40 | 'password':'invalid password'}) | |
42 |
|
41 | |||
43 | except formencode.Invalid, error: |
|
42 | except formencode.Invalid, error: | |
44 | c.form_result = error.value |
|
43 | c.form_result = error.value | |
45 | c.form_errors = error.error_dict or {} |
|
44 | c.form_errors = error.error_dict or {} | |
46 | html = render('admin/admin.html') |
|
45 | html = render('admin/admin.html') | |
47 |
|
46 | |||
48 | return htmlfill.render( |
|
47 | return htmlfill.render( | |
49 | html, |
|
48 | html, | |
50 | defaults=c.form_result, |
|
49 | defaults=c.form_result, | |
51 | encoding="UTF-8" |
|
50 | encoding="UTF-8" | |
52 | ) |
|
51 | ) | |
53 | if c.admin_user: |
|
52 | if c.admin_user: | |
54 | sa = meta.Session |
|
53 | sa = meta.Session | |
55 |
|
54 | |||
56 | users_log = sa.query(UserLogs)\ |
|
55 | users_log = sa.query(UserLogs)\ | |
57 | .order_by(UserLogs.action_date.desc()) |
|
56 | .order_by(UserLogs.action_date.desc()) | |
58 | p = int(request.params.get('page', 1)) |
|
57 | p = int(request.params.get('page', 1)) | |
59 | c.users_log = Page(users_log, page=p, items_per_page=10) |
|
58 | c.users_log = Page(users_log, page=p, items_per_page=10) | |
60 | c.log_data = render('admin/admin_log.html') |
|
59 | c.log_data = render('admin/admin_log.html') | |
61 | if request.params.get('partial'): |
|
60 | if request.params.get('partial'): | |
62 | return c.log_data |
|
61 | return c.log_data | |
63 | return render('admin/admin.html') |
|
62 | return render('admin/admin.html') | |
64 |
|
63 | |||
65 | def hgrc(self, dirname): |
|
64 | def hgrc(self, dirname): | |
66 | filename = os.path.join(dirname, '.hg', 'hgrc') |
|
65 | filename = os.path.join(dirname, '.hg', 'hgrc') | |
67 | return filename |
|
66 | return filename | |
68 |
|
67 | |||
69 | def add_repo(self, new_repo): |
|
68 | def add_repo(self, new_repo): | |
70 |
|
69 | |||
71 |
|
70 | |||
72 | #extra check it can be add since it's the command |
|
71 | #extra check it can be add since it's the command | |
73 | if new_repo == '_admin': |
|
72 | if new_repo == '_admin': | |
74 | c.msg = 'DENIED' |
|
73 | c.msg = 'DENIED' | |
75 | c.new_repo = '' |
|
74 | c.new_repo = '' | |
76 | return render('add.html') |
|
75 | return render('admin/add.html') | |
77 |
|
76 | |||
78 | new_repo = new_repo.replace(" ", "_") |
|
77 | new_repo = new_repo.replace(" ", "_") | |
79 | new_repo = new_repo.replace("-", "_") |
|
78 | new_repo = new_repo.replace("-", "_") | |
80 |
|
79 | |||
81 | try: |
|
80 | try: | |
82 | self._create_repo(new_repo) |
|
81 | self._create_repo(new_repo) | |
83 | c.new_repo = new_repo |
|
82 | c.new_repo = new_repo | |
84 | c.msg = 'added repo' |
|
83 | c.msg = 'added repo' | |
|
84 | from pylons_app.lib.base import _get_repos | |||
|
85 | #clear our cached list for refresh with new repo | |||
|
86 | region_invalidate(_get_repos, None, 'repo_list_2') | |||
85 | except Exception as e: |
|
87 | except Exception as e: | |
86 | c.new_repo = 'Exception when adding: %s' % new_repo |
|
88 | c.new_repo = 'Exception when adding: %s' % new_repo | |
87 | c.msg = str(e) |
|
89 | c.msg = str(e) | |
88 |
|
90 | |||
89 | return render('add.html') |
|
91 | return render('admin/add.html') | |
90 |
|
92 | |||
91 |
|
93 | |||
92 | def _create_repo(self, repo_name): |
|
94 | def _create_repo(self, repo_name): | |
93 | if repo_name in [None, '', 'add']: |
|
95 | if repo_name in [None, '', 'add']: | |
94 | raise Exception('undefined repo_name of repo') |
|
96 | raise Exception('undefined repo_name of repo') | |
95 |
|
97 | repo_path = os.path.join(g.base_path, repo_name) | ||
96 | if check_repo(repo_name, g.base_path): |
|
98 | if check_repo(repo_name, g.base_path): | |
97 |
log.info('creating repo %s in %s', repo_name, |
|
99 | log.info('creating repo %s in %s', repo_name, repo_path) | |
98 | cmd = """mkdir %s && hg init %s""" \ |
|
100 | from vcs.backends.hg import MercurialRepository | |
99 | % (self.repo_path, self.repo_path) |
|
101 | MercurialRepository(repo_path, create=True) | |
100 | os.popen(cmd) |
|
102 |
General Comments 0
You need to be logged in to leave comments.
Login now