diff --git a/pylons_app/controllers/users.py b/pylons_app/controllers/users.py --- a/pylons_app/controllers/users.py +++ b/pylons_app/controllers/users.py @@ -1,6 +1,8 @@ from formencode import htmlfill from pylons import request, response, session, tmpl_context as c, url, \ app_globals as g +from pylons.i18n.translation import _ +from pylons_app.lib import helpers as h from pylons.controllers.util import abort, redirect from pylons_app.lib.auth import LoginRequired from pylons_app.lib.base import BaseController, render @@ -37,10 +39,11 @@ class UsersController(BaseController): # url('users') user_model = UserModel() - login_form = UserForm() + login_form = UserForm()() try: form_result = login_form.to_python(dict(request.POST)) user_model.create(form_result) + h.flash(_('created user %s') % form_result['username'], category='success') return redirect(url('users')) except formencode.Invalid as errors: @@ -64,14 +67,14 @@ class UsersController(BaseController): # method='put') # url('user', id=ID) user_model = UserModel() - login_form = UserForm() + login_form = UserForm(edit=True)() try: form_result = login_form.to_python(dict(request.POST)) user_model.update(id, form_result) + h.flash(_('User updated succesfully'), category='success') return redirect(url('users')) except formencode.Invalid as errors: - errors.value c.user = user_model.get_user(id) c.form_errors = errors.error_dict return htmlfill.render( @@ -90,6 +93,7 @@ class UsersController(BaseController): try: self.sa.delete(self.sa.query(User).get(id)) self.sa.commit() + h.flash(_('sucessfully deleted user'), category='success') except: self.sa.rollback() raise diff --git a/pylons_app/model/forms.py b/pylons_app/model/forms.py --- a/pylons_app/model/forms.py +++ b/pylons_app/model/forms.py @@ -19,7 +19,9 @@ list=[1,2,3,4,5] for SELECT use formencode.All(OneOf(list), Int()) """ -from formencode.validators import UnicodeString, OneOf, Int, Number, Regex +from formencode.validators import UnicodeString, OneOf, Int, Number, Regex, \ + Email, Bool, StringBoolean +from formencode import All from pylons import session from pylons.i18n.translation import _ from pylons_app.lib.auth import get_crypt_password @@ -48,7 +50,16 @@ class ValidAuthToken(formencode.validato if value != authentication_token(): raise formencode.Invalid(self.message('invalid_token', state, search_number=value), value, state) +class ValidUsername(formencode.validators.FancyValidator): + def validate_python(self, value, state): + pass + +class ValidPassword(formencode.validators.FancyValidator): + + def to_python(self, value, state): + return get_crypt_password(value) + class ValidAuth(formencode.validators.FancyValidator): messages = { 'invalid_password':_('invalid password'), @@ -70,6 +81,9 @@ class ValidAuth(formencode.validators.Fa except (NoResultFound, MultipleResultsFound, OperationalError) as e: log.error(e) user = None + raise formencode.Invalid(self.message('invalid_password', + state=State_obj), value, state, + error_dict=self.e_dict) if user: if user.active: if user.username == username and user.password == crypted_passwd: @@ -124,4 +138,18 @@ class LoginForm(formencode.Schema): #chained validators have access to all data chained_validators = [ValidAuth] - +def UserForm(edit=False): + class _UserForm(formencode.Schema): + allow_extra_fields = True + filter_extra_fields = True + username = All(UnicodeString(strip=True, min=3, not_empty=True), ValidUsername) + if edit: + new_password = All(UnicodeString(strip=True, min=3, not_empty=False), ValidPassword) + else: + password = All(UnicodeString(strip=True, min=3, not_empty=False), ValidPassword) + active = StringBoolean(if_missing=False) + name = UnicodeString(strip=True, min=3, not_empty=True) + lastname = UnicodeString(strip=True, min=3, not_empty=True) + email = Email(not_empty=True) + + return _UserForm diff --git a/pylons_app/model/user_model.py b/pylons_app/model/user_model.py new file mode 100644 --- /dev/null +++ b/pylons_app/model/user_model.py @@ -0,0 +1,48 @@ +#!/usr/bin/env python +# encoding: utf-8 +# +# Copyright (c) 2010 marcink. All rights reserved. +# +from pylons_app.model.db import User +from pylons_app.model.meta import Session +''' +Created on Apr 9, 2010 + +@author: marcink +''' + +class UserModel(object): + + def __init__(self): + self.sa = Session() + + def get_user(self, id): + return self.sa.query(User).get(id) + + def create(self, form_data): + try: + new_user = User() + for k, v in form_data.items(): + setattr(new_user, k, v) + + self.sa.add(new_user) + self.sa.commit() + except: + self.sa.rollback() + raise + + def update(self, id, form_data): + try: + new_user = self.sa.query(User).get(id) + for k, v in form_data.items(): + if k == 'new_password' and v != '': + + new_user.password = v + else: + setattr(new_user, k, v) + + self.sa.add(new_user) + self.sa.commit() + except: + self.sa.rollback() + raise diff --git a/pylons_app/templates/admin/users/user_add.html b/pylons_app/templates/admin/users/user_add.html --- a/pylons_app/templates/admin/users/user_add.html +++ b/pylons_app/templates/admin/users/user_add.html @@ -7,7 +7,7 @@ <%def name="breadcrumbs()"> ${h.link_to(u'Admin',h.url('admin_home'))} / - ${_('Users')} + ${_('Users')} %def> <%def name="page_nav()"> ${self.menu('admin')} @@ -21,20 +21,37 @@
${_('id')} | ${_('username')} | +${_('name')} | +${_('lastname')} | ${_('active')} | ${_('admin')} | ${_('action')} |
${user.user_id} | ${h.link_to(user.username,h.url('edit_user', id=user.user_id))} | +${user.name} | +${user.lastname} | ${user.active} | ${user.admin} | diff --git a/pylons_app/templates/login.html b/pylons_app/templates/login.html --- a/pylons_app/templates/login.html +++ b/pylons_app/templates/login.html @@ -21,9 +21,7 @@ from pylons_app.lib import filters |
${_('Username')} | ${h.text('username')} | -${self.get_form_error('username')} - - | +${self.get_form_error('username')} | |||
${_('Password')} |