1
0
mirror of https://github.com/peeringdb/peeringdb.git synced 2024-05-11 05:55:09 +00:00
Files
Stefan Pratter 5eb925e319 Support 202301 (#1329)
* fix next redirect when using U2F 2FA auth (#1191)

* Added self identifier to API

* fix migrations hierarchy after merging in previous support branch

* campus object

Co-authored-by: Stefan Pratter <stefan@20c.com>

* fix out of bound error message
add city / country to campus view

* fix tests

* relock poetry

* linting

* linting

* fix docs regen

* regen docs

* linting

* refactor self entity view to support carrier and campus object types and also make it easier to support additional object types in the future

* remove debug message

---------

Co-authored-by: Gajanan Patil <dipaksavaliya.python@gmail.com>
2023-02-15 07:55:01 +00:00

88 lines
2.3 KiB
Python

"""
Django middleware to handle maintenance mode.
"""
import os
from django.http import JsonResponse
from django.urls import resolve, reverse
from rest_framework.viewsets import ModelViewSet
from peeringdb_server import settings
def on(timeout=None):
"""
Turn maintenance mode on.
Keyword Arguments:
- timeout<int=None>: if specified will automatically
end maintenance mode after n seconds
"""
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:
"""
Middleware will return 503 json responses for all write
ops (POST PUT PATCH DELETE).
"""
def __init__(self, get_response=None):
self.get_response = get_response
def __call__(self, request):
response = self.process_request(request)
if response:
return response
return self.get_response(request)
def process_request(self, request):
if not active():
return None
if request.method.lower() in ["post", "put", "patch", "delete"]:
view, args, kwargs = resolve(request.path_info)
if view.__name__ in ["request_login"]:
# login should be allowed even in maint. mode
return None
elif hasattr(view, "cls") and issubclass(view.cls, ModelViewSet):
# api response
return JsonResponse(
{"meta": {"error": str(ActionBlocked())}}, status=503
)
else:
# other
resolve(reverse("maintenance"))
return JsonResponse(
{"non_field_errors": [str(ActionBlocked())]}, status=503
)
else:
return None
class ActionBlocked(Exception):
def __init__(self):
super().__init__(
"The site is currently in maintenance mode, during which this action is disabled, please try again in a few minutes"
)