Commit e33e6550 authored by Thom Wiggers's avatar Thom Wiggers

Merge branch 'fix/member-middleware-drf' into 'master'

Fix MemberMiddleware in the API

See merge request !616
parents 8a9d9adc 1f36f70d
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