wiki:DeveloperGuidelinesNewModule

Version 13 (modified by Fran Boon, 16 years ago) ( diff )

Vehicles example including main resource table

DeveloperGuidelines

How to add a new module?

Copy an existing module, paste & edit. Good modules to look at to start with are or & cr as these are simplest & standard.

Model

Add module to db.module:

This makes it visible on the front page & the left-hand navigation menu

Create a file /models/module.py

This needs a table to store the module's menu options in:

module='name'

# Menu Options
db.define_table('%s_menu_option' % module,
                SQLField('name'),
                SQLField('function'),
                SQLField('description',length=256),
                SQLField('priority','integer'),
                SQLField('enabled','boolean',default='True'))
db['%s_menu_option' % module].name.requires=[IS_NOT_EMPTY(),IS_NOT_IN_DB(db,'%s_menu_option.name' % module)]
db['%s_menu_option' % module].name.requires=IS_NOT_EMPTY()
db['%s_menu_option' % module].priority.requires=[IS_NOT_EMPTY(),IS_NOT_IN_DB(db,'%s_menu_option.priority' % module)]

Add additional tables to this file, as-required for your resources.
To avoid namespace clashes, use the format: db.module_table

e.g. if beilding a Vehicle Management System, create db.veh_vehicle:

db.define_table('veh_vehicle',
                SQLField('modified_on','datetime',default=now),
                SQLField('uuid',length=64,default=uuid.uuid4()),
                SQLField('name'))
db.veh_vehicle.name.requires=IS_NOT_EMPTY()
db.veh_vehicle.name.comment=SPAN("*",_class="req")

Also add the messages for your resources:

crud_strings_vehicle=Storage(title_create=T('Add Vehicle'),
            title_display=T('Vehicle Details'),
            title_list=T('List Vehicles'),
            title_update=T('Edit Vehicle'),
            subtitle_list=T('Vehicles'),
            subtitle_create=T('Add New Vehicle'),
            label_list_button=T('List Vehicles'),
            label_create_button=T('Add Shelter'),
            msg_record_created=T('Vehicle added'),
            msg_record_modified=T('Vehicle updated'),
            msg_record_deleted=T('Vehicle deleted'),
            msg_list_empty=T('No Vehicles currently registered'))

Copy/paste & do just a few small tweaks once pasted:

  • Search/Replace 'Shelter' with Your_resource
  • Fix plurals, if required (e.g. Persons -> People, Classs -> Classes, Metadatas -> Metadata)
  • Maybe change phrasing: registered vs defined, etc

Controller

Add CRUD functions for these tables to /controllers/module.py:

def vehicle():
    "RESTful CRUD controller"
    return shn_rest_controller(module,'vehicle')

Manual method for if/when you need more control: DeveloperGuidelinesCreateReadUpdateDelete

Populate the module's menu_options table with the functions that you wish to expose to the module's front page & left-hand navigation bar.

Views

Add HTML templates for any custom functions: /views/module/function.html

NB Only index.html is required to start with since the RESTful controller re-uses standard views

DeveloperGuidelines

Note: See TracWiki for help on using the wiki.