#!/usr/bin/env python # -*- coding: iso-8859-1 -*- # # $Id$ # """ Common definitions for demo and interactive tests. You might define something similar for your own environment. I admin that this demo script is overly complex because I'm trying to showcase most of the basic features of Atocha. Your code could be much simpler. """ # stdlib imports import sys, os, StringIO, base64, shelve from os.path import * # Current renderer type for demo/tests: 'text' or 'htmlout'. rtype = 'htmlout' # atocha imports from atocha import * # htmlout imports if rtype == 'htmlout': try: from htmlout import * from atocha.renderers.rhtmlout import * rtype = 'htmlout' except ImportError: # We won't be able to test the htmlout renderers. print >> sys.stderr, ('Warning: htmlout not available, ' 'falling back on text renderers.') rtype = 'text' # Definition of test form. # form1 = Form( 'test-form', # Normal unicode string. StringField('name', N_("Person's Name")), # Ascii string. StringField('postal', N_("Postal Code"), encoding='ascii'), # Hidden field. StringField('secret', state=Field.HIDDEN, initial='zanzibar', encoding='latin-1'), # Password field. PasswordField('passwd', N_("Password"), size=12, maxlen=8), # A text area. TextAreaField('description', N_("Description"), rows=10, cols=60), # Simple date. DateField('birthday', N_("Birthday")), # A fancier date input widget. JSDateField('barmitz', N_("Bar Mitzvah")), # Email address and URL fields. EmailField('email', N_("Email")), URLField('homepage', N_("Home Page")), # Numerical fields. IntField('number', N_("Age")), FloatField('height', N_("Height"), format='%.7f'), # Boolean checkbox. BoolField('veggie', N_("Vegetarian?"),), # Radio buttons. RadioField('sex', [('m', N_('Male')), ('f', N_('Female')), ('x', N_('Maybe'))], N_('Sex'), orient=ORI_HORIZONTAL, initial='x'), # A single-choice menu. MenuField('marital', [(1, N_('Single')), (2, N_('Married')), (3, N_('Divorced')), (0, N_('(Other...)'))], N_('Marital Status'), initial=3), # A list of options. CheckboxesField('dances', [('salsa', N_('Salsa')), ('tango', N_('Tango')), ('rumba', N_('Rumba')), ('chacha', N_('Cha-cha-chá')), ('merengue', N_('Merengue')),], N_('Favourite Dances'), initial=['rumba', 'salsa']), # A list of options (single). ListboxField('beer', [('heineken', N_('Heineken')), ('kro', N_('Kronenbourg')), ('corona', N_('Corona')), ('budweiser', N_('Budweiser')), ('paulaner', N_('Paulaner')),], N_('The Best Beer (If Any)'), ), # A list of options (multiple). ListboxField('yogas', [('ash', N_('Ashtanga')), ('iyen', N_('Iyengar')), ('kri', N_('Kripalu')), ('bik', N_('Bikram')), ('kun', N_('Kundalini'))], N_('Yogas Practiced'), initial=['ash', 'kun'], multiple=1 ), # A file that can be uploaded. FileUploadField('donation', N_('Donation (Send File)')), # A file that can be sent or reset. SetFileField('photo', N_('Photograph')), ## # Agree checkbox. ## AgreeField('terms', N_("Agree to Terms"),), # Disabled field. StringField('veteran', N_("Veteran"), initial=u'Disabled...', state=Field.DISABLED), # Read-Only field. StringField('notouch', N_("Touch Me"), initial=u"Don't touch", state=Field.READONLY), action='handle', reset=1) # HTML page templates for our test scripts template_pre = """ %(scripts)s
'
for name, value in values.iteritems():
print >> s, '%s: %s' % (name, repr(value))
print >> s, ''
s.write(template_post)
return s.getvalue()
def handler_reset():
"""
Handler that resets the form data stored in the local DB.
"""
db = getdb()
# Set form data for edit.
for n in 'data', 'photo', 'photofn', 'session':
try:
del db['%s-%s' % (n, form1.name)]
except Exception:
pass
db.close()
return handler_display()
#===============================================================================
# TEST FORM OVERRIDE
#===============================================================================
## form1 = Form(
## 'test-form',
## # Normal unicode string.
## StringField('name', N_("Person's name")),
## # Ascii string.
## StringField('postal', N_("Postal code"), encoding='ascii'),
## action='handle', reset=1)