Stuck retrieving parameters in sessions - GAE webapp2 - python

I'm trying to get sessions to work with Webapp2 with GAE, but I'm stuck retrieving the stored parameters. Maybe I'm overlooking the obvious.
A dressed down version of with main.py where I store a value in the webapp2 session:
import webapp2
from webapp2_extras import sessions
other imports...
class BaseHandler(webapp2.RequestHandler): # Copied from Google's doc
def dispatch(self):
# Get a session store for this request.
self.session_store = sessions.get_store(request=self.request)
try:
# Dispatch the request.
webapp2.RequestHandler.dispatch(self)
finally:
# Save all sessions.
self.session_store.save_sessions(self.response)
#webapp2.cached_property
def session(self):
# Returns a session using the default cookie key.
return self.session_store.get_session()
class MainPage(webapp2.RequestHandler): # My main page proper
def get(self):
self.session['foo'] = 'bar' # Store somehing in the session
template_values = {
}
template = JINJA_ENVIRONMENT.get_template('index.html')
self.response.write(template.render(template_values))
application = webapp2.WSGIApplication([
('/', MainPage, ),], debug = True)
My problem is how to access the value stored in the session from another module. In test.py I do the following:
from webapp2_extras import sessions
import main
other imports ...
class Test(main.BaseHandler):
def post(self):
foo1 = self.session.get('foo')
return foo1
class ProductsPage(webapp2.RequestHandler):
def get(self):
foo2 = Test.post()
...
But I get the following error:
TypeError: unbound method post() must be called with Test instance as first argument (got nothing instead)
I just can't find the proper way to use the Test class and retrieve the value stored in the session. I must be overlooking something when using the class, but I'm stuck and can't find the way out.
Maybe somebody looking from outside will spot it easily?

Looking further I found a post that helped me
GAE webapp2 session handling not working when putting basehandler to a different file
After some tinkering here's the recipe that worked for me to setup a session in webapp2 and share the session among different .py files. Hope it saves others some time.
1) Create basehandler.py, this will need to be imported by all .py files using the session. This is straight from Google's documentation:
import webapp2
from webapp2_extras import sessions
class BaseHandler(webapp2.RequestHandler):
def dispatch(self):
# Get a session store for this request
self.session_store = sessions.get_store(request = self.request)
try:
webapp2.RequestHandler.dispatch(self)
finally:
self.session_store.save_sessions(self.response)
#webapp2.cached_property
def session(self):
# Returns a session using the default cookie key
return self.session_store.get_session()
2) Create main.py importing BaseHandler. In my case I store a value in the session:
import os
import urllib
import webapp2
import jinja2
from basehandler import BaseHandler
JINJA_ENVIRONMENT = jinja2.Environment(
loader=jinja2.FileSystemLoader(os.path.dirname(__file__)),
extensions=['jinja2.ext.autoescape'])
class MainPage(BaseHandler):
def get(self):
self.session['foo'] = 'bar' # Store a value in the session
# you can store more than one :-)
# Define values that will be passed onto the template
template_values = {
'title': 'TITLE'
}
# search for an appropriate template and render it.
template = JINJA_ENVIRONMENT.get_template('./templates/index.html')
self.response.write(template.render(template_values))
config = {}
config['webapp2_extras.sessions'] = {
'secret_key': 'my-super-secret-key'
}
application = webapp2.WSGIApplication([
('/', MainPage)
], config = config, debug = True)
3) Create another.py taking care to also import BaseHandler and use the same secret key. Here you can retrieve the value stored by main.py or any other .py file sharing the same session:
import os
import urllib
import webapp2
import jinja2
from basehandler import BaseHandler
JINJA_ENVIRONMENT = jinja2.Environment(
loader=jinja2.FileSystemLoader(os.path.dirname(__file__)),
extensions=['jinja2.ext.autoescape'])
class AnotherPage(BaseHandler):
def get(self):
foo1 = self.session.get('foo')
# Now go and do something with foo1, even store a new value as in main.py
# ...
# ...
# Define values that will be passed onto the template
template_values = {
'title': 'TITLE'
}
# search for an appropriate template and render it.
template = JINJA_ENVIRONMENT.get_template('./templates/additional.html')
self.response.write(template.render(template_values))
config = {}
config['webapp2_extras.sessions'] = {
'secret_key': 'my-super-secret-key'
}
application = webapp2.WSGIApplication([
('/', AdditionalPage)
], config = config, debug = True)

Related

How works logging in Flask [duplicate]

I am trying to access access application configuration inside a blueprint authorisation.py which in a package api. I am initializing the blueprint in __init__.py which is used in authorisation.py.
__init__.py
from flask import Blueprint
api_blueprint = Blueprint("xxx.api", __name__, None)
from api import authorisation
authorisation.py
from flask import request, jsonify, current_app
from ..oauth_adapter import OauthAdapter
from api import api_blueprint as api
client_id = current_app.config.get('CLIENT_ID')
client_secret = current_app.config.get('CLIENT_SECRET')
scope = current_app.config.get('SCOPE')
callback = current_app.config.get('CALLBACK')
auth = OauthAdapter(client_id, client_secret, scope, callback)
#api.route('/authorisation_url')
def authorisation_url():
url = auth.get_authorisation_url()
return str(url)
I am getting RuntimeError: working outside of application context
I understand why that is but then what is the correct way of accessing those configuration settings?
----Update----
Temporarily, I have done this.
#api.route('/authorisation_url')
def authorisation_url():
client_id, client_secret, scope, callback = config_helper.get_config()
auth = OauthAdapter(client_id, client_secret, scope, callback)
url = auth.get_authorisation_url()
return str(url)
Use flask.current_app in place of app in the blueprint view.
from flask import current_app
#api.route("/info")
def get_account_num():
num = current_app.config["INFO"]
The current_app proxy is only available in the context of a request.
Overloading record method seems to be quite easy:
api_blueprint = Blueprint('xxx.api', __name__, None)
api_blueprint.config = {}
#api_blueprint.record
def record_params(setup_state):
app = setup_state.app
api_blueprint.config = dict([(key,value) for (key,value) in app.config.iteritems()])
To build on tbicr's answer, here's an example overriding the register method example:
from flask import Blueprint
auth = None
class RegisteringExampleBlueprint(Blueprint):
def register(self, app, options, first_registration=False):
global auth
config = app.config
client_id = config.get('CLIENT_ID')
client_secret = config.get('CLIENT_SECRET')
scope = config.get('SCOPE')
callback = config.get('CALLBACK')
auth = OauthAdapter(client_id, client_secret, scope, callback)
super(RegisteringExampleBlueprint,
self).register(app, options, first_registration)
the_blueprint = RegisteringExampleBlueprint('example', __name__)
And an example using the record decorator:
from flask import Blueprint
from api import api_blueprint as api
auth = None
# Note there's also a record_once decorator
#api.record
def record_auth(setup_state):
global auth
config = setup_state.app.config
client_id = config.get('CLIENT_ID')
client_secret = config.get('CLIENT_SECRET')
scope = config.get('SCOPE')
callback = config.get('CALLBACK')
auth = OauthAdapter(client_id, client_secret, scope, callback)
Blueprints have register method which called when you register blueprint. So you can override this method or use record decorator to describe logic which depends from app.
The current_app approach is fine but you must have some request context. If you don't have one (some pre-work like testing, e.g.) you'd better place
with app.test_request_context('/'):
before this current_app call.
You will have RuntimeError: working outside of application context , instead.
You either need to import the main app variable (or whatever you have called it) that is returned by Flask():
from someplace import app
app.config.get('CLIENT_ID')
Or do that from within a request:
#api.route('/authorisation_url')
def authorisation_url():
client_id = current_app.config.get('CLIENT_ID')
url = auth.get_authorisation_url()
return str(url)
You could also wrap the blueprint in a function and pass the app as an argument:
Blueprint:
def get_blueprint(app):
bp = Blueprint()
return bp
Main:
from . import my_blueprint
app.register_blueprint(my_blueprint.get_blueprint(app))
I know this is an old thread. But while writing a flask service, I used a method like this to do it. It's longer than the solutions above but it gives you the possibility to use customized class yourself. And frankly, I like to write services like this.
Step 1:
I added a struct in a different module file where we can make the class structs singleton. And I got this class structure from this thread already discussed. Creating a singleton in Python
class Singleton(type):
_instances = {}
def __call__(cls, *args, **kwargs):
if cls not in cls._instances:
cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
else:
cls._instances[cls].__init__(*args, **kwargs)
return cls._instances[cls]
Step 2:
Then I created a Singleton EnvironmentService class from our Singleton class that we defined above, just for our purpose. Instead of recreating such classes, create them once and use them in other modules, routes, etc. import. We can access the class with the same reference.
from flask import Config
from src.core.metaclass.Singleton import Singleton
class EnvironmentService(metaclass=Singleton):
__env: Config = None
def initialize(self, env):
self.__env = env
return EnvironmentService()
def get_all(self):
return self.__env.copy()
def get_one(self, key):
return self.__env.get(key)
Step 3:
Now we include the service in the application in our project root directory. This process should be applied before the routes.
from flask import Flask
from src.services.EnvironmentService import EnvironmentService
app = Flask(__name__)
# Here is our service
env = EnvironmentService().initialize(app.config)
# Your routes...
Usage:
Yes, we can now access our service from other routes.
from src.services.EnvironmentService import EnvironmentService
key = EnvironmentService().get_one("YOUR_KEY")

