1
0
mirror of https://github.com/peeringdb/peeringdb.git synced 2024-05-11 05:55:09 +00:00
Files
peeringdb-peeringdb/peeringdb_server/maintenance.py
Matt Griswold ba6f9b6432 Qu1003 (#621)
* use new peeringdb client (1.0.0) for pdb_load_data sync (#599)

* drop django-mobi for lack of py3/dj2 support (#492)
remove django-forms-bootstrap for lack of py3/dj2 support (#492)

* black formatted

* django2.2 and py3 upgrade (#492)

* drop ixlans (#21) ui and api changes

* drop local_asn (#168)

* org search (#193)

* phone number validation (#50)

* implement help text tooltips (#228)

* Mark own ASN as transit-free (#394)

* py3 fix for `pdb_migrate_ixlans` command when writing migration report

* pdb_migrate_ixlans: properly handle py3 Runtime error if ixlan dict changes during iteration

* set rest DEFAULT_SCHEMA_CLASS to coreapi to fix swagger apidocs
fix migration 0027 missing from facsimile manifest

* fix swagger doc strings

* fix tests that were broken from api doc fixes

* fix UniqueFieldValidator for netixlan ipaddress validation that broke during django/drf upgrade

* fix org merge tool layout issues

* travis config

* update pipfile and lock

* black formatting

* update travis dist

* beta mode banner (#411)

* add beta banner template (#411)

* automatically scheduled sync may not always be on, add a flag that lets us reflect that state in the beta banner message
clean up beta banner implementation (#411)

* add tests for beta banner (#411)
2020-01-08 13:29:58 -06:00

89 lines
2.4 KiB
Python

import os
from django.core.cache import cache
from django.utils.translation import ugettext_lazy as _
from django.http import JsonResponse
from django.urls import reverse, resolve
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(object):
"""
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(ActionBlocked, self).__init__(
"The site is currently in maintenance mode, during which this action is disabled, please try again in a few minutes"
)