49b7ea3b2a5f485846b98487afc754f89da34708
[auf_savoirs_en_partage_django.git] / auf_savoirs_en_partage / chercheurs / middleware.py
1 from chercheurs.models import Personne, Chercheur
2
3 class LazyChercheur(object):
4
5 def __get__(self, request, obj_type=None):
6 if not hasattr(request, '_cached_chercheur'):
7 request._cached_chercheur = None
8 if request.user.is_authenticated():
9 try:
10 request._cached_chercheur = Chercheur.objects.get(actif=True, courriel=request.user.email)
11 except (Personne.DoesNotExist, Chercheur.DoesNotExist):
12 pass
13 return request._cached_chercheur
14
15 class ChercheurMiddleware(object):
16
17 def process_request(self, request):
18 request.__class__.chercheur = LazyChercheur()