Wiki

Clone wiki

rum / MoreThanCrud

More than CRUD

This describes some techniques demonstrated in TgRumDemo.

CRUD is also mini WSGI framework, which might provide elegant solutions:

First make sure, that you load a real (non-dummy) policy and provide a template path.

TG Users can do that using (see rumdemo)

admin = RumAlchemyController(
    model,
    template_path=config['paths']['templates'][0],
    render_flash=False, # Since this app's master template will render it
    policy=MyPolicy
    )

import the model and some rum stuff

from tgrumdemo.model import User
from rum.controller import resource_action, CRUDController, ControllerFactory

Then subclass the CRUDController and register it for User objects.

class UserController(CRUDController):
    @resource_action('member', 'GET')
    def hello(self):
        return {'item': self.obj}

ControllerFactory.register(UserController, User)

By default the view is searched in some template with the same name. So we put a Genshi template named hello.html in the template path.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
                      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:py="http://genshi.edgewall.org/"
      xmlns:xi="http://www.w3.org/2001/XInclude">
<head>
  <meta content="text/html; charset=UTF-8" http-equiv="content-type" py:replace="''"/>
  <title>Hello</title>
</head>
<body>
    Hello ${item}!
</body>
</html>

Since for a non-dummy policy, we have explicitely to allow access to resources, we register a rule for our Policy.

#only same user can say hello to himself
MyPolicy.register(is_same_user, obj=User, action="hello")

The function is_same_user is reused from tgrumdemo.policy

Voila, we have a controller, that automatically fetches instances, gives 404, when that instance does not exists, checks permissions, using our declarative policy and renders it into the template.

Since our app is mounted under /admin in TgRumDemo is accessible under /admin/users/<user_id>/hello

Updated