Need to access self.request.GET from main.py in lib.py

I need to do calculations among other things within lib.py and I need to access the user input data within main.py for it, because it needs to be that way. How do I do this?
doing from main import MainHandler in lib.py and then calling it "works" in the sense that it doesn't give any code errors, but displays a blank page when done that way and I get a log error saying that I cannot import it.
main.py
import webapp2
from lib import FormData
from pages import FormPage
from pages import ResultsPage
class MainHandler(webapp2.RequestHandler):
def get(self):
f = FormPage()
s = ResultsPage()
fd1 = FormData()
if self.request.GET:
fd1.name = self.request.GET['name']
fd1.email = self.request.GET['email']
fd1.weight = self.request.GET['weight']
fd1.height = self.request.GET['height']
self.response.write(s.second_page(fd1.name, fd1.email, fd1.weight, fd1.height))
else:
self.response.write(f.form_page)
app = webapp2.WSGIApplication([
('/', MainHandler)
], debug=True)
lib.py
class FormData(object):
def __init__(self):
pass
I need to be able to access:
fd1.name = self.request.GET['name']
fd1.email = self.request.GET['email']
fd1.weight = self.request.GET['weight']
fd1.height = self.request.GET['height']
In lib.py
Your if/else statement is outside the get method, so it shouldn't even work properly.
I think the most clean way would be to pass the request data to the FormData class already in the initialiser. So the code would look like this:
main.py
import webapp2
from lib import FormData
from pages import FormPage
from pages import ResultsPage
class MainHandler(webapp2.RequestHandler):
def get(self):
f = FormPage()
s = ResultsPage()
fd1 = FormData(data=self.request.GET)
if fd1.name: # This is None, if form has not been submitted
self.response.write(s.second_page(fd1.name, fd1.email, fd1.weight, fd1.height))
else:
self.response.write(f.form_page)
app = webapp2.WSGIApplication([
('/', MainHandler)
], debug=True)
lib.py
class FormData(object):
def __init__(self, data):
self.name = data.get('name')
self.email = data.get('email')
self.weight = data.get('weight')
self.height = data.get('height')
As a side note, I don't really know this webapp2 framework, so there might be a better way to do this.

Access Flask config outside of application factory

