To understand the Display
class, let us create a very simple application. Type in the following code and run it:
Chapter8/crmcontact.py
import os import cherrypy from entity import AbstractEntity, Attribute, Picklist from browse import Browse from display import Display from logondb import LogonDB db="/tmp/crmcontact.db" class Entity(AbstractEntity): database = db class Contact(Entity): firstname = Attribute(displayname="First Name") lastname = Attribute(displayname="Last Name", notnull=True, primary=True) gender = Attribute(displayname="Gender", notnull=True, validate=Picklist( Male=1,Female=2,Unknown=0)) telephone = Attribute(displayname="Telephone") class ContactBrowser(Browse): edit = Display(Contact, edit=True) add = Display(Contact, add=True) current_dir = os.path.dirname(os.path.abspath(__file__)) cherrypy.quickstart(ContactBrowser(Contact),config={ '/': { 'log.access_file' : os.path.join(current_dir,"access.log"), 'log.screen': False, 'tools.sessions.on': True } })
When you point your browser to http://localhost:8080
, you will be presented with an empty list of contacts that you may expand by clicking the Add button. This will present you with the following screen:
Here you may enter new values, and when you click the add button, a new contact will be added to the database, after which, you will return to the list of contacts, but now with an extra one added.
In the application tree that we constructed, we mounted several instances of the Display
class, each with its own initialization parameters. These parameters are merely stored in the instance by the __init__()
method for referral later:
Chapter8/display.py
def __init__(self, entity, edit=False, add=False, logon=None, columns=None): self.entity = entity self.edit = edit self.add = add self.logon = logon if columns is None: self.columns = entity.columns else: self.columns = columns
The most important parameter is entity
. This will be the Entity
class that we want Display
to be able to add or edit.
__init__()
also takes an edit
or add
parameter that when set will determine the type of activity this instance of Display
will perform. If neither is given, an instance will just be displayed without the possibility of altering its attributes. In the stripped down crmcontact.py
application, we created a ContactBrowser
class that holds references to two different instances of the Display
class. The one in the add
class variable is created with an add
attribute set to True
, while the one in the edit
variable is created with an edit
attribute set to True
. The Add new button in the browser is equipped with a click handler that will replace the browse list with the form that will be served by the Display
instance that was created with the add
argument.
3.136.17.12