##// END OF EJS Templates
Rewrite of user managment, improved forms, added some user info
marcink -
r238:a55c1787 default
parent child Browse files
Show More
@@ -0,0 +1,48 b''
1 #!/usr/bin/env python
2 # encoding: utf-8
3 #
4 # Copyright (c) 2010 marcink. All rights reserved.
5 #
6 from pylons_app.model.db import User
7 from pylons_app.model.meta import Session
8 '''
9 Created on Apr 9, 2010
10
11 @author: marcink
12 '''
13
14 class UserModel(object):
15
16 def __init__(self):
17 self.sa = Session()
18
19 def get_user(self, id):
20 return self.sa.query(User).get(id)
21
22 def create(self, form_data):
23 try:
24 new_user = User()
25 for k, v in form_data.items():
26 setattr(new_user, k, v)
27
28 self.sa.add(new_user)
29 self.sa.commit()
30 except:
31 self.sa.rollback()
32 raise
33
34 def update(self, id, form_data):
35 try:
36 new_user = self.sa.query(User).get(id)
37 for k, v in form_data.items():
38 if k == 'new_password' and v != '':
39
40 new_user.password = v
41 else:
42 setattr(new_user, k, v)
43
44 self.sa.add(new_user)
45 self.sa.commit()
46 except:
47 self.sa.rollback()
48 raise
@@ -1,6 +1,8 b''
1 from formencode import htmlfill
1 from formencode import htmlfill
2 from pylons import request, response, session, tmpl_context as c, url, \
2 from pylons import request, response, session, tmpl_context as c, url, \
3 app_globals as g
3 app_globals as g
4 from pylons.i18n.translation import _
5 from pylons_app.lib import helpers as h
4 from pylons.controllers.util import abort, redirect
6 from pylons.controllers.util import abort, redirect
5 from pylons_app.lib.auth import LoginRequired
7 from pylons_app.lib.auth import LoginRequired
6 from pylons_app.lib.base import BaseController, render
8 from pylons_app.lib.base import BaseController, render
@@ -37,10 +39,11 b' class UsersController(BaseController):'
37 # url('users')
39 # url('users')
38
40
39 user_model = UserModel()
41 user_model = UserModel()
40 login_form = UserForm()
42 login_form = UserForm()()
41 try:
43 try:
42 form_result = login_form.to_python(dict(request.POST))
44 form_result = login_form.to_python(dict(request.POST))
43 user_model.create(form_result)
45 user_model.create(form_result)
46 h.flash(_('created user %s') % form_result['username'], category='success')
44 return redirect(url('users'))
47 return redirect(url('users'))
45
48
46 except formencode.Invalid as errors:
49 except formencode.Invalid as errors:
@@ -64,14 +67,14 b' class UsersController(BaseController):'
64 # method='put')
67 # method='put')
65 # url('user', id=ID)
68 # url('user', id=ID)
66 user_model = UserModel()
69 user_model = UserModel()
67 login_form = UserForm()
70 login_form = UserForm(edit=True)()
68 try:
71 try:
69 form_result = login_form.to_python(dict(request.POST))
72 form_result = login_form.to_python(dict(request.POST))
70 user_model.update(id, form_result)
73 user_model.update(id, form_result)
74 h.flash(_('User updated succesfully'), category='success')
71 return redirect(url('users'))
75 return redirect(url('users'))
72
76
73 except formencode.Invalid as errors:
77 except formencode.Invalid as errors:
74 errors.value
75 c.user = user_model.get_user(id)
78 c.user = user_model.get_user(id)
76 c.form_errors = errors.error_dict
79 c.form_errors = errors.error_dict
77 return htmlfill.render(
80 return htmlfill.render(
@@ -90,6 +93,7 b' class UsersController(BaseController):'
90 try:
93 try:
91 self.sa.delete(self.sa.query(User).get(id))
94 self.sa.delete(self.sa.query(User).get(id))
92 self.sa.commit()
95 self.sa.commit()
96 h.flash(_('sucessfully deleted user'), category='success')
93 except:
97 except:
94 self.sa.rollback()
98 self.sa.rollback()
95 raise
99 raise
@@ -19,7 +19,9 b' list=[1,2,3,4,5]'
19 for SELECT use formencode.All(OneOf(list), Int())
19 for SELECT use formencode.All(OneOf(list), Int())
20
20
21 """
21 """
22 from formencode.validators import UnicodeString, OneOf, Int, Number, Regex
22 from formencode.validators import UnicodeString, OneOf, Int, Number, Regex, \
23 Email, Bool, StringBoolean
24 from formencode import All
23 from pylons import session
25 from pylons import session
24 from pylons.i18n.translation import _
26 from pylons.i18n.translation import _
25 from pylons_app.lib.auth import get_crypt_password
27 from pylons_app.lib.auth import get_crypt_password
@@ -48,7 +50,16 b' class ValidAuthToken(formencode.validato'
48 if value != authentication_token():
50 if value != authentication_token():
49 raise formencode.Invalid(self.message('invalid_token', state,
51 raise formencode.Invalid(self.message('invalid_token', state,
50 search_number=value), value, state)
52 search_number=value), value, state)
53 class ValidUsername(formencode.validators.FancyValidator):
51
54
55 def validate_python(self, value, state):
56 pass
57
58 class ValidPassword(formencode.validators.FancyValidator):
59
60 def to_python(self, value, state):
61 return get_crypt_password(value)
62
52 class ValidAuth(formencode.validators.FancyValidator):
63 class ValidAuth(formencode.validators.FancyValidator):
53 messages = {
64 messages = {
54 'invalid_password':_('invalid password'),
65 'invalid_password':_('invalid password'),
@@ -70,6 +81,9 b' class ValidAuth(formencode.validators.Fa'
70 except (NoResultFound, MultipleResultsFound, OperationalError) as e:
81 except (NoResultFound, MultipleResultsFound, OperationalError) as e:
71 log.error(e)
82 log.error(e)
72 user = None
83 user = None
84 raise formencode.Invalid(self.message('invalid_password',
85 state=State_obj), value, state,
86 error_dict=self.e_dict)
73 if user:
87 if user:
74 if user.active:
88 if user.active:
75 if user.username == username and user.password == crypted_passwd:
89 if user.username == username and user.password == crypted_passwd:
@@ -124,4 +138,18 b' class LoginForm(formencode.Schema):'
124 #chained validators have access to all data
138 #chained validators have access to all data
125 chained_validators = [ValidAuth]
139 chained_validators = [ValidAuth]
126
140
127
141 def UserForm(edit=False):
142 class _UserForm(formencode.Schema):
143 allow_extra_fields = True
144 filter_extra_fields = True
145 username = All(UnicodeString(strip=True, min=3, not_empty=True), ValidUsername)
146 if edit:
147 new_password = All(UnicodeString(strip=True, min=3, not_empty=False), ValidPassword)
148 else:
149 password = All(UnicodeString(strip=True, min=3, not_empty=False), ValidPassword)
150 active = StringBoolean(if_missing=False)
151 name = UnicodeString(strip=True, min=3, not_empty=True)
152 lastname = UnicodeString(strip=True, min=3, not_empty=True)
153 email = Email(not_empty=True)
154
155 return _UserForm
@@ -7,7 +7,7 b''
7 <%def name="breadcrumbs()">
7 <%def name="breadcrumbs()">
8 ${h.link_to(u'Admin',h.url('admin_home'))}
8 ${h.link_to(u'Admin',h.url('admin_home'))}
9 /
9 /
10 ${_('Users')}
10 ${_('Users')}
11 </%def>
11 </%def>
12 <%def name="page_nav()">
12 <%def name="page_nav()">
13 ${self.menu('admin')}
13 ${self.menu('admin')}
@@ -21,20 +21,37 b''
21 <tr>
21 <tr>
22 <td>${_('Username')}</td>
22 <td>${_('Username')}</td>
23 <td>${h.text('username')}</td>
23 <td>${h.text('username')}</td>
24 <td>${self.get_form_error('username')}</td>
25 </tr>
26 <tr>
27 <td>${_('Password')}</td>
28 <td>${h.password('password')}</td>
29 <td>${self.get_form_error('password')}</td>
30 </tr>
31 <tr>
32 <td>${_('Name')}</td>
33 <td>${h.text('name')}</td>
34 <td>${self.get_form_error('name')}</td>
24 </tr>
35 </tr>
25 <tr>
36 <tr>
26 <td>${_('password')}</td>
37 <td>${_('Lastname')}</td>
27 <td>${h.text('password')}</td>
38 <td>${h.text('lastname')}</td>
39 <td>${self.get_form_error('lastname')}</td>
28 </tr>
40 </tr>
29 <tr>
41 <tr>
42 <td>${_('Email')}</td>
43 <td>${h.text('email')}</td>
44 <td>${self.get_form_error('email')}</td>
45 </tr>
46 <tr>
30 <td>${_('Active')}</td>
47 <td>${_('Active')}</td>
31 <td>${h.checkbox('active')}</td>
48 <td>${h.checkbox('active',value=True)}</td>
49 <td>${self.get_form_error('active')}</td>
32 </tr>
50 </tr>
33 <tr>
51 <tr>
34 <td></td>
52 <td></td>
35 <td>${h.submit('add','add')}</td>
53 <td>${h.submit('save','save')}</td>
36 </tr>
54 </tr>
37
38 </table>
55 </table>
39 ${h.end_form()}
56 ${h.end_form()}
40 </div>
57 </div>
@@ -21,14 +21,32 b''
21 <tr>
21 <tr>
22 <td>${_('Username')}</td>
22 <td>${_('Username')}</td>
23 <td>${h.text('username')}</td>
23 <td>${h.text('username')}</td>
24 <td>${self.get_form_error('username')}</td>
24 </tr>
25 </tr>
25 <tr>
26 <tr>
26 <td>${_('New password')}</td>
27 <td>${_('New password')}</td>
27 <td>${h.text('new_password')}</td>
28 <td>${h.text('new_password')}</td>
29 <td>${self.get_form_error('new_password')}</td>
28 </tr>
30 </tr>
29 <tr>
31 <tr>
32 <td>${_('Name')}</td>
33 <td>${h.text('name')}</td>
34 <td>${self.get_form_error('name')}</td>
35 </tr>
36 <tr>
37 <td>${_('Lastname')}</td>
38 <td>${h.text('lastname')}</td>
39 <td>${self.get_form_error('lastname')}</td>
40 </tr>
41 <tr>
42 <td>${_('Email')}</td>
43 <td>${h.text('email')}</td>
44 <td>${self.get_form_error('email')}</td>
45 </tr>
46 <tr>
30 <td>${_('Active')}</td>
47 <td>${_('Active')}</td>
31 <td>${h.checkbox('active',value=True)}</td>
48 <td>${h.checkbox('active',value=True)}</td>
49 <td>${self.get_form_error('active')}</td>
32 </tr>
50 </tr>
33 <tr>
51 <tr>
34 <td></td>
52 <td></td>
@@ -18,16 +18,18 b''
18 <h2>${_('Mercurial users')}</h2>
18 <h2>${_('Mercurial users')}</h2>
19 <table class="table_disp">
19 <table class="table_disp">
20 <tr class="header">
20 <tr class="header">
21 <td>${_('id')}</td>
22 <td>${_('username')}</td>
21 <td>${_('username')}</td>
22 <td>${_('name')}</td>
23 <td>${_('lastname')}</td>
23 <td>${_('active')}</td>
24 <td>${_('active')}</td>
24 <td>${_('admin')}</td>
25 <td>${_('admin')}</td>
25 <td>${_('action')}</td>
26 <td>${_('action')}</td>
26 </tr>
27 </tr>
27 %for user in c.users_list:
28 %for user in c.users_list:
28 <tr>
29 <tr>
29 <td>${user.user_id}</td>
30 <td>${h.link_to(user.username,h.url('edit_user', id=user.user_id))}</td>
30 <td>${h.link_to(user.username,h.url('edit_user', id=user.user_id))}</td>
31 <td>${user.name}</td>
32 <td>${user.lastname}</td>
31 <td>${user.active}</td>
33 <td>${user.active}</td>
32 <td>${user.admin}</td>
34 <td>${user.admin}</td>
33 <td>
35 <td>
@@ -21,9 +21,7 b' from pylons_app.lib import filters'
21 <tr>
21 <tr>
22 <td>${_('Username')}</td>
22 <td>${_('Username')}</td>
23 <td>${h.text('username')}</td>
23 <td>${h.text('username')}</td>
24 <td>${self.get_form_error('username')}
24 <td>${self.get_form_error('username')}</td>
25
26 </td>
27 </tr>
25 </tr>
28 <tr>
26 <tr>
29 <td>${_('Password')}</td>
27 <td>${_('Password')}</td>
General Comments 0
You need to be logged in to leave comments. Login now