I'm currently using the Flask Application Factory pattern with Blueprints. The issue that I'm having is how do I access the app.config object outside of the application factory?
I don't need all the configuration options from the Flask app. I just need 6 keys. So the current way I do this is when the create_app(application factory) is called, I basically create a global_config dictionary object and I just set the global_config dictionary to have the 6 keys that I need.
Then, the other modules that need those configuration options, they just import global_config dictionary.
I'm thinking, there has to be a better way to do this right?
So, on to the code
My current init.py file:
def set_global_config(app_config):
global_config['CUPS_SAFETY'] = app_config['CUPS_SAFETY']
global_config['CUPS_SERVERS'] = app_config['CUPS_SERVERS']
global_config['API_SAFE_MODE'] = app_config['API_SAFE_MODE']
global_config['XSS_SAFETY'] = app_config['XSS_SAFETY']
global_config['ALLOWED_HOSTS'] = app_config['ALLOWED_HOSTS']
global_config['SQLALCHEMY_DATABASE_URI'] = app_config['SQLALCHEMY_DATABASE_URI']
def create_app(config_file):
app = Flask(__name__, instance_relative_config=True)
try:
app.config.from_pyfile(config_file)
except IOError:
app.config.from_pyfile('default.py')
cel.conf.update(app.config)
set_global_config(app.config)
else:
cel.conf.update(app.config)
set_global_config(app.config)
CORS(app, resources=r'/*')
Compress(app)
# Initialize app with SQLAlchemy
db.init_app(app)
with app.app_context():
db.Model.metadata.reflect(db.engine)
db.create_all()
from authenication.auth import auth
from club.view import club
from tms.view import tms
from reports.view import reports
from conveyor.view import conveyor
# Register blueprints
app.register_blueprint(auth)
app.register_blueprint(club)
app.register_blueprint(tms)
app.register_blueprint(reports)
app.register_blueprint(conveyor)
return app
An example of a module that needs access to those global_config options:
from package import global_config as config
club = Blueprint('club', __name__)
#club.route('/get_printers', methods=['GET', 'POST'])
def getListOfPrinters():
dict = {}
for eachPrinter in config['CUPS_SERVERS']:
dict[eachPrinter] = {
'code': eachPrinter,
'name': eachPrinter
}
outDict = {'printers': dict, 'success': True}
return jsonify(outDict)
There has to be a better way then passing a global dictionary around the application correct?
There is no need to use global names here, that defeats the purpose of using an app factory in the first place.
Within views, such as in your example, current_app is bound to the app handling the current app/request context.
from flask import current_app
#bp.route('/')
def example():
servers = current_app.config['CUPS_SERVERS']
...
If you need access to the app while setting up a blueprint, the record decorator marks functions that are called with the state the blueprint is being registered with.
#bp.record
def setup(state):
servers = state.app.config['CUPS_SERVERS']
...

Why is testbed not flushing the datastore on TearDown() and why environ values aren't passed to the code I'm testing?

