can we return variable from the python decoretor?

54 views Asked by At

I have used function for checking session key in django (it need to code).

def sessioncheck(unikey):
   from django.contrib.sessions.models import Session
   try:
       session_obj = Session.objects.get(pk=unikey)
       userinfo = session_obj.get_decoded()
       # Here i want some data to return 
       userkey = userinfo["session_datakey"] 
       username = userinfo["session_user"] 
       return userkey,username # i need it in my function and as many other stuff to with it
   except(ObjectDoesNotExist, KeyError):
       response = JsonResponse({"expire": "u r not here anymore"})
       return response

Here I like to use it as decorator? but not getting it how can be done i.e return part for the userkey
The above function i like to use as decorector.

# Currunt i m using as
def sessioncall(request, unikey):
      gotukey,username = sessioncheck(unikey)
      if type(token).__name__ is not "JsonResponse":
           # Lots of work using **gotukey** and **username** over here 
           # work change from function to function for use gotukey
           response = JsonResponse({"hmm": "man you are in now"})
           return response
      else:
           return gotukey

I know we can use decorator to raise error but i also want to return some values if it success.
my question is can we write decorator? or we need to use at function call only for return

0

There are 0 answers