Verified Commit 5c30de5f authored by Sli's avatar Sli
Browse files

core: redesign request middleware with django latest design and better use of threading

parent 1c03ce62
Pipeline #2138 passed with stage
in 37 minutes and 38 seconds
......@@ -50,27 +50,31 @@ class AuthenticationMiddleware(DjangoAuthenticationMiddleware):
def process_request(self, request):
assert hasattr(request, "session"), (
"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 "
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
This is a hack that looks into the thread
!!! Do not use if your operation is asynchronus !!!
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.thread_local = thread_local
def __call__(self, request):
self.thread_local.current_request = request
response = self.get_response(request)
return response
setattr(_threadlocal, "request", request)
return self.get_response(request)
......@@ -26,7 +26,7 @@ from django.db.models.signals import pre_delete
from django.dispatch import receiver
from django.conf import settings
from core.middleware import RequestMiddleware
from core.middleware import get_signal_request
from core.models import OperationLog
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 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
session_token = request.session.get("counter_token", None)
session = getattr(request, "session", {})
session_token = session.get("counter_token", None)
if session_token:
counter = Counter.objects.filter(token=session_token).first()
if counter:
if counter and len(counter.get_barmen_list()) > 0:
return counter.get_random_barman()
# Get the current logged user if not from a counter
......@@ -106,7 +106,7 @@ MIDDLEWARE = (
ROOT_URLCONF = "sith.urls"
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment