Differences between revisions 2 and 11 (spanning 9 versions)
Revision 2 as of 2009-11-19 03:35:51
Size: 2261
Editor: BenGolding
Comment:
Revision 11 as of 2011-02-15 06:05:18
Size: 2339
Editor: localhost
Comment: converted to 1.6 markup
Deletions are marked like this. Additions are marked like this.
Line 10: Line 10:
class Login: class User:
Line 17: Line 17:
            pw = pwd.getpwent(self.username)             pw = pwd.getpwnam(self.username)
Line 26: Line 26:
from albatross.form import * from albatross.ext.form import *
Line 29: Line 29:
    def __init__(self, login):
        elements = Fieldset((
    def __init__(self, user):
        fields = (
Line 33: Line 33:
        ))         )
        fieldsets = (Fieldset(fields), )
Line 37: Line 38:
        FieldsetForm.__init__(self, 'User login', (elements, ), buttons=buttons)         FieldsetForm.__init__(self, 'User login', fieldsets, buttons=buttons)
Line 39: Line 40:
        self.load(login)         self.load(user)
Line 46: Line 47:
    if not ctx.has_value('login'):
        ctx.locals.login = Login('', '')
        ctx.add_session_vars('login')
        ctx.locals.login_form = LoginForm(ctx.locals.login)
    if not ctx.has_value('user'):
        ctx.locals.user = User('', '')
        ctx.add_session_vars('user')
        ctx.locals.login_form = LoginForm(ctx.locals.user)
Line 51: Line 52:
    ctx.locals.login_error = ''
Line 65: Line 67:
When the user presses the "Login" button, it will come back to our {{{page_process}}} method in {{{login.py}}}. We check if the username and password are correct and punt them into the application proper or tell them they've got it wrong. When the user presses the "Login" button, it will come back to our {{{page_process}}} method in {{{login.py}}}. We check if the username and password are correct and punt them into the application proper (via the "search" page) or tell them they've got it wrong.
Line 71: Line 73:
        ctx.locals.login_form.merge(ctx.locals.login)
        if ctx.locals.login.is_password_valid():
        ctx.locals.login_form.merge(ctx.locals.user)
        if ctx.locals.user.is_password_valid():

A simple example

Here's a simple example of how we could use Albatross Forms to collect a username and password from the user.

We need to define a model class to hold the data:

   1 import pwd, crypt
   2 
   3 class User:
   4     def __init__(self, username, password):
   5         self.username = username
   6         self.password = password
   7 
   8     def is_password_valid(self):
   9         try:
  10             pw = pwd.getpwnam(self.username)
  11         except KeyError:
  12             return False
  13         return (crypt.crypt(self.password, pw.pw_passwd) == pw.pw_passwd)

Next, we need to define a form to display the fields:

   1 from albatross.ext.form import *
   2 
   3 class LoginForm(FieldsetForm):
   4     def __init__(self, user):
   5         fields = (
   6             TextField('Username', 'username'),
   7             PasswordField('Password', 'password'),
   8         )
   9         fieldsets = (Fieldset(fields), )
  10         buttons = Buttons((
  11             Button('Login', 'login'),
  12         ))
  13         FieldsetForm.__init__(self, 'User login', fieldsets, buttons=buttons)
  14         
  15         self.load(user)

We need to create an instance of the Login model and maintain that so that any captured data is retained. In our login.py, we use:

   1 def page_enter(ctx):
   2     if not ctx.has_value('user'):
   3         ctx.locals.user = User('', '')
   4         ctx.add_session_vars('user')
   5         ctx.locals.login_form = LoginForm(ctx.locals.user)
   6         ctx.add_session_vars('login_form')
   7     ctx.locals.login_error = ''

In login.html, to display the form to the user we use:

When the user presses the "Login" button, it will come back to our page_process method in login.py. We check if the username and password are correct and punt them into the application proper (via the "search" page) or tell them they've got it wrong.

   1 def page_process(ctx):
   2     if ctx.req_equals('login'):
   3         # nothing to validate
   4         ctx.locals.login_form.merge(ctx.locals.user)
   5         if ctx.locals.user.is_password_valid():
   6             ctx.redirect('search')
   7         else:
   8             ctx.locals.login_error = 'Login incorrect'

None: Simple_forms_example (last edited 2011-02-15 06:05:18 by localhost)