Show More
@@ -1,119 +1,120 b'' | |||
|
1 | 1 | import logging |
|
2 | 2 | |
|
3 | 3 | from pylons import request, response, session, tmpl_context as c, url, app_globals as g |
|
4 | 4 | from pylons.controllers.util import abort, redirect |
|
5 | 5 | |
|
6 | 6 | from pylons_app.lib.base import BaseController, render |
|
7 | 7 | import os |
|
8 | 8 | from mercurial import ui, hg |
|
9 | 9 | from mercurial.error import RepoError |
|
10 | 10 | from ConfigParser import ConfigParser |
|
11 | 11 | from pylons_app.lib import auth |
|
12 | 12 | from pylons_app.model.forms import LoginForm |
|
13 | 13 | import formencode |
|
14 | 14 | import formencode.htmlfill as htmlfill |
|
15 | 15 | from pylons_app.model import meta |
|
16 | 16 | from pylons_app.model.db import Users, UserLogs |
|
17 | 17 | log = logging.getLogger(__name__) |
|
18 | 18 | |
|
19 | 19 | class AdminController(BaseController): |
|
20 | 20 | |
|
21 | 21 | def __before__(self): |
|
22 | 22 | c.staticurl = g.statics |
|
23 | 23 | c.admin_user = session.get('admin_user', False) |
|
24 | 24 | c.admin_username = session.get('admin_username') |
|
25 | 25 | |
|
26 | 26 | def index(self): |
|
27 | 27 | # Return a rendered template |
|
28 | 28 | if request.POST: |
|
29 | 29 | #import Login Form validator class |
|
30 | 30 | login_form = LoginForm() |
|
31 | 31 | |
|
32 | 32 | try: |
|
33 | 33 | c.form_result = login_form.to_python(dict(request.params)) |
|
34 | 34 | if auth.admin_auth(c.form_result['username'], c.form_result['password']): |
|
35 | 35 | session['admin_user'] = True |
|
36 | 36 | session['admin_username'] = c.form_result['username'] |
|
37 | 37 | session.save() |
|
38 | 38 | return redirect(url('admin_home')) |
|
39 | 39 | else: |
|
40 | 40 | raise formencode.Invalid('Login Error', None, None, |
|
41 | 41 | error_dict={'username':'invalid login', |
|
42 | 42 | 'password':'invalid password'}) |
|
43 | 43 | |
|
44 | 44 | except formencode.Invalid, error: |
|
45 | 45 | c.form_result = error.value |
|
46 | 46 | c.form_errors = error.error_dict or {} |
|
47 | 47 | html = render('/admin.html') |
|
48 | 48 | |
|
49 | 49 | return htmlfill.render( |
|
50 | 50 | html, |
|
51 | 51 | defaults=c.form_result, |
|
52 | 52 | encoding="UTF-8" |
|
53 | 53 | ) |
|
54 | 54 | if c.admin_user: |
|
55 | 55 | sa = meta.Session |
|
56 |
c.users_log = sa.query(UserLogs) |
|
|
56 | c.users_log = sa.query(UserLogs)\ | |
|
57 | .order_by(UserLogs.action_date.desc()).limit(10).all() | |
|
57 | 58 | return render('/admin.html') |
|
58 | 59 | |
|
59 | 60 | def hgrc(self, dirname): |
|
60 | 61 | filename = os.path.join(dirname, '.hg', 'hgrc') |
|
61 | 62 | return filename |
|
62 | 63 | |
|
63 | 64 | def add_repo(self, new_repo): |
|
64 | 65 | |
|
65 | 66 | |
|
66 | 67 | #extra check it can be add since it's the command |
|
67 | 68 | if new_repo == '_admin': |
|
68 | 69 | c.msg = 'DENIED' |
|
69 | 70 | c.new_repo = '' |
|
70 | 71 | return render('add.html') |
|
71 | 72 | |
|
72 | 73 | new_repo = new_repo.replace(" ", "_") |
|
73 | 74 | new_repo = new_repo.replace("-", "_") |
|
74 | 75 | |
|
75 | 76 | try: |
|
76 | 77 | self._create_repo(new_repo) |
|
77 | 78 | c.new_repo = new_repo |
|
78 | 79 | c.msg = 'added repo' |
|
79 | 80 | except Exception as e: |
|
80 | 81 | c.new_repo = 'Exception when adding: %s' % new_repo |
|
81 | 82 | c.msg = str(e) |
|
82 | 83 | |
|
83 | 84 | return render('add.html') |
|
84 | 85 | |
|
85 | 86 | def _check_repo(self, repo_name): |
|
86 | 87 | p = os.path.dirname(os.path.dirname(os.path.dirname(__file__))) |
|
87 | 88 | config_path = os.path.join(p, 'hgwebdir.config') |
|
88 | 89 | |
|
89 | 90 | cp = ConfigParser() |
|
90 | 91 | |
|
91 | 92 | cp.read(config_path) |
|
92 | 93 | repos_path = cp.get('paths', '/').replace("**", '') |
|
93 | 94 | |
|
94 | 95 | if not repos_path: |
|
95 | 96 | raise Exception('Could not read config !') |
|
96 | 97 | |
|
97 | 98 | self.repo_path = os.path.join(repos_path, repo_name) |
|
98 | 99 | |
|
99 | 100 | try: |
|
100 | 101 | r = hg.repository(ui.ui(), self.repo_path) |
|
101 | 102 | hg.verify(r) |
|
102 | 103 | #here we hnow that repo exists it was verified |
|
103 | 104 | log.info('%s repo is already created', repo_name) |
|
104 | 105 | raise Exception('Repo exists') |
|
105 | 106 | except RepoError: |
|
106 | 107 | log.info('%s repo is free for creation', repo_name) |
|
107 | 108 | #it means that there is no valid repo there... |
|
108 | 109 | return True |
|
109 | 110 | |
|
110 | 111 | |
|
111 | 112 | def _create_repo(self, repo_name): |
|
112 | 113 | if repo_name in [None, '', 'add']: |
|
113 | 114 | raise Exception('undefined repo_name of repo') |
|
114 | 115 | |
|
115 | 116 | if self._check_repo(repo_name): |
|
116 | 117 | log.info('creating repo %s in %s', repo_name, self.repo_path) |
|
117 | 118 | cmd = """mkdir %s && hg init %s""" \ |
|
118 | 119 | % (self.repo_path, self.repo_path) |
|
119 | 120 | os.popen(cmd) |
@@ -1,85 +1,85 b'' | |||
|
1 | 1 | ## -*- coding: utf-8 -*- |
|
2 | 2 | <%inherit file="base/base.html"/> |
|
3 | 3 | <%def name="get_form_error(element)"> |
|
4 | 4 | %if type(c.form_errors) == dict: |
|
5 | 5 | %if c.form_errors.get(element,False): |
|
6 | 6 | <span class="error-message"> |
|
7 | 7 | ${c.form_errors.get(element,'')} |
|
8 | 8 | </span> |
|
9 | 9 | %endif |
|
10 | 10 | %endif |
|
11 | 11 | </%def> |
|
12 | 12 | <%def name="title()"> |
|
13 | 13 | ${_('Repository managment')} |
|
14 | 14 | </%def> |
|
15 | 15 | <%def name="breadcrumbs()"> |
|
16 | 16 | ${h.link_to(u'Home',h.url('/'))} |
|
17 | 17 | / |
|
18 | 18 | ${h.link_to(u'Admin',h.url('admin_home'))} |
|
19 | 19 | </%def> |
|
20 | 20 | <%def name="page_nav()"> |
|
21 | 21 | <li>${h.link_to(u'Home',h.url('/'))}</li> |
|
22 | 22 | <li class="current">${_('Admin')}</li> |
|
23 | 23 | </%def> |
|
24 | 24 | <%def name="main()"> |
|
25 | 25 | %if c.admin_user: |
|
26 | 26 | <ul class="submenu"> |
|
27 | 27 | <li> |
|
28 | 28 | ${h.link_to(u'Repos',h.url('repos'))} |
|
29 | 29 | </li> |
|
30 | 30 | <li> |
|
31 | 31 | ${h.link_to(u'Users',h.url('users'))} |
|
32 | 32 | </li> |
|
33 | 33 | </ul> |
|
34 | 34 | <br/> |
|
35 | 35 | <div> |
|
36 | 36 | <h2>Welcome ${c.admin_username}</h2> |
|
37 |
<div> |
|
|
37 | <div>${_('Last 10 user actions')</div> | |
|
38 | 38 | %if c.users_log: |
|
39 | 39 | <table> |
|
40 | 40 | <tr> |
|
41 | 41 | <td>${_('Username')}</td> |
|
42 | 42 | <td>${_('Repository')}</td> |
|
43 | 43 | <td>${_('Action')}</td> |
|
44 | 44 | <td>${_('Date')}</td> |
|
45 | 45 | </tr> |
|
46 | 46 | %for cnt,l in enumerate(c.users_log): |
|
47 | 47 | <tr class="parity${cnt%2}"> |
|
48 | 48 | <td>${l.user.username}</td> |
|
49 | 49 | <td>${l.repository}</td> |
|
50 | 50 | <td>${l.action}</td> |
|
51 | 51 | <td>${l.action_date}</td> |
|
52 | 52 | </tr> |
|
53 | 53 | %endfor |
|
54 | 54 | </table> |
|
55 | 55 | %else: |
|
56 | 56 | ${_('No actions yet')} |
|
57 | 57 | %endif |
|
58 | 58 | |
|
59 | 59 | </div> |
|
60 | 60 | %else: |
|
61 | 61 | <div> |
|
62 | 62 | <br /> |
|
63 | 63 | <h2>${_('Login')}</h2> |
|
64 | 64 | ${h.form(h.url.current())} |
|
65 | 65 | <table> |
|
66 | 66 | <tr> |
|
67 | 67 | <td>${_('Username')}</td> |
|
68 | 68 | <td>${h.text('username')}</td> |
|
69 | 69 | <td>${get_form_error('username')} </td> |
|
70 | 70 | </tr> |
|
71 | 71 | <tr> |
|
72 | 72 | <td>${_('Password')}</td> |
|
73 | 73 | <td>${h.password('password')}</td> |
|
74 | 74 | <td>${get_form_error('password')}</td> |
|
75 | 75 | </tr> |
|
76 | 76 | <tr> |
|
77 | 77 | <td></td> |
|
78 | 78 | <td>${h.submit('login','login')}</td> |
|
79 | 79 | </tr> |
|
80 | 80 | </table> |
|
81 | 81 | ${h.end_form()} |
|
82 | 82 | </div> |
|
83 | 83 | %endif |
|
84 | 84 | |
|
85 | 85 | </%def> No newline at end of file |
General Comments 0
You need to be logged in to leave comments.
Login now