Changeset - a55c17874486
[Not reviewed]
default
0 6 1
Marcin Kuzminski - 15 years ago 2010-05-30 17:55:56
marcin@python-works.com
Rewrite of user managment, improved forms, added some user info
7 files changed with 131 insertions and 16 deletions:
0 comments (0 inline, 0 general)
pylons_app/controllers/users.py
Show inline comments
 
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
 
from pylons_app.model.db import User, UserLog
 
from pylons_app.model.forms import UserForm
 
from pylons_app.model.user_model import UserModel
 
@@ -34,16 +36,17 @@ class UsersController(BaseController):
 
    
 
    def create(self):
 
        """POST /users: Create a new item"""
 
        # 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:
 
            c.form_errors = errors.error_dict
 
            return htmlfill.render(
 
                 render('admin/users/user_add.html'),
 
@@ -61,20 +64,20 @@ class UsersController(BaseController):
 
        #    <input type="hidden" name="_method" value="PUT" />
 
        # Or using helpers:
 
        #    h.form(url('user', id=ID),
 
        #           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(
 
                 render('admin/users/user_edit.html'),
 
                defaults=errors.value,
 
                encoding="UTF-8")
 
@@ -87,12 +90,13 @@ class UsersController(BaseController):
 
        #    h.form(url('user', id=ID),
 
        #           method='delete')
 
        # url('user', id=ID)
 
        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
 
        return redirect(url('users'))
 
        
 
    def show(self, id, format='html'):
pylons_app/model/forms.py
Show inline comments
 
@@ -16,13 +16,15 @@ ignore_key_missing      False     If Tru
 
<name> = formencode.validators.<name of validator>
 
<name> must equal form name
 
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
 
from pylons_app.model import meta
 
from pylons_app.model.db import User
 
from sqlalchemy.exc import OperationalError
 
