Skip to content

Commit

Permalink
manage: use app_override default named arg instead of app to plac…
Browse files Browse the repository at this point in the history
…e nicely with flask.ext.script
  • Loading branch information
vsudilov committed Apr 27, 2015
1 parent 5aa20fa commit 77fdc69
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
24 changes: 16 additions & 8 deletions adsws/accounts/manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,25 @@
from adsws.core import db
from adsws.accounts import create_app

from flask.ext.script import Manager, Command
from flask.ext.script import Manager

_app = create_app()
accounts_manager = Manager(_app)
accounts_manager = Manager(create_app())
accounts_manager.__doc__ = __doc__ # Overwrite default docstring


@accounts_manager.command
def cleanup_tokens(app=_app):
def cleanup_tokens(app_override=None):
"""
Cleans expired oauth2tokens from the database defined in
app.config['SQLALCHEMY_DATABASE_URI']
:param app: Flask application to bind on
:param app_override: flask.app instance to use instead of manager.app
:return: None
"""

if app_override is not None:
app = app_override
else:
app = accounts_manager.app

with app.app_context():
tokens = db.session.query(OAuthToken).filter(
OAuthToken.expires <= datetime.datetime.now()
Expand All @@ -43,17 +46,22 @@ def cleanup_tokens(app=_app):


@accounts_manager.command
def cleanup_clients(app=_app, timedelta="days=31"):
def cleanup_clients(app_override=None, timedelta="days=31"):
"""
Cleans expired oauth2clients that are older than a specified date in the
database defined in app.config['SQLALCHEMY_DATABASE_URI']
:param app: Flask application to bind on
:param app_override: flask.app instance to use instead of manager.app
:param timedelta: String representing the datetime.timedelta against which
to compare client's last_activity ["days=31"].
:type timedelta: basestring
:return: None
"""

if app_override is not None:
app = app_override
else:
app = accounts_manager.app

td = {timedelta.split('=')[0]: float(timedelta.split('=')[1])}
td = datetime.timedelta(**td)

Expand Down
12 changes: 6 additions & 6 deletions adsws/tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def test_token_cleanup(self):
)

# Only those tokens which have already expired should be removed
cleanup_tokens(app=self.app)
cleanup_tokens(app_override=self.app)
current_tokens = db.session.query(OAuthToken).all()
self.assertNotEqual(original_tokens, current_tokens)
self.assertEqual(3, len(current_tokens))
Expand All @@ -121,7 +121,7 @@ def test_token_cleanup(self):
# and check that this token has been removed after calling
# the cleanup_tokens script again
time.sleep(3)
cleanup_tokens(app=self.app)
cleanup_tokens(app_override=self.app)
current_tokens = db.session.query(OAuthToken).all()
self.assertNotEqual(original_tokens, current_tokens)
self.assertEqual(2, len(current_tokens))
Expand All @@ -143,26 +143,26 @@ def test_client_cleanup(self):
)

# No clients should be cleaned
cleanup_clients(app=self.app, timedelta="days=31")
cleanup_clients(app_override=self.app, timedelta="days=31")
current_clients = db.session.query(OAuthClient).all()
self.assertEqual(5, len(current_clients))

# Cleanup all clients that are older than 0 seconds from now()
cleanup_clients(app=self.app, timedelta="seconds=0")
cleanup_clients(app_override=self.app, timedelta="seconds=0")
current_clients = db.session.query(OAuthClient).all()
self.assertEqual(3, len(current_clients))

# Wait 3 seconds, then perform the same cleanup. Should have one less
# client after this operation.

time.sleep(3.1)
cleanup_clients(app=self.app, timedelta="seconds=0.1")
cleanup_clients(app_override=self.app, timedelta="seconds=0.1")
current_clients = db.session.query(OAuthClient).all()
self.assertEqual(2, len(current_clients))

# Cleanup the client whose last_activity was set to 1 hour
# into the future. This case should never happen in practice!
cleanup_clients(app=self.app, timedelta="hours=-1")
cleanup_clients(app_override=self.app, timedelta="hours=-1")
current_clients = db.session.query(OAuthClient).all()
self.assertEqual(1, len(current_clients))

Expand Down

0 comments on commit 77fdc69

Please sign in to comment.