Flask application context error not solved with 'with' clausule - flask

I am attempting to create a Flask middleware in order to make py2neo transactions atomic. First I got a working outside of application context error, and I tried to apply this solution, as seen in the following code:
from flask import g
from py2neo import Graph
def get_db():
return Graph(password="secret")
class TransactionMiddleware(object):
def __init__(self, app):
self.app = app
with app.app_context(): # Error raises here.
g.graph = get_db()
g.transaction = g.graph.begin()
def __call__(self, environ, start_response):
try:
app_status = self.app(environ, start_response)
# some code
return app_status
except BaseException:
g.transaction.rollback()
raise
else:
g.transaction.commit()
But I got this error: AttributeError: 'function' object has no attribute 'app_context'.
I don't know if the solution I'm trying is not suitable for my case, or what is the problem.

You are in a WSGI middleware, so what gets passed in as an app is actually a method called Flask.wsgi_app the results of which you a later supposed to return from your __call__ handler.
Perhaps you can simply import your actual flask app and use app_context on that, as in:
from app import app # or whatever module it's in
# and then
class TransactionMiddleware(object):
...
def __call__(self, environ, start_response):
with app.app_context():
...
I would also consider just instantiating your Graph somehere on a module level. Perhaps next to your flask app instantiation and not attaching it to g. This way you can use it without application context:
# app.py
flask = Flask(__main__)
db = Graph(password="secret")
You can use it by directly importing:
# middleware.py
from app import db

Related

module 'requests' has no attribute 'files'

Running the script gives below error :
AttributeError: module 'requests' has no attribute 'files'
Code is as follows :
import requests
app = Flask(__name__)
#app.route('/api/test/', methods=['POST'])
def test():
filesReceived = requests.files['file']
What I understand from your code, you are trying to get the file received by the request, right?
If that's what you want, you are using the wrong import.
Requests is an http library for python.
If you want the file sent in the request, you need to use request from flask.
So your code will be something like:
from flask import request
app = Flask(__name__)
#app.route('/api/test/', methods=['POST'])
def test():
filesReceived = request.files['file']

AttributeError: type object 'DeleteAccount' has no attribute 'DAccount'

