2015-05-06 19:30:13 +00:00
|
|
|
import sys
|
|
|
|
|
2015-05-03 17:44:46 +00:00
|
|
|
from django import db
|
|
|
|
|
|
|
|
|
2015-05-06 19:30:13 +00:00
|
|
|
def running_syncdb():
|
|
|
|
return 'migrate' in sys.argv or 'syncdb' in sys.argv or 'makemigrations' in sys.argv
|
|
|
|
|
|
|
|
|
|
|
|
def database_ready():
|
|
|
|
return (
|
|
|
|
not running_syncdb() and
|
|
|
|
'setuppostgres' not in sys.argv and
|
|
|
|
'test' not in sys.argv and
|
|
|
|
# Celerybeat has yet to stablish a connection at AppConf.ready()
|
|
|
|
'celerybeat' not in sys.argv and
|
|
|
|
# Allow to run python manage.py without a database
|
|
|
|
sys.argv != ['manage.py'] and '--help' not in sys.argv
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2015-05-03 17:44:46 +00:00
|
|
|
def close_connection(execute):
|
|
|
|
""" Threads have their own connection pool, closing it when finishing """
|
|
|
|
def wrapper(*args, **kwargs):
|
|
|
|
try:
|
|
|
|
log = execute(*args, **kwargs)
|
|
|
|
except Exception as e:
|
2015-05-06 14:39:25 +00:00
|
|
|
raise
|
2015-05-03 17:44:46 +00:00
|
|
|
else:
|
|
|
|
wrapper.log = log
|
|
|
|
finally:
|
|
|
|
db.connection.close()
|
|
|
|
return wrapper
|