Providing data to a function using a decorator
Johan Hartzenberg <[email protected]> Sat, 5 Nov 2016 13:23:24 -0700 (PDT)
| Newsgroups | gmane.comp.python.cherrypy |
|---|---|
| Message-ID | <[email protected]> |
Many of my route handlers go through a sequence of similar steps, eg
def handler_x(self, **kwargs):
#Check Authentication
...
#Validate Input
...
#Check Authorization
....
#Perform Database Query/Update
...
#Format result into response
return response
It would seem that this sequence lends itself to being put into decorators.
I have to some extent put these into functions already, but I still end up
with a lot of code like
def handler_x(self, **kwargs):
...
try:
dbcursor = ....
except ..
Etc etc for every step of the handler
The format of each of these "decorators" appears to be
try to get some value (a user-name, a db connection, etc)
Check whether it was sucesful
Return a response if not
Use the response in the next step
So basically imagine
@dict_to_json
@check_authentication (this sets username)
@check_valid_input
@get a database connection (this uses username, and sets a cursor)
@check authorization (this uses username and cursor)
def handler_x(self, **kwargs):
use username and db_cursor here
return a result
So the question is how do I get the results from one decorator to be
accessible to the next.
My first thought would be to build on cherrypy.request ... set the results
as properties on there, which can then be accessed by subsequent
functions/decorators... But maybe there is another way that I am not aware
of...
--
You received this message because you are subscribed to the Google Groups "cherrypy-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cherrypy-users+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/[email protected]
To post to this group, send email to cherrypy-users-/JYPxA39Uh5TLH3MbocFF+G/[email protected]
Visit this group at https://groups.google.com/group/cherrypy-users.
For more options, visit https://groups.google.com/d/optout.