wiki:DeveloperGuidelinesNewModule

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

New way of defining crud strings

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:

module='veh'
resource='vehicle'
table=module+'_'+resource
single=resource.capitalize()
# NB May need manual fixing!
plural=single+'s'
db.define_table(table,
                SQLField('modified_on','datetime',default=now),
                SQLField('uuid',length=64,default=uuid.uuid4()),
                SQLField('name'))
db['%s' % table].represent=lambda table:shn_list_item(table,resource='vehicle',action='display')
db.veh_vehicle.name.requires=IS_NOT_EMPTY()
db.veh_vehicle.name.comment=SPAN("*",_class="req")

Also add the messages for your resources:

title_create=T('Add %s' % single)
title_display=T('%s Details' % single)
title_list=T('List %s' % plural)
title_update=T('Edit %s' % single)
subtitle_create=T('Add New %s' % single)
subtitle_list=T('%s' % plural)
label_list_button=T('List %s' % plural)
label_create_button=T('Add %s' % single)
msg_record_created=T('%s added' % single)
msg_record_modified=T('%s updated' % single)
msg_record_deleted=T('%s deleted' % single)
msg_list_empty=T('No %s currently registered' % plural)
exec('crud_strings.%s=Storage(title_create=title_create, title_display=title_display, title_list=title_list, title_update=title_update, subtitle_create=subtitle_create, subtitle_list=subtitle_list, label_list_button=label_list_button, label_create_button=label_create_button, msg_record_created=msg_record_created, msg_record_modified=msg_record_modified, msg_record_deleted=msg_record_deleted, msg_list_empty=msg_list_empty)' % resource)

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

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