I'm using Python 2.7, webapp2 and GAE for developing a web app.
I'm facing a problem which i don't understand, since I know what it means but everything I did seems correct.
I get the error
AttributeError: type object 'DeleteAccount' has no attribute 'DAccount'
when i try to access the URL myappdomain/DeleteAccount/
I imported in the main file the python file DeleteAccount.py
import DeleteAccount
I linked the URL with the relative class DAccount of DeleteAccount
webapp2.Route(r'/DeleteAccount/',DeleteAccount.DAccount)
That's my DeleteAccount.py file
import logging
import webapp2
from webapp2_extras import sessions
import datastore
import FBLogin
class BaseHandler(webapp2.RequestHandler):
def dispatch(self):
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):
return self.session_store.get_session()
class DAccount(BaseHandler):
def get(self):
w = self.response.write
logging.info("deleting account")
w('I've deleted the account')
What's wrong? I did what I've done with every other module and everything has worked well with them

Clearing Python Flask cache does not work

I have the following three files.
app.py
from flask_restful import Api
from lib import globals
from flask import Flask
from flask.ext.cache import Cache
globals.algos_app = Flask(__name__)
#cache in file system
globals.cache = Cache(globals.algos_app, config={'CACHE_TYPE': 'filesystem', 'CACHE_DIR': '/tmp'})
api = Api(globals.algos_app)
api.add_resource(Test, '/test')
if __name__ == '__main__':
globals.algos_app.run(host='0.0.0.0', debug=True)
globals.py
global algos_app
global cache
Test.py
from flask_restful import Resource
from lib import globals
from flask_restful import Resource
import time
class Test(Resource):
def get(self):
return self.someMethod()
def post(self):
globals.cache.clear()
return self.someMethod()
#globals.cache.cached()
def someMethod(self):
return str(time.ctime())
I have a GET method which needs to the value from the cache and a POST method which updates the cache by first clearing the cache.
However, no matter I call the GET or the POST method, it always gives me the value from the cache.
PS: At the moment I am simply testing on the development server however I do need to deploy it using WSGI later.
I am not sure if it is the best way, but I did it using the following way.
class Test(Resource):
def get(self):
return globals.cache.get('curr_time')
def post(self):
result = self.someMethod()
globals.cache.set('curr_time', result, timeout=3600)
def someMethod(self):
return str(time.ctime())

How to access the orm with celery tasks?

I'm trying to flip a boolean flag for particular types of objects in my database using sqlalchemy+celery beats. But how do I access my orm from the tasks.py file?
from models import Book
from celery.decorators import periodic_task
from application import create_celery_app
celery = create_celery_app()
# Create celery: http://flask.pocoo.org/docs/0.10/patterns/celery/
# This task works fine
#celery.task
def celery_send_email(to,subject,template):
with current_app.app_context():
msg = Message(
subject,
recipients=[to],
html=template,
sender=current_app.config['MAIL_DEFAULT_SENDER']
)
return mail.send(msg)
#This fails
#periodic_task(name='release_flag',run_every=timedelta(seconds=10))
def release_flag():
with current_app.app_context(): <<< #Fails on this line
books = Book.query.all() <<<< #Fails here too
for book in books:
book.read = True
book.save()
I'm using celery beat command to run this:
celery -A tasks worker -l INFO --beat
But I'm getting the following error:
raise RuntimeError('working outside of application context')
RuntimeError: working outside of application context
Which points back to the with current_app.app_context() line
If I remove the current_app.app_context() line I will get the following error:
RuntimeError: application not registered on db instance and no application bound to current context
Is there a particular way to access the flask-sqlalchemy orm for celery tasks? Or would there be a better approach to what I'm trying to do?
So far the only workaround which works was to add the following line after db.init_app(app) in my application factory pattern:
db.app = app
I was following this repo to create my celery app https://github.com/mattupstate/overholt/blob/master/overholt/factory.py
You're getting that error because current_app requires an app context to work, but you're trying to use it to set up an app context. You need to use the actual app to set up the context, then you can use current_app.
with app.app_context():
# do stuff that requires the app context
Or you can use a pattern such as the one described in the Flask docs to subclass celery.Task so it knows about the app context by default.
from celery import Celery
def make_celery(app):
celery = Celery(app.import_name, broker=app.config['CELERY_BROKER_URL'])
celery.conf.update(app.config)
TaskBase = celery.Task
class ContextTask(TaskBase):
abstract = True
def __call__(self, *args, **kwargs):
with app.app_context():
return TaskBase.__call__(self, *args, **kwargs)
celery.Task = ContextTask
return celery
celery = make_celery(app)

how to import or call a python module/class in other modules with different file paths

I want to follow this structure for a WEB "WSGI pep 3333" API (educational purposes):
/home/
`--app.py
`--API_module/
`--__init__.py
`--api.py
`--exceptions.py
`--modules/
`--default/
`--__init__.py
`--default.py
app.py calls API_module with something like:
app = API_module.api()
the api.py based on "HTTP GET requests" will load modules stored on directory named modules, for now, I am just loading a module named default.
api.py looks like:
import os
import imp
from exceptions import HTTPError, HTTPException
class API(object):
def __call__(self, env, start_response):
self.env = env
self.method = env['REQUEST_METHOD']
try:
body = self.router()
body.dispatch()
except HTTPError, e:
print 'HTTP method not valid %s' % e
except, Exception e:
print 'something went wrong'
start_response(status, headers)
yield body
def router():
module_path = '/home/modules/default/default.py'
if not os.access(module_path, os.R_OK):
raise HTTPException()
else:
py_mod = imp.load_source('default', '/home/modules/default/default.py'
return py_mod.Resource(self)
and default.py contains something like:
class Resoure(object):
def __init__(self, app):
self.app = app
def dispatch(self):
raise HTTPException()
So far I can import dynamically the modules but if I want to raise an exception from the default.py module I get an:
global name 'HTTPException' is not defined
Therefore I would like to know, how to take advantage of the API_module/exceptions and use them in all the dynamically modules.
Any ideas, suggestions comments are welcome.
It's a matter of sys.path.
In your setup, since your API_module is included from app.py you should have the root of your application in your sys.path so you should just include the API_module in the usual way:
from API_module import exceptions
or
from API_module.exceptions import *
based on how you use it.
Btw, i suggest you use pymod = __import__('modules.default.default', fromlist=['']) and put an __init__.py' file in themodules/` root directory.