Re: parameterized controllers
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 -~--~~~~--~~--~--~---
Re: parameterized controllers
I did something similar using the __before__ method class CustomerController(BaseController): def __before__(self): if 'cust_id' in request.environ['pylons.routes_dict']: cust_id = request.environ['pylons.routes_dict'] ['cust_id'] c.customer=meta.Session.query(Customer).get(cust_id) else: c.customer = None def DoSomething(self, cust_id, id): . def DoMore(self, cust_id, id): . The routes_dict looks like this: {'action': u'dostuff', 'controller': u'customert', 'id': u'123'} Hopefully that works. Cheers Aaron --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
Re: parameterized controllers
Previously Aaron R wrote: I did something similar using the __before__ method class CustomerController(BaseController): def __before__(self): if 'cust_id' in request.environ['pylons.routes_dict']: cust_id = request.environ['pylons.routes_dict'] ['cust_id'] c.customer=meta.Session.query(Customer).get(cust_id) else: c.customer = None def DoSomething(self, cust_id, id): . def DoMore(self, cust_id, id): . The routes_dict looks like this: {'action': u'dostuff', 'controller': u'customert', 'id': u'123'} Hopefully that works. It works and is indeed better. I still think my approach is nicer: it does not require poking manually at the routes memory but provides a clean interface. 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 -~--~~~~--~~--~--~---
Re: parameterized controllers
for a route like /company/:company_id/child_resource/id i do something like: class ChildResourceController(BaseController): def _get_method_args(self): args = BaseController._get_method_args(self) args['campaign'] = model.Company.get(args['campaign_id']) return args def myaction(self, company, id): ... --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
Re: parameterized controllers
On Mon, May 19, 2008 at 12:35 AM, Wichert Akkerman [EMAIL PROTECTED] wrote: Previously Aaron R wrote: I did something similar using the __before__ method class CustomerController(BaseController): def __before__(self): if 'cust_id' in request.environ['pylons.routes_dict']: cust_id = request.environ['pylons.routes_dict'] ['cust_id'] c.customer=meta.Session.query(Customer).get(cust_id) else: c.customer = None def DoSomething(self, cust_id, id): . def DoMore(self, cust_id, id): . The routes_dict looks like this: {'action': u'dostuff', 'controller': u'customert', 'id': u'123'} Hopefully that works. It works and is indeed better. I still think my approach is nicer: it does not require poking manually at the routes memory but provides a clean interface. def __before__(self, cust_id): You can also use c.cust_id because all routing variables are shadowed onto 'c'. -- Mike Orr [EMAIL PROTECTED] --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
Re: parameterized controllers
i'm not sure if this is the same... i often do stuff like this: class coreObjectController: def do_stuff(): Session.query( self.model_object ).blach def do_more(): pass class aCrontroller( coreObjectController ): self. model_object = class_a class bCrontroller( coreObjectController ): self. model_object = class_b --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
Re: parameterized controllers
Previously Jonathan Vanasco wrote: i'm not sure if this is the same... i often do stuff like this: class coreObjectController: def do_stuff(): Session.query( self.model_object ).blach def do_more(): pass class aCrontroller( coreObjectController ): self. model_object = class_a class bCrontroller( coreObjectController ): self. model_object = class_b I don't see what the link between this and my suggestion I'm afraid. 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 -~--~~~~--~~--~--~---