websauna.system.crud package

An abstract CRUD implementation based on traversal. The default support for SQLAlchemy and Deform.

class websauna.system.crud.CRUD(request)[source]

Bases: websauna.system.core.traversal.Resource

Define create-read-update-delete interface for an model.

We use Pyramid traversing to get automatic ACL permission support for operations. As long given CRUD resource parts define __acl__ attribute, permissions are respected automatically.

URLs are the following:

List: $base/listing

Add: $base/add

Show $base/$id/show

Edit: $base/$id/edit

Delete: $base/$id/delete

delete_object(obj)[source]

Delete one item in the CRUD.

Called by delete view if no alternative logic is implemented. :type obj: object :param obj: Raw object, e.g. SQLAlchemy instance.

fetch_object(id)[source]

Load object from the database for CRUD path for view/edit/delete.

Parameters:id (str) – Object id.
Return type:object
Returns:Object from database.
get_object_url(obj, view_name=None)[source]

Get URL for view for an object inside this CRUD.

Parameters:
  • obj (object) – Raw object, e.g. SQLAlchemy instance, which can be wrapped with wrap_to_resource.
  • view_name (Optional[str]) – Traverse view name for the resource. E.g. show, edit.
Return type:

str

Returns:

URL to the object.

make_resource(obj)[source]

Take raw model instance and wrap it to Resource for traversing.

Parameters:obj (object) – SQLAlchemy object or similar model object.
Return type:Resource
Returns:websauna.core.traverse.Resource
mapper = <websauna.system.crud.urlmapper.Base64UUIDMapper object>

Mapper defines how objects are mapped to URL space. The default mapper assumes models have attribute uuid which is base64 encoded to URL. You can change this to websauna.system.crud.urlmapper.IdMapper if you instead to want to use id as a running counter primary column in URLs. This is not recommended in security wise, though.

plural_name = 'items'

Helper noun used in the default placeholder texts

singular_name = 'item'

Helper noun used in the default placeholder texts

title = '(untitled CRUD)'
traverse_to_object(path)[source]

Wraps object to a traversable URL.

Loads raw database object with id and puts it inside Instance object, with __parent__ and __name__ pointers.

Parameters:path (str) – Path to be traversed to.
Return type:Resource
Returns:websauna.core.traverse.Resource
wrap_to_resource(obj)[source]

Wrap object to a traversable part.

Parameters:obj (object) – SQLAlchemy object or similar model object.
Return type:Resource
Returns:websauna.core.traverse.Resource
class websauna.system.crud.Resource(request, obj)[source]

Bases: websauna.system.core.traversal.Resource

One object in CRUD traversing.

Maps the raw database object under CRUD view/edit/delete control to traverse path.

Presents an underlying model instance mapped to an URL path. __parent__ attribute points to a CRUD instance.

get_model()[source]

Get the model class represented by this resource.

Return type:type
get_object()[source]

Return the wrapped database object.

Return type:Any
get_path()[source]

Extract the traverse path name from the database object.

get_title()[source]

Title used on view, edit, delete, pages.

By default use the capitalized URL path path.

Return type:str