Question

What i am trying to achieve is getting the user object from the webapp2.cached property. i have a method defined in my request handler which gives me the current user.

@webapp2.cached_property
  def current_user(self):
  user_dict = self.auth.get_user_by_session()
  return self.auth.store.user_model.get_by_id(user_dict['user_id'])

I want to get the User Object because it is a UserProperty() of one of my models which is as follows:

class CountryImage(db.Model):
  image = db.BlobProperty()
  user = db.UserProperty()
  country = db.ReferenceProperty(Country)
  approved = db.BooleanProperty(default=False)

Now when the upload form is posted, everything works fine except for the "user" which shows as "None" in the datastore.

The possible reason i found while i was playing with it in the interactive console was that the method current_user passes a web cached property and not the actual user object.

For Example:

< webapp2.cached_property object at 0xb46aee8c > 

Now my question is what would be the best possible way of retrieving the user object ? Thanks in advance for all your help.

Amyth

PS: EDIT:

following is the code where i am trying to store the image in the datastore:

NewImg = models.CountryImage()
NewImg.image = self.request.get('image')
NewImg.user = self.current_user
NewImg.country = models.Country.all().filter("url_name =", country).get()
continue_url = self.request.url
NewImg.put()

PS:

Just to add i have also tried using "users.get_current_user()" method which also returns "None"

Was it helpful?

Solution

Thanks for your responses, however i have soved this already. In case you are using User model from webapp2_extras (which is an extension to User model from appengine.api.users) you can get the user as follows

from google.appengine.api import users
from webapp2_extras.appengine.auth.models import User

user = users.User(User.get_by_auth_id(self.current_user.auth_ids[0]).name)

or

user = users.User(User.get_by_auth_id(self.current_user.auth_ids[0]).email)

OTHER TIPS

i'm curious, the country image, is that uploaded by users? or are you managing those as resources?

if not, you might be better to use a StringProperty on the user model, then reference a string path to a static image asset and save yourself the extra db lookups

users.get_current_user() should be the way to go, but that requires the user to login first. Are you trying to support anonymous users?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top