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:
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.