##// 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 1 from formencode import htmlfill
2 2 from pylons import request, response, session, tmpl_context as c, url, \
3 3 app_globals as g
4 from pylons.i18n.translation import _
5 from pylons_app.lib import helpers as h
4 6 from pylons.controllers.util import abort, redirect
5 7 from pylons_app.lib.auth import LoginRequired
6 8 from pylons_app.lib.base import BaseController, render
@@ -37,10 +39,11 b' class UsersController(BaseController):'
37 39 # url('users')
38 40
39 41 user_model = UserModel()
40 login_form = UserForm()
42 login_form = UserForm()()
41 43 try:
42 44 form_result = login_form.to_python(dict(request.POST))
43 45 user_model.create(form_result)
46 h.flash(_('created user %s') % form_result['username'], category='success')
44 47 return redirect(url('users'))
45 48
46 49 except formencode.Invalid as errors:
@@ -64,14 +67,14 b' class UsersController(BaseController):'
64 67 # method='put')
65 68 # url('user', id=ID)
66 69 user_model = UserModel()
67 login_form = UserForm()
70 login_form = UserForm(edit=True)()
68 71 try:
69 72 form_result = login_form.to_python(dict(request.POST))
70 73 user_model.update(id, form_result)
74 h.flash(_('User updated succesfully'), category='success')
71 75 return redirect(url('users'))
72 76
73 77 except formencode.Invalid as errors:
74 errors.value
75 78 c.user = user_model.get_user(id)
76 79 c.form_errors = errors.error_dict
77 80 return htmlfill.render(
@@ -90,6 +93,7 b' class UsersController(BaseController):'
90 93 try:
91 94 self.sa.delete(self.sa.query(User).get(id))
92 95 self.sa.commit()
96 h.flash(_('sucessfully deleted user'), category='success')
93 97 except:
94 98 self.sa.rollback()
95 99 raise
@@ -19,7 +19,9 b' list=[1,2,3,4,5]'
19 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 25 from pylons import session
24 26 from pylons.i18n.translation import _
25 27 from pylons_app.lib.auth import get_crypt_password
@@ -48,6 +50,15 b' class ValidAuthToken(formencode.validato'
48 50 if value != authentication_token():
49 51 raise formencode.Invalid(self.message('invalid_token', state,
50 52 search_number=value), value, state)
53 class ValidUsername(formencode.validators.FancyValidator):
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)
51 62
52 63 class ValidAuth(formencode.validators.FancyValidator):
53 64 messages = {
@@ -70,6 +81,9 b' class ValidAuth(formencode.validators.Fa'
70 81 except (NoResultFound, MultipleResultsFound, OperationalError) as e:
71 82 log.error(e)
72 83 user = None
84 raise formencode.Invalid(self.message('invalid_password',
85 state=State_obj), value, state,
86 error_dict=self.e_dict)
73 87 if user:
74 88 if user.active:
75 89 if user.username == username and user.password == crypted_passwd:
@@ -124,4 +138,18 b' class LoginForm(formencode.Schema):'
124 138 #chained validators have access to all data
125 139 chained_validators = [ValidAuth]
126 140
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)
127 154
155 return _UserForm
@@ -21,20 +21,37 b''
21 21 <tr>
22 22 <td>${_('Username')}</td>
23 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>
24 30 </tr>
25 31 <tr>
26 <td>${_('password')}</td>
27 <td>${h.text('password')}</td>
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>
28 45 </tr>
29 46 <tr>
30 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 50 </tr>
33 51 <tr>
34 52 <td></td>
35 <td>${h.submit('add','add')}</td>
53 <td>${h.submit('save','save')}</td>
36 54 </tr>
37
38 55 </table>
39 56 ${h.end_form()}
40 57 </div>
@@ -21,14 +21,32 b''
21 21 <tr>
22 22 <td>${_('Username')}</td>
23 23 <td>${h.text('username')}</td>
24 <td>${self.get_form_error('username')}</td>
24 25 </tr>
25 26 <tr>
26 27 <td>${_('New password')}</td>
27 28 <td>${h.text('new_password')}</td>
29 <td>${self.get_form_error('new_password')}</td>
30 </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>
28 45 </tr>
29 46 <tr>
30 47 <td>${_('Active')}</td>
31 48 <td>${h.checkbox('active',value=True)}</td>
49 <td>${self.get_form_error('active')}</td>
32 50 </tr>
33 51 <tr>
34 52 <td></td>
@@ -18,16 +18,18 b''
18 18 <h2>${_('Mercurial users')}</h2>
19 19 <table class="table_disp">
20 20 <tr class="header">
21 <td>${_('id')}</td>
22 21 <td>${_('username')}</td>
22 <td>${_('name')}</td>
23 <td>${_('lastname')}</td>
23 24 <td>${_('active')}</td>
24 25 <td>${_('admin')}</td>
25 26 <td>${_('action')}</td>
26 27 </tr>
27 28 %for user in c.users_list:
28 29 <tr>
29 <td>${user.user_id}</td>
30 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 33 <td>${user.active}</td>
32 34 <td>${user.admin}</td>
33 35 <td>
@@ -21,9 +21,7 b' from pylons_app.lib import filters'
21 21 <tr>
22 22 <td>${_('Username')}</td>
23 23 <td>${h.text('username')}</td>
24 <td>${self.get_form_error('username')}
25
26 </td>
24 <td>${self.get_form_error('username')}</td>
27 25 </tr>
28 26 <tr>
29 27 <td>${_('Password')}</td>
General Comments 0
You need to be logged in to leave comments. Login now