Browse Source

Always use request context

master
Brett Langdon 9 years ago
parent
commit
d2217f3125
No known key found for this signature in database GPG Key ID: A2ECAB73CE12147F
4 changed files with 18 additions and 16 deletions
  1. +9
    -0
      CHANGELOG
  2. +6
    -13
      flask_defer.py
  3. +1
    -1
      setup.py
  4. +2
    -2
      test_flask_defer.py

+ 9
- 0
CHANGELOG View File

@ -1,6 +1,15 @@
Flask-Defer Changelog
=====================
Version 1.1.0
-------------
:Released: 11-29-2016
:Changes:
Public API is the same, except now we only attach functions to the request context
Version 1.0.1
-------------


+ 6
- 13
flask_defer.py View File

@ -1,18 +1,15 @@
try:
from flask import _app_ctx_stack as stack
except ImportError:
from flask import _request_ctx_stack as stack
from flask import has_request_context, _request_ctx_stack
__all__ = ['after_request', 'defer', 'FlaskDefer']
def defer(func, *args, **kwargs):
ctx = stack.top
# If we are not in a request/app context, then just execute
if not ctx:
if not has_request_context():
return func(*args, **kwargs)
ctx = _request_ctx_stack.top
# We are in a request/app context, defer until request/app teardown
params = dict(func=func, args=args, kwargs=kwargs)
if not hasattr(ctx, 'deferred_tasks'):
@ -30,14 +27,10 @@ class FlaskDefer(object):
self.init_app(app)
def init_app(self, app):
if hasattr(app, 'teardown_appcontext'):
app.teardown_appcontext(self._execute_deferred)
else:
app.teardown_request(self._execute_deferred)
app.teardown_request(self._execute_deferred)
def _execute_deferred(self, exception):
ctx = stack.top
ctx = _request_ctx_stack.top
if hasattr(ctx, 'deferred_tasks'):
for params in ctx.deferred_tasks:
# DEV: Do not try/except, let these function calls fail
params['func'](*params['args'], **params['kwargs'])

+ 1
- 1
setup.py View File

@ -12,7 +12,7 @@ def get_long_description():
setup(
name='Flask-Defer',
version='1.0.1',
version='1.1.0',
url='https://github.com/brettlangdon/flask-defer.git',
license='MIT',
author='Brett Langdon',


+ 2
- 2
test_flask_defer.py View File

@ -1,7 +1,7 @@
import unittest
from flask import Flask
from flask_defer import FlaskDefer, defer, stack
from flask import Flask, _request_ctx_stack as stack
from flask_defer import FlaskDefer, defer
def deferred_task(name, with_keyword=False):


Loading…
Cancel
Save