On Mar 5, 9:48 pm, Abhishek Mishra <ideam...@gmail.com> wrote: > Hi I'm trying to implement a decorator for custom error pages in > web2py into sahanapy. > Ref -http://web2py.com/AlterEgo/default/show/75 > > I'm trying to keep it as a module in /modules directory so that I can > import it into the controllers and place the decorator appropriately. > > I have kept error handling decorator as > /modules/onerror.py > > and am importing it like this - > exec('from applications.%s.modules.onerror import onerror as onerror' > % > request.application)
you can also use: onerror = load_import('onerror').onerror > HTTP object wasn't available in onerror.py so i did a - > from gluon.http import * python modules are normal python modules also in web2py. They only see python keywords unless you import them. web2py has 5 special objects (request, response, session, cache, T) and your app has others (db, auth, crud). Because they are instantiated at every request, they cannot import by the module, they have to be passed explicitly to the functions in the module. Mind that while this is awkward this is what other frameworks ask to do all the time for all functions. web2py saves you this for models and controllers. > But then I readlized that the request object is also not available to > the decorator as in the line - > filename=os.path.join(request.folder,'views/errors/error > %i.html'%status) > > I have some doubts - > > 1. Are the modules in the /modules directory automatically loaded as a > new web2py instance is created? If so how can I access them? No. They are norml python modules. You have to import them to use them. > 2. Is there a way I can pass the request object to this decorator from > my controller? Hints are welcome. You need a meta decorator def meta_decoration(something): def actual_decorator(f,something=something): do_whatever_you_want() return f() return decorator @meta_decorator(request) def function_to_be_decorated(): return dict() Anyway you do not need this decorator. Look into routes.examples.py for routes_onerror. There is a simple way to do what you want. > My current onerror.py -http://paste.pocoo.org/show/186165/ > The way I'm trying to use in a controller -http://paste.pocoo.org/show/186167/ > > Let me know if I'm doing it in an unobvious way. > > Thanks, > Abhishek -- You received this message because you are subscribed to the Google Groups "web2py-users" group. To post to this group, send email to web...@googlegroups.com. To unsubscribe from this group, send email to web2py+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/web2py?hl=en.