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 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: class MemberMiddleware:
def __init__(self, get_response): def __init__(self, get_response):
self.get_response = get_response self.get_response = get_response
def __call__(self, request): def __call__(self, request):
try: # This needs to be a lazy object as Django REST Frameworks calls the
request.member = Member.objects.get(pk=request.user.pk) # the middleware before setting request.user
except Member.DoesNotExist: # This also avoids unnecessary queries when request.member is not used
request.member = None request.member = SimpleLazyObject(lambda: get_member(request))
return self.get_response(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