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