mirror of
https://github.com/peeringdb/peeringdb.git
synced 2024-05-11 05:55:09 +00:00
* django3, py39, lgtm, linting (#715) * IX-F Importer: ticket status change when posting re-occuring conflict to existing resolved ticket (#920) * fix recaptcha requirement for user creation in django-admin (#715) * IX-F Importer: fix command output buffering #967 * Drop dot1q_support field #903 * fix test (#967) * Add name, city, country to ixfac (GET operation) #166 * additional tests fir #166 * Allow IXP to trigger ix-f importer for their exchange #779 * add docker compose for dev * add selinux labels for mountpoints * fixes #1013: The process to permanently remove old soft-deleted network contacts pdb_delete_pocs raises a false ProtectedAction * fix api test * relock poetry * remove django_namespace_perms from installed apps * fix user permissios ui * remove remaining references to django namespace perms * linting * copy tox.ini * comment flake8 check until we figure out why it ignores configs from tox.ini * black format * poetry lock Co-authored-by: Stefan Pratter <stefan@20c.com>
86 lines
2.3 KiB
Python
86 lines
2.3 KiB
Python
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):
|
|
"""
|
|
turns 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
|
|
fn, args, kwargs = 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"
|
|
)
|