I'm trying to setup unittests for my App Engine tutorial example.
I have defined the user in self.testbed.setup_env but the data is not visible in the tested code. Somehow, despite this data not being rendered by the print statement, it's being saved as a structured property in the datastore and is not deleted in the TearDown().
The code works when I use dev_appserver.py and enter the data into the form manually.
When I attempt to run the tests with nosetests --with-gae, the mock user data is not not available
Since I'm attempting to debug a test, I can't use a debugger and I'm relying on print statements.
import sys, os, subprocess, time, unittest, shlex
sys.path.append("/usr/local/google_appengine")
sys.path.append('/usr/local/google_appengine/lib/')
sys.path.append("/usr/local/google_appengine/lib/yaml/lib")
sys.path.append("/usr/local/google_appengine/lib/webapp2-2.5.2")
sys.path.append("/usr/local/google_appengine/lib/django-1.5")
sys.path.append("/usr/local/google_appengine/lib/cherrypy")
sys.path.append("/usr/local/google_appengine/lib/concurrent")
sys.path.append("/usr/local/google_appengine/lib/docker")
sys.path.append("/usr/local/google_appengine/lib/requests")
sys.path.append("/usr/local/google_appengine/lib/websocket")
sys.path.append("/usr/local/google_appengine/lib/fancy_urllib")
sys.path.append("/usr/local/google_appengine/lib/antlr3")
os.environ['APPLICATION_ID'] = 'myapp'
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from google.appengine.api import memcache, apiproxy_stub, apiproxy_stub_map
from google.appengine.ext import testbed
from google.appengine.datastore import datastore_stub_util
from google.appengine.tools.devappserver2 import devappserver2
from guestbook import Author, Greeting
from google.appengine.api import users
class NewVisitorTest(unittest.TestCase):
# enable the datastore stub
nosegae_datastore_v3 = True
nosegae_datastore_v3_kwargs = {
'datastore_file': '/tmp/nosegae.sqlite3',
'use_sqlite': True
}
def setUp(self):
# Start the dev server
cmd = "/usr/local/bin/dev_appserver.py /Users/Bryan/work/GoogleAppEngine/guestbook/app.yaml --port 8080 --storage_path /tmp/datastore --clear_datastore --skip_sdk_update_check"
self.dev_appserver = subprocess.Popen(shlex.split(cmd),
stdout=subprocess.PIPE)
time.sleep(2) # Important, let dev_appserver start up
self.testbed = testbed.Testbed()
self.testbed.setup_env(app_id='guestbook')
self.testbed.activate()
self.datastore_stub = apiproxy_stub_map.apiproxy.GetStub('datastore_v3')
# setup the dev_appserver
APP_CONFIGS = ['app.yaml']
self.browser = webdriver.Firefox()
self.browser.implicitly_wait(3)
#
def tearDown(self):
self.testbed.deactivate()
self.browser.quit()
self.dev_appserver.terminate()
def loginUser(self, email="elonMusk#example.com", id='123456', is_admin=False):
self.testbed.setup_env(
user_email=email,
user_id=id,
user_is_admin='1' if is_admin else '0',
overwrite=True
)
self.testbed.init_user_stub()
def test_guest_can_submit_new_greeting_and_author(self):
self.browser.get('http://localhost:8080')
self.loginUser()
self.browser.find_element_by_name('content').send_keys("Test Driven Development is awesome!!!")
self.browser.find_element_by_id('submit_guestbook').submit()
# Can I access them via querys on the database?
assert("Test Driven Development is awesome!!!" in self.browser.page_source)
self.browser.get('http://localhost:8000/datastore')
assert(Greeting.query(Greeting.author.email=='elonMusk#example.com').get())
self.browser.get('http://localhost:8000/datastore')
# I'm opening the datastore to see that the entity is saved.
# The entity is saved but the Author property is empty.
time.sleep(10) # this gives me time to view the datastore in browser.
self.assertEqual(1, Greeting.query().count())
Here is questbook.py:
import os
import urllib
from google.appengine.api import users
from google.appengine.ext import ndb
import jinja2
import webapp2
JINJA_ENVIRONMENT = jinja2.Environment(
loader=jinja2.FileSystemLoader(os.path.dirname(__file__)),
extensions=['jinja2.ext.autoescape'],
autoescape=True)
DEFAULT_GUESTBOOK_NAME = 'default_guestbook'
# We set a parent key on the 'Greetings' to ensure that they are all
# in the same entity group. Queries across the single entity group
# will be consistent. However, the write rate should be limited to
# ~1/second.
def guestbook_key(guestbook_name=DEFAULT_GUESTBOOK_NAME):
"""Constructs a Datastore key for a Guestbook entity.
We use guestbook_name as the key.
"""
return ndb.Key('Guestbook', guestbook_name)
class Author(ndb.Model):
"""Sub model for representing an author."""
identity = ndb.StringProperty(indexed=False)
email = ndb.StringProperty(indexed=True)
class Greeting(ndb.Model):
"""A main model for representing an individual Guestbook entry."""
author = ndb.StructuredProperty(Author)
content = ndb.StringProperty(indexed=False)
date = ndb.DateTimeProperty(auto_now_add=True)
class MainPage(webapp2.RequestHandler):
def get(self):
guestbook_name = self.request.get('guestbook_name',
DEFAULT_GUESTBOOK_NAME)
greetings_query = Greeting.query(
ancestor=guestbook_key(guestbook_name)).order(-Greeting.date)
greetings = greetings_query.fetch(10)
user = users.get_current_user()
if user:
url = users.create_logout_url(self.request.uri)
url_linktext = 'Logout'
else:
url = users.create_login_url(self.request.uri)
url_linktext = 'Login'
template_values = {
'user': user,
'greetings': greetings,
'guestbook_name': urllib.quote_plus(guestbook_name),
'url': url,
'url_linktext': url_linktext,
}
template = JINJA_ENVIRONMENT.get_template('index.html')
self.response.write(template.render(template_values))
class Guestbook(webapp2.RequestHandler):
def post(self):
guestbook_name = self.request.get('guestbook_name',
DEFAULT_GUESTBOOK_NAME)
greeting = Greeting(parent=guestbook_key(guestbook_name))
if users.get_current_user():
greeting.author = Author(
identity=users.get_current_user().user_id(),
email=users.get_current_user().email())
greeting.content = self.request.get('content')
######################################
import os
print("XXXXXXXXXXXXXXXXXXXXX guestbook in post() USER_EMAIL: %s" % os.environ.get('USER_EMAIL'))
#print("xoxoxoxoxoxoxoxoxoxox guestbook greeting.author.email: %s" % greeting.author.email )
greeting_key = greeting.put()
query_params = {'guestbook_name': guestbook_name}
self.redirect('/?' + urllib.urlencode(query_params))
application = webapp2.WSGIApplication([
('/', MainPage),
('/sign', Guestbook),
], debug=False)
The datastore you can access from your test suite (the datastore_stub "mock" datastore) is not the same as the one the dev_appserver uses in your example. Hence changes to one of two datastores (such as adding an entity) will not be reflected in the other one, which is why your Greeting.query() returns nothing.
Instead of running a full blown, separate dev_appserver process, you can write your tests using the webtest framework (http://docs.pylonsproject.org/projects/webtest/en/latest/testapp.html) which will be simpler than having to manage a separate process; it will look this:
def setUp(self):
[...]
self.web_app = webtest.TestApp(guestbook.application)
[...]
def test_get(self):
response = self.web_app.get('/')

Simple scenario using middleware class for mobile detection with app engine

I'm a newbie to app engine and python and am just trying to get a basic idea for how things work.
I have a simple app, with one mapped url (/). All the classes I'm trying to use are in the base directory of the app.
This is my main.py - all i want to do is use the middleware class to pass a variable to the template so I can render different parts of the page depending on device type.
import webapp2
import jinja2
import os
from useragents import search_strings
jinja_environment = jinja2.Environment(
loader=jinja2.FileSystemLoader(os.path.dirname(__file__)))
class MainPage(webapp2.RequestHandler):
def get(self):
template = jinja_environment.get_template('templates/index.html')
self.response.out.write(template.render())
app = webapp2.WSGIApplication([('/', MainPage)],
debug=True)
class Middleware(object):
#staticmethod
def process_request(request):
"""Adds a "mobile" attribute to the request which is True or False
depending on whether the request should be considered to come from a
small-screen device such as a phone or a PDA
//rest of class is [here][1]
"""
import webapp2
import jinja2
import os
from useragents import search_strings
jinja_environment = jinja2.Environment(
loader=jinja2.FileSystemLoader(os.path.dirname(__file__)))
class MainPage(webapp2.RequestHandler):
def get(self):
#i don't know if you want to overwrite self.request but here it is
self.request = Middleware.process_request(self.request)
template = jinja_environment.get_template('templates/index.html')
self.response.out.write(template.render())
app = webapp2.WSGIApplication([('/', MainPage)],
debug=True)
class Middleware(object):
#staticmethod
def process_request(request):
"""Adds a "mobile" attribute to the request which is True or False
depending on whether the request should be considered to come from a
small-screen device such as a phone or a PDA
//rest of class is [here][1]
"""

Categories

Resources