mirror of
https://github.com/peeringdb/peeringdb.git
synced 2024-05-11 05:55:09 +00:00
black formatted
This commit is contained in:
@@ -9,6 +9,7 @@ from rest_framework.viewsets import ModelViewSet
|
||||
|
||||
from peeringdb_server import settings
|
||||
|
||||
|
||||
def on(timeout=None):
|
||||
"""
|
||||
turns maintenance mode on
|
||||
@@ -18,22 +19,26 @@ def on(timeout=None):
|
||||
- timeout<int=None>: if specified will automatically
|
||||
end maintenance mode after n seconds
|
||||
"""
|
||||
open(settings.MAINTENANCE_MODE_LOCKFILE, 'ab', 0).close()
|
||||
open(settings.MAINTENANCE_MODE_LOCKFILE, "ab", 0).close()
|
||||
|
||||
|
||||
def off():
|
||||
""" turn maintenance mode off """
|
||||
if active():
|
||||
os.remove(settings.MAINTENANCE_MODE_LOCKFILE)
|
||||
|
||||
|
||||
def active():
|
||||
""" return True if maintenance mode is currently active """
|
||||
return os.path.isfile(settings.MAINTENANCE_MODE_LOCKFILE)
|
||||
|
||||
|
||||
def raise_if_active():
|
||||
""" raise ActionBlocked exception if maintenance mode is active """
|
||||
if active():
|
||||
raise ActionBlocked()
|
||||
|
||||
|
||||
class Middleware(object):
|
||||
|
||||
"""
|
||||
@@ -45,7 +50,7 @@ class Middleware(object):
|
||||
if not active():
|
||||
return None
|
||||
|
||||
if request.method.lower() in ["post","put","patch","delete"]:
|
||||
if request.method.lower() in ["post", "put", "patch", "delete"]:
|
||||
|
||||
view, args, kwargs = resolve(request.path_info)
|
||||
|
||||
@@ -54,15 +59,21 @@ class Middleware(object):
|
||||
return None
|
||||
elif hasattr(view, "cls") and issubclass(view.cls, ModelViewSet):
|
||||
# api response
|
||||
return JsonResponse({"meta":{"error":str(ActionBlocked())}}, status=503)
|
||||
return JsonResponse(
|
||||
{"meta": {"error": str(ActionBlocked())}}, status=503
|
||||
)
|
||||
else:
|
||||
# other
|
||||
fn, args, kwargs = resolve(reverse("maintenance"))
|
||||
return JsonResponse({"non_field_errors":[str(ActionBlocked())]}, status=503)
|
||||
return JsonResponse(
|
||||
{"non_field_errors": [str(ActionBlocked())]}, status=503
|
||||
)
|
||||
else:
|
||||
return None
|
||||
|
||||
|
||||
class ActionBlocked(Exception):
|
||||
def __init__(self):
|
||||
super(ActionBlocked, self).__init__("The site is currently in maintenance mode, during which this action is disabled, please try again in a few minutes")
|
||||
super(ActionBlocked, self).__init__(
|
||||
"The site is currently in maintenance mode, during which this action is disabled, please try again in a few minutes"
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user