Return to Snippet

Revision: 12799
at March 29, 2009 09:28 by wackysalut

Initial Code
### My App name = Vigilia

## in vigilia/forms/
class FieldSet(forms.FieldSet):
    def _render(self, **kwargs):
        return render('/fieldset.mako',
    def _render_readonly(self, **kwargs):
        return render('/fieldset_readonly.mako',

    def insert_after(self, after_what, field):
        """Insert a field to be rendered after a given field"""
        idx = self._render_fields._list.index(after_what)
        if idx is not None:
            self.insert_at_index(idx + 1, field)
            raise ValueError('No such field in render_fields: %s' % after_what)

    def insert_at_index(self, idx, field):
        """Insert a field to be rendered before a given field"""
        if field.key in self._render_fields._list:
        self._render_fields._list.insert(idx, field.key)
        self._render_fields[field.key] = field

class RadioSetRenderer(fields.RadioSet):
    widget = staticmethod(
    def render(self, options, **kwargs):
        self.radios = []
        self.options = opts = options
        for i, (choice_name, choice_value) in enumerate(opts):
            choice_id = '%s_%i' % (, i)
            radio = self.widget(, choice_name, id=choice_id,
                            checked=self._is_checked(choice_value), **kwargs)
            label = h.make_tag('label', c=radio + ' %s' % choice_value)
        return h.make_tag('div', c=h.make_tag("br").join(self.radios))

class CheckboxRenderer(RadioSetRenderer):
    widget = staticmethod(h.checkbox)

class EmailSmsRenderer(fields.TextFieldRenderer):

class PermissionField(fields.Field):
    is_collection = False
    is_composite_foreign_key = False

class PermissionsRenderer(fields.SelectFieldRenderer):
    def render(self, **kwargs):
        return, [p.permission for p in self.field.model.permissions],
              , multiple=True)
    def deserialize(self):
        # This simple calculation makes sure we don't delete rows that
        # haven't changed. We only add rows that were actually added,
        # and remove the rows that were actually deleted from the last
        # saved records.
        u = self.field.model

        p1 = set([perm.permission for perm in u.permissions])
        p2 = set(self._params.getall(
        p_rem = list(p1.difference(p2))
        p_add = list(p2.difference(p1))

        # Remove unwanted permissions..
        u.permissions = [perm for perm in u.permissions
                         if perm.permission not in p_rem]
        # Add new permissions
        for x in p_add:

        return None

def password_validator(value, field):
    if field.parent.passwd1.value != value:
        raise validators.ValidationError('Les deux mots de passes ne concordent pas')

## in vigilia/model/

# Permissions definitions
permissions_list = [
    ('admin', u'Administration globale'),
    ('moderation', u'Modération des commentaires'),
    ('posts_admin', u'Administration des posts'),
    ('posts_post', u'Poster un événement'),
    ('events_admin', u'Gestion des Activités'),
    ('logs_view', u'Visionnement des logs'),

# Users
users_t = Table('users', metadata,
                Column('id', Integer, primary_key=True),
                Column('username', String(25)),
                Column('password', String(50)),
                Column('firstname', Unicode(80)),
                Column('lastname', Unicode(80)),
                Column('gender', String(5), default='M'),
                Column('email', Unicode(255)),
                Column('email_sms', Unicode(150)),
                Column('photo', Unicode(255)),  # Lien relatif à une variable
                                               # de configuration, vers l'image
                Column('telephone', Unicode(100)),
                Column('dob', Date), # Date of birth - Sans garder l'année
                Column('pref_email_new_post', Boolean),
                Column('pref_email_updt_post', Boolean),
                Column('pref_email_sms_post', Boolean), # Only when asked for
                Column('pref_email_new_event', Boolean),
                Column('pref_email_updt_event', Boolean),

class User(object):
    def fullname(self):
        """Create the full-name representation of the user."""
        if not self.firstname:
            return unicode(self.username)
        return self.firstname + \
               (' %s' % self.lastname if self.lastname else '')

    def __str__(self):
        return self.fullname

    def __repr__(self):
        return "<User: %s>" % self.username

    def __cmp__(self, other):
        if self.firstname is None or other.firstname is None:
            return 0
        n1 = self.firstname + self.lastname
        n2 = other.firstname + other.lastname
        return cmp(n1, n2)

    def has_perm(self, *args):
        """Checks if user has certain permissions"""
        return has_perm(permissions=[p.permission for p in self.permissions],

mapper(User, users_t, {
    'post_answers': relation(PostAnswer, lazy=True, backref='answer_user'),
    'recipient_to': relation(Recipient, backref='user'),
    'last_viewed': relation(LastViewed, backref='user'),
    'permissions': relation(Permission),
    'openids': relation(OpenID, backref='user'),

## in vigilia/controller/

from vigilia.lib.base import *

log = logging.getLogger(__name__)

def gen_fieldset(mdl):
    fs = forms.FieldSet(mdl)
    fs.add(forms.Field('alert', value='').label(u'E-mail notification')\
                .checkbox([('X', u'Envoyer un courriel avec ces informations '\
                                  'au nouvel usager')])\
    inc = [fs.username.label(u"Nom d'usager"),
           fs.passwd1.password().label(u'Mot de passe'),
           fs.passwd2.password().label(u'Répétez le mot de passe').validate(forms.password_validator),
           fs.gender.label(u'Genre').radio([('M', 'M'), ('F', 'F')]).with_renderer(forms.RadioSetRenderer),
           fs.lastname.label(u'Nom de famille'),
           fs.email_sms.label(u'Passerelle SMS (courriel)').with_renderer(forms.EmailSmsRenderer),
           fs.dob.label(u'Date de naissance').with_renderer(forms.DateFieldRendererFr),
    return fs

class UsersController(BaseController):

    def index(self):
        c.list = model.User.query().order_by('firstname, lastname').all()

        return render('/users/index.html')

    def edit(self, id):
        # New or modify
        if id:
            c.usr = model.User.query.get(id)
            c.usr = model.User

        c.permissions_list = model.permissions_list

        fs = gen_fieldset(c.usr)
        fs = fs.bind(c.usr, data=request.POST or None)
        if not id:
            # Require password if it's first adding..
            fs.insert_after('passwd2', fs.alert)

        if request.POST and fs.validate():
            if fs.passwd1.value:
                fs.model.password = fs.passwd1.value
                push_flash(INFO, u"Le mot de passe de l'usager a été modifié")
            if fs.alert.value:
                ### TODO: send a notification e-mail here
                push_flash(INFO, u"Un courriel de notification a été envoyé")
            push_flash(SUCCESS, u"Usager sauvegardé avec succès")
            redirect_to(controller='users', action='index')

        c.fieldset = fs
        return render('/users/edit.html')

## in vigilia/lib/

rom vigilia import forms

Initial URL

Initial Description
This is based on FormEncode 1.2 + two patches you get here:
and here:
(at this point, they might be included in the official release).

Initial Title
FormAlchemy example with custom validator, renderers, forms - Pylons app

Initial Tags

Initial Language