Hi Wichert,

What I use to avoid such repetitions is:

# in BaseController

# sequence of (param, attr, getter)
_fetch = []

def __before__(self):
    for param, attr, getter in self._fetch:
        setattr(c, attr, getter(getattr(c, param)))

# in CustomerController
_getters = (
    ('id', 'customer', model.Customer.get),
    ('invoice', 'invoice', model.Invoice.get),
)

def action(self):
    # use c.customer and c.invoice
    if c.customer is None:
        abort(code=404)

Cheers

On May 18, 9:51 am, Wichert Akkerman <[EMAIL PROTECTED]> wrote:
> I seem to regularly run into a model where I would like to change how
> controller instantiation happens. A quite common patern I see is that
> you want to do something with a subitem of a model, for example manage
> the tasks for a particular project. With standard pylons you can easily
> setup a route for a URL like /customer/<customer-id>/task/<task-id> and
> map that to CustomerController.task(self, customer_id, task_id), where
> you lookup the Customer model and then the right task for that customer.
> This is then quickly duplicated when you add contracts, invoices, etc.
> There is repetition there: every action method will start with code
> to find the correct customer, like this:
>
>     class CustomerController(BaseController):
>         def DoSomething(self, cust_id, id):
>             customer=meta.Session.query(Customer).get(cust_id)
>             ...
>
>         def DoMore(self, cust_id, id):
>             customer=meta.Session.query(Customer).get(cust_id)
>             ...
>
> I wonder if it would be better if you could specify controller construction
> parameters in the route. The route format could easily be extended so
> support controller-parameters, for example by using @ instead of :. In
> my example I could have a route like this: /:controller/@objid/:action/:id
> which would tell pylons to instantiate a controller with the objid parameter
> (CustomerController(objid=123)) on which the action is called. Your code
> would then look like this:
>
>     class CustomerController(BaseController):
>         def __init__(self, cust_id=None):
>             if cust_id is not None:
>                customer=meta.Session.query(Customer).get(cust_id)
>
>         def DoSomething(self, id):
>            ....
>
>         def DoMore(self, id):
>             ....
>
> this reduces repitition and makes it possible to do (more) common setup
> work in the controller constructor.
>
> Wichert.
>
> --
> Wichert Akkerman <[EMAIL PROTECTED]>    It is simple to make 
> things.http://www.wiggy.net/                  It is hard to make things 
> simple.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" group.
To post to this group, send email to pylons-discuss@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/pylons-discuss?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to