Commit 1f36f70d authored by Tom van Bussel's avatar Tom van Bussel

Fix MemberMiddleware in the API

The MemberMiddleware was broken when DRF's TokenAuthentication was used.
parent 8a9d9adc
from django.utils.functional import SimpleLazyObject
from members.models import Member
def get_member(request):
try:
return Member.objects.get(pk=request.user.pk)
except Member.DoesNotExist:
return None
class MemberMiddleware:
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request):
try:
request.member = Member.objects.get(pk=request.user.pk)
except Member.DoesNotExist:
request.member = None
# This needs to be a lazy object as Django REST Frameworks calls the
# the middleware before setting request.user
# This also avoids unnecessary queries when request.member is not used
request.member = SimpleLazyObject(lambda: get_member(request))
return self.get_response(request)
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