Browse Source

Allow calling after_request outside of request context

master v1.0.1
Brett Langdon 9 years ago
parent
commit
e99cc374d7
No known key found for this signature in database GPG Key ID: A2ECAB73CE12147F
5 changed files with 27 additions and 4 deletions
  1. +10
    -1
      CHANGELOG
  2. +4
    -0
      README.rst
  3. +7
    -1
      flask_defer.py
  4. +1
    -1
      setup.py
  5. +5
    -1
      test_flask_defer.py

+ 10
- 1
CHANGELOG View File

@ -1,5 +1,14 @@
Flask-Defer Changelog
===================
=====================
Version 1.0.1
-------------
:Released: 11-28-2016
:Changes:
Execute tasks immediately if we are outside of a request context
Version 1.0.0
-------------


+ 4
- 0
README.rst View File

@ -19,6 +19,10 @@ Installation
Usage
~~~~~
Passing a function and it's arguments to `flask_defer.after_request` will register that function to execute when the Flask request has ended.
If a call to `flask_defer.after_request` happens outside of a request context then the function will be executed immediately.
.. code:: python
from flask import Flask


+ 7
- 1
flask_defer.py View File

@ -7,8 +7,14 @@ __all__ = ['after_request', 'defer', 'FlaskDefer']
def defer(func, *args, **kwargs):
params = dict(func=func, args=args, kwargs=kwargs)
ctx = stack.top
# If we are not in a request/app context, then just execute
if not ctx:
return func(*args, **kwargs)
# 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'):
setattr(ctx, 'deferred_tasks', [])
ctx.deferred_tasks.append(params)


+ 1
- 1
setup.py View File

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


+ 5
- 1
test_flask_defer.py View File

@ -5,7 +5,7 @@ from flask_defer import FlaskDefer, defer, stack
def deferred_task(name, with_keyword=False):
pass
return (name, with_keyword)
class TestFlaskDefer(unittest.TestCase):
@ -114,6 +114,10 @@ class TestFlaskDefer(unittest.TestCase):
kwargs=dict(with_keyword=True, extra_param='param'),
))
def test_no_app_context(self):
result = defer(deferred_task, 'name', with_keyword=True)
self.assertEqual(result, ('name', True))
if __name__ == '__main__':
unittest.main()

Loading…
Cancel
Save