|Version 55 (modified by 8 years ago) ( diff ),|
How to add a new module?
The Eden Book has a tutorial for adding a new module. However, it is simplified from the normal way modules are included in Eden. Especially, the model file differs.
Add your module to settings.modules in
private/templates/<template>/config.py if it is for a specific template.
Add it to
private/templates/default/config.py to provide an example of how your module should be entered in settings.modules.
Create a file
Add an import statement to
import s3db.<MODULE NAME>
For each major "resource" in your module, add a class that is a subclass of S3Model. The class must define a model() method that defines the tables for that resource. The class can define multiple tables, for other resources associated with the major resource. For instance, if you have a major resource representing a vehicle, you might want a table to represent types of vehicles, so users can add new types without changing the code. The main table for specific vehicles and the table for types can go in the same model class.
To avoid namespace clashes, use your module name as a prefix for table names. Follow with underscore and the name of the resource your table represents: <MODULE NAME>_<RESOURCE>. Resource names should be unique.
E.g. if building a Vehicle Management System with module name vms, with individual vehicles and vehicle types:
__all__ = ["S3VehicleModel", "vms_vehicle", "vms_vehicle_type", ] class S3VehicleModel(S3Model): """ Vehicle Management """ names = ["vms_vehicle", "vms_vehicle_type", ] def model(self): T = current.T db = current.db tablename = "vms_vehicle_type" represent = S3Represent(lookup=tablename) table = s3db.define_table(tablename, Field("name", requires = IS_NOT_EMPTY() ), *s3_meta_fields()) tablename = "vms_vehicle" represent = S3Represent(lookup=tablename) table = s3db.define_table(tablename, Field("name", requires = IS_NOT_EMPTY() ), Field("type", requires=IS_ONE_OF(db, "vms_vehicle_type.id", represent), ) *s3_meta_fields())
Create a file:
Add CRUD functions for your tables:
module = request.controller def vehicle(): "RESTful CRUD controller" resource = request.function return s3_rest_controller(module, resource)
Additional options are described here: s3_rest_controller
Add the messages for your resources:
def vehicle(): "RESTful CRUD controller" resource = request.function s3.crud_strings[tablename] = Storage( title_create = T("Add Vehicle"), title_display = T("Vehicle Details"), title_list = T("List Vehicles"), title_update = T("Edit Vehicle"), title_search = T("Search Vehicles"), subtitle_create = T("Add New Vehicle"), subtitle_list = T("Vehicles"), label_list_button = T("List Vehicles"), label_create_button = T("Add Vehicle"), label_delete_button = T("Delete Vehicle"), 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")) return s3_rest_controller(module, resource)
Add a menu:
# Options Menu (available in all Functions' Views) response.menu_options = [ [T("Home"), False, URL(f="index")], [T("Add Vehicle"), False, URL(f="vehicle", args="create")], [T("List Vehicles"), False, URL(f="vehicle")], [T("Search Vehicles"), False, URL(f="vehicle", args="search")] ]
Add HTML templates for any custom functions:
/views/<MODULE NAME>/<FUNCTION NAME>.html
index.html is required to start with since the RESTful controller normally re-uses standard views.
If Custom Views are wanted they are detected automatically if files are found in