mirror of
https://github.com/netbox-community/netbox.git
synced 2025-08-26 01:06:11 -06:00
cleaned up code
This commit is contained in:
parent
6346e4dd60
commit
496cfb4dca
@ -1,7 +1,6 @@
|
||||
import logging
|
||||
import time
|
||||
|
||||
from django.conf import settings
|
||||
from django.db.backends.utils import CursorWrapper as _CursorWrapper
|
||||
|
||||
from netbox.exceptions import DatabaseWriteDenied
|
||||
@ -10,11 +9,8 @@ logger = logging.getLogger('netbox.db')
|
||||
|
||||
class ReadOnlyCursorWrapper:
|
||||
"""
|
||||
A read-only wrapper around a database cursor.
|
||||
|
||||
This wrapper prevents write operations from being performed on the database. It is used to prevent changes to the
|
||||
database during a read-only request. It is not intended to be used directly; rather, it is applied automatically by
|
||||
the ReadOnlyMiddleware. See the documentation for that class for more information.
|
||||
database during a read-only request.
|
||||
"""
|
||||
|
||||
SQL_BLACKLIST = (
|
||||
@ -35,20 +31,19 @@ class ReadOnlyCursorWrapper:
|
||||
def __init__(self, cursor, db, *args, **kwargs):
|
||||
self.cursor = cursor
|
||||
self.db = db
|
||||
self.read_only = settings.MAINTENANCE_MODE
|
||||
|
||||
def __check_sql(self, sql):
|
||||
if self._write_sql(sql):
|
||||
raise DatabaseWriteDenied
|
||||
|
||||
def execute(self, sql, params=()):
|
||||
# Check the SQL
|
||||
if self.read_only and self._write_sql(sql):
|
||||
raise DatabaseWriteDenied
|
||||
|
||||
self.__check_sql(sql)
|
||||
return self.cursor.execute(sql, params)
|
||||
|
||||
def executemany(self, sql, param_list):
|
||||
# Check the SQL
|
||||
if self.read_only and self._write_sql(sql):
|
||||
raise DatabaseWriteDenied
|
||||
|
||||
self.__check_sql(sql)
|
||||
return self.cursor.executemany(sql, param_list)
|
||||
|
||||
def __getattr__(self, item):
|
||||
|
@ -229,20 +229,13 @@ class DatabaseReadOnlyMiddleware(MiddlewareMixin):
|
||||
if not isinstance(exception, DatabaseWriteDenied):
|
||||
return None
|
||||
|
||||
not_allowed_methods = ['POST', 'PUT', 'PATCH', 'DELETE']
|
||||
error_message = 'The database is currently in read-only mode. Please try again later.'
|
||||
status_code = 503
|
||||
|
||||
# If the request is an API request, return a 503 Service Unavailable response
|
||||
if is_api_request(request) and request.method in not_allowed_methods:
|
||||
return JsonResponse({'detail': error_message, }, status=status_code)
|
||||
if is_api_request(request):
|
||||
return JsonResponse({'detail': error_message}, status=status_code)
|
||||
else:
|
||||
# Handle exceptions
|
||||
if request.method in not_allowed_methods:
|
||||
# Display a message to the user
|
||||
messages.error(request, error_message)
|
||||
|
||||
# Redirect back to the referring page
|
||||
return HttpResponseReload(request)
|
||||
else:
|
||||
return HttpResponse(error_message, status=status_code)
|
||||
# Display a message to the user
|
||||
messages.error(request, error_message)
|
||||
# Redirect back to the referring page
|
||||
return HttpResponseReload(request)
|
||||
|
Loading…
Reference in New Issue
Block a user