views:

91

answers:

3

Is there a way so that I can get access to request object, while saving a db object, without explicitly passing it e.g.

class RequestData(db.Model):
    ...
    def put(self):
        # autopopulate fields from current request

I just want a quick way to access request, instead of passing it thru so many layers of view/forms/etc

A: 

Hi Anurag,

There isn't any pre-built way of achieving this to the best of my knowledge, however writing such a piece of code should not be very hard.

If its GAE, the self.request.arguments() method can be used to read the properties of the request object and thus should enable iteratively accessing values of all the request properties.

Srirangan
You would also want to build automated validations in this request-to-model interface.
Srirangan
i do not understand what you mean, can you give an example of how I can get request object in a db Model?
Anurag Uniyal
A: 

views.py:

import threading
request_storage = threading.local()
def myview(request):
    request_storage.data = request

models.py:

class MyClass():
    def save():
        from views import request_storage
        request_storage.data.GET['myarg']

this will use request_storage instance from current thread

Antony Hatchkins
is threading module available in GAE?
Anurag Uniyal
hmm. I didn't notice the tag. Was it there? No it isn't available there. In GAE I'd stick to Iamamac's version.
Antony Hatchkins
+2  A: 

Since Google AppEngine uses CGI protocol, the request information is all there in the environment variables (See CGI Environment Variables).

You can regenerate the request object just like this:

req = google.appengine.ext.webapp.Request(dict(os.environ))
Iamamac
This appears to answer the question, what gives?
bentford
yes it is, I can get all the cookies which I wanted, thanks :)
Anurag Uniyal
Thank you! This helped me as well
Nick Gotch