Differences between revisions 1 and 2
Revision 1 as of 2009-11-19 02:07:06
Size: 1098
Editor: BenGolding
Comment:
Revision 2 as of 2009-11-19 03:35:51
Size: 2261
Editor: BenGolding
Comment:
Deletions are marked like this. Additions are marked like this.
Line 16: Line 16:
        crypted_passwd = pwd.getpwent(self.username).pw_passwd
        crypted_passwd = pwd.getpwnam(username)[1]
        return (crypt.crypt(self.password, crypted_passwd) == crypted_passwd)
        try:
            
pw = pwd.getpwent(self.username)
        except KeyError:
          return False
        return (crypt.crypt(self.password, pw.pw_passwd) == pw.pw_passwd)
Line 32: Line 34:
        FieldsetForm.__init__(self, 'User login', (elements, ))         buttons = Buttons((
            Button('Login', 'login'),
        ))
FieldsetForm.__init__(self, 'User login', (elements, ), buttons=buttons)
Line 37: Line 42:
Create the form in page_enter 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:
Line 39: Line 44:
XXX need a login button {{{#!python
def page_enter(ctx):
    if not ctx.has_value('login'):
        ctx.locals.login = Login('', '')
        ctx.add_session_vars('login')
        ctx.locals.login_form = LoginForm(ctx.locals.login)
        ctx.add_session_vars('login_form')
}}}
Line 41: Line 53:
Render the form using alx-form In {{{login.html}}}, to display the form to the user we use:

{{{#!html
<al-form method="post">

  <alx-form name="login_form" errors />

  <al-expr expr="login_error" />

</al-form>
}}}

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.

{{{#!python
def page_process(ctx):
    if ctx.req_equals('login'):
        # nothing to validate
        ctx.locals.login_form.merge(ctx.locals.login)
        if ctx.locals.login.is_password_valid():
            ctx.redirect('search')
        else:
            ctx.locals.login_error = 'Login incorrect'
}}}

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 Login:
   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.getpwent(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.form import *
   2 
   3 class LoginForm(FieldsetForm):
   4     def __init__(self, login):
   5         elements = Fieldset((
   6             TextField('Username', 'username'),
   7             PasswordField('Password', 'password'),
   8         ))
   9         buttons = Buttons((
  10             Button('Login', 'login'),
  11         ))
  12         FieldsetForm.__init__(self, 'User login', (elements, ), buttons=buttons)
  13         
  14         self.load(login)

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('login'):
   3         ctx.locals.login = Login('', '')
   4         ctx.add_session_vars('login')
   5         ctx.locals.login_form = LoginForm(ctx.locals.login)
   6         ctx.add_session_vars('login_form')

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 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.login)
   5         if ctx.locals.login.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)