mirror of
https://github.com/ae-utbm/sith.git
synced 2024-11-22 06:03:20 +00:00
core: redesign request middleware with django latest design and better use of threading
This commit is contained in:
parent
1c03ce621f
commit
5c30de5f22
@ -50,27 +50,31 @@ class AuthenticationMiddleware(DjangoAuthenticationMiddleware):
|
|||||||
def process_request(self, request):
|
def process_request(self, request):
|
||||||
assert hasattr(request, "session"), (
|
assert hasattr(request, "session"), (
|
||||||
"The Django authentication middleware requires session middleware "
|
"The Django authentication middleware requires session middleware "
|
||||||
"to be installed. Edit your MIDDLEWARE_CLASSES setting to insert "
|
"to be installed. Edit your MIDDLEWARE setting to insert "
|
||||||
"'django.contrib.sessions.middleware.SessionMiddleware' before "
|
"'django.contrib.sessions.middleware.SessionMiddleware' before "
|
||||||
"'account.middleware.AuthenticationMiddleware'."
|
"'account.middleware.AuthenticationMiddleware'."
|
||||||
)
|
)
|
||||||
request.user = SimpleLazyObject(lambda: get_cached_user(request))
|
request.user = SimpleLazyObject(lambda: get_cached_user(request))
|
||||||
|
|
||||||
|
|
||||||
class RequestMiddleware:
|
_threadlocal = threading.local()
|
||||||
|
|
||||||
|
|
||||||
|
def get_signal_request():
|
||||||
"""
|
"""
|
||||||
|
!!! Do not use if your operation is asynchronus !!!
|
||||||
Allow to access current request in signals
|
Allow to access current request in signals
|
||||||
This is a hack that looks into the thread
|
This is a hack that looks into the thread
|
||||||
!!! Do not use if your operation is asynchronus !!!
|
|
||||||
Mainly used for log purpose
|
Mainly used for log purpose
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, get_response, thread_local=threading.local()):
|
return getattr(_threadlocal, "request", None)
|
||||||
|
|
||||||
|
|
||||||
|
class SignalRequestMiddleware:
|
||||||
|
def __init__(self, get_response):
|
||||||
self.get_response = get_response
|
self.get_response = get_response
|
||||||
self.thread_local = thread_local
|
|
||||||
|
|
||||||
def __call__(self, request):
|
def __call__(self, request):
|
||||||
self.thread_local.current_request = request
|
setattr(_threadlocal, "request", request)
|
||||||
|
return self.get_response(request)
|
||||||
response = self.get_response(request)
|
|
||||||
return response
|
|
||||||
|
@ -26,7 +26,7 @@ from django.db.models.signals import pre_delete
|
|||||||
from django.dispatch import receiver
|
from django.dispatch import receiver
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
|
|
||||||
from core.middleware import RequestMiddleware
|
from core.middleware import get_signal_request
|
||||||
from core.models import OperationLog
|
from core.models import OperationLog
|
||||||
|
|
||||||
from counter.models import Selling, Refilling, Counter
|
from counter.models import Selling, Refilling, Counter
|
||||||
@ -34,13 +34,17 @@ from counter.models import Selling, Refilling, Counter
|
|||||||
|
|
||||||
def write_log(instance, operation_type):
|
def write_log(instance, operation_type):
|
||||||
def get_user():
|
def get_user():
|
||||||
request = RequestMiddleware(get_response=None).thread_local.current_request
|
request = get_signal_request()
|
||||||
|
|
||||||
|
if not request:
|
||||||
|
return None
|
||||||
|
|
||||||
# Get a random barmen if deletion is from a counter
|
# Get a random barmen if deletion is from a counter
|
||||||
session_token = request.session.get("counter_token", None)
|
session = getattr(request, "session", {})
|
||||||
|
session_token = session.get("counter_token", None)
|
||||||
if session_token:
|
if session_token:
|
||||||
counter = Counter.objects.filter(token=session_token).first()
|
counter = Counter.objects.filter(token=session_token).first()
|
||||||
if counter:
|
if counter and len(counter.get_barmen_list()) > 0:
|
||||||
return counter.get_random_barman()
|
return counter.get_random_barman()
|
||||||
|
|
||||||
# Get the current logged user if not from a counter
|
# Get the current logged user if not from a counter
|
||||||
|
@ -106,7 +106,7 @@ MIDDLEWARE = (
|
|||||||
"django.middleware.clickjacking.XFrameOptionsMiddleware",
|
"django.middleware.clickjacking.XFrameOptionsMiddleware",
|
||||||
"django.middleware.security.SecurityMiddleware",
|
"django.middleware.security.SecurityMiddleware",
|
||||||
"core.middleware.AuthenticationMiddleware",
|
"core.middleware.AuthenticationMiddleware",
|
||||||
"core.middleware.RequestMiddleware",
|
"core.middleware.SignalRequestMiddleware",
|
||||||
)
|
)
|
||||||
|
|
||||||
ROOT_URLCONF = "sith.urls"
|
ROOT_URLCONF = "sith.urls"
|
||||||
|
Loading…
Reference in New Issue
Block a user