When using Python2.7 in App Engine, is it possible to refer to global global queries?

I am using the Python2.7 runtime with setting threadafe to false in the manifest.

Is it safe to do

user = users.get_current_user()

once at the top of the script, in the global space and refer to it from different handlers without any problems with the namespace?

+2
source share
1 answer

It is better to create a base class, add some functions there, and then extend all your handlers from the base class, because it get_current_user()is related to the request handler and only makes sense there.

Here is an example:

import webapp2
from google.appengine.api import users

class BaseHandler(webapp2.RequestHandler):
  def get_user(self):
    #Maybe also adding some logic here or returning your own User model
    return users.get_current_user()


class MainPage(BaseHandler):
  def get(self):
    if self.get_user():
      self.response.headers['Content-Type'] = 'text/plain'
      self.response.out.write('Hello, ' + self.get_user().nickname())
    else:
      self.redirect(users.create_login_url(self.request.uri))
+4
source

All Articles