@@ -45,13 +47,22 @@ class ValidAuthToken(formencode.validato
 

	
 
    def validate_python(self, value, state):
 

	
 
        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'),
 
            'invalid_login':_('invalid user name'),
 
            'disabled_account':_('Your acccount is disabled')
 
            
 
@@ -67,12 +78,15 @@ class ValidAuth(formencode.validators.Fa
 
        username = value['username']
 
        try:
 
            user = sa.query(User).filter(User.username == username).one()
 
        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:
 
                    from pylons_app.lib.auth import AuthUser
 
                    auth_user = AuthUser()
 
                    auth_user.username = username
 
@@ -121,7 +135,21 @@ 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
pylons_app/model/user_model.py
Show inline comments
 
new file 100644
 
#!/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      
pylons_app/templates/admin/users/user_add.html
Show inline comments
 
@@ -4,13 +4,13 @@
 
<%def name="title()">
 
    ${_('User administration')}
 
</%def>
 
<%def name="breadcrumbs()">
 
	${h.link_to(u'Admin',h.url('admin_home'))}
 
	 /  
 
	 ${_('Users')}
 
	${_('Users')}
 
</%def>
 
<%def name="page_nav()">
 
	${self.menu('admin')}
 
	${self.submenu('users')}
 
</%def>
 
<%def name="main()">
 
@@ -18,24 +18,41 @@
 
        <h2>${_('User')} - ${_('add new')}</h2>
 
        ${h.form(url('users'))}
 
        <table>
 
        	<tr>
 
        		<td>${_('Username')}</td>
 
        		<td>${h.text('username')}</td>
 
        		<td>${self.get_form_error('username')}</td>
 
        	</tr>
 
        	<tr>
 
        		<td>${_('Password')}</td>
 
        		<td>${h.password('password')}</td>
 
        		<td>${self.get_form_error('password')}</td>
 
        	</tr>        	
 
        	<tr>
 
        		<td>${_('Name')}</td>
 
        		<td>${h.text('name')}</td>
 
        		<td>${self.get_form_error('name')}</td>
 
        	</tr>
 
        	<tr>
 
        		<td>${_('password')}</td>
 
        		<td>${h.text('password')}</td>
 
        		<td>${_('Lastname')}</td>
 
        		<td>${h.text('lastname')}</td>
 
        		<td>${self.get_form_error('lastname')}</td>
 
        	</tr>
 
        	<tr>
 
        		<td>${_('Email')}</td>
 
        		<td>${h.text('email')}</td>
 
        		<td>${self.get_form_error('email')}</td>
 
        	</tr>        	        	
 
        	<tr>
 
        		<td>${_('Active')}</td>
 
        		<td>${h.checkbox('active')}</td>
 
        		<td>${h.checkbox('active',value=True)}</td>
 
        		<td>${self.get_form_error('active')}</td>
 
        	</tr>
 
        	<tr>
 
        		<td></td>
 
        		<td>${h.submit('add','add')}</td>
 
        		<td>${h.submit('save','save')}</td>
 
        	</tr>
 
        	        	        	
 
        </table>
 
        ${h.end_form()}
 
    </div>
 
</%def>    
 
\ No newline at end of file
pylons_app/templates/admin/users/user_edit.html
Show inline comments
 
@@ -18,20 +18,38 @@
 
        <h2>${_('User')} - ${c.user.username}</h2>
 
        ${h.form(url('user', id=c.user.user_id),method='put')}
 
        <table>
 
        	<tr>
 
        		<td>${_('Username')}</td>
 
        		<td>${h.text('username')}</td>
 
        		<td>${self.get_form_error('username')}</td>
 
        	</tr>
 
        	<tr>
 
        		<td>${_('New password')}</td>
 
        		<td>${h.text('new_password')}</td>
 
        		<td>${self.get_form_error('new_password')}</td>
 
        	</tr>
 
        	<tr>
 
        		<td>${_('Name')}</td>
 
        		<td>${h.text('name')}</td>
 
        		<td>${self.get_form_error('name')}</td>
 
        	</tr>
 
        	<tr>
 
        		<td>${_('Lastname')}</td>
 
        		<td>${h.text('lastname')}</td>
 
        		<td>${self.get_form_error('lastname')}</td>
 
        	</tr>
 
        	<tr>
 
        		<td>${_('Email')}</td>
 
        		<td>${h.text('email')}</td>
 
        		<td>${self.get_form_error('email')}</td>
 
        	</tr>        	        	
 
        	<tr>
 
        		<td>${_('Active')}</td>
 
        		<td>${h.checkbox('active',value=True)}</td>
 
        		<td>${self.get_form_error('active')}</td>
 
        	</tr>
 
        	<tr>
 
        		<td></td>
 
        		<td>${h.submit('save','save')}</td>
 
        	</tr>
 
        	        	        	
pylons_app/templates/admin/users/users.html
Show inline comments
 
@@ -15,22 +15,24 @@
 
</%def>
 
<%def name="main()">
 
	<div>
 
        <h2>${_('Mercurial users')}</h2>
 
        <table class="table_disp">
 
         <tr class="header">
 
            <td>${_('id')}</td>
 
            <td>${_('username')}</td>
 
            <td>${_('name')}</td>
 
            <td>${_('lastname')}</td>
 
            <td>${_('active')}</td>
 
            <td>${_('admin')}</td>
 
            <td>${_('action')}</td>
 
         </tr>
 
            %for user in c.users_list:
 
                <tr>
 
                    <td>${user.user_id}</td>
 
                    <td>${h.link_to(user.username,h.url('edit_user', id=user.user_id))}</td>
 
                    <td>${user.name}</td>
 
                    <td>${user.lastname}</td>
 
                    <td>${user.active}</td>
 
                    <td>${user.admin}</td>
 
                    <td>
 
	                    ${h.form(url('user', id=user.user_id),method='delete')}
 
	                    	${h.submit('remove','delete',class_="delete_icon action_button")}
 
	                    ${h.end_form()}
pylons_app/templates/login.html
Show inline comments
 
@@ -18,15 +18,13 @@ from pylons_app.lib import filters
 
        <h2>${_('Login')}</h2>
 
        ${h.form(h.url.current())}
 
        <table>
 
            <tr>
 
                <td>${_('Username')}</td>
 
                <td>${h.text('username')}</td>
 
                <td>${self.get_form_error('username')} 
 

	
 
                </td>
 
                <td>${self.get_form_error('username')}</td>
 
            </tr>
 
            <tr>
 
                <td>${_('Password')}</td>
 
                <td>${h.password('password')}</td>
 
                <td>${self.get_form_error('password')}</td> 
 
            </tr>
0 comments (0 inline, 0 general)