globally fewer db requests

This commit is contained in:
Aurélien Delobelle 2017-04-06 14:15:03 +02:00
parent f57bab8ae9
commit 1302adf156

View file

@ -1,15 +1,28 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from __future__ import (absolute_import, division, from django.contrib.auth.models import User
print_function, unicode_literals)
from builtins import *
from django.http import HttpResponseForbidden
from kfet.backends import KFetBackend from kfet.backends import KFetBackend
from kfet.models import Account
class KFetAuthenticationMiddleware(object): class KFetAuthenticationMiddleware(object):
"""Authenticate another user for this request if KFetBackend succeeds.
By the way, if a user is authenticated, we refresh its from db to add
values from CofProfile and Account of this user.
"""
def process_request(self, request): def process_request(self, request):
if request.user.is_authenticated():
# avoid multiple db accesses in views and templates
user_pk = request.user.pk
print(user_pk)
request.user = (
User.objects
.select_related('profile__account_kfet')
.get(pk=user_pk)
)
kfet_backend = KFetBackend() kfet_backend = KFetBackend()
temp_request_user = kfet_backend.authenticate(request) temp_request_user = kfet_backend.authenticate(request)
if temp_request_user: if temp_request_user: