middleware.py 723 B

1234567891011121314151617
  1. from django.core.exceptions import ImproperlyConfigured
  2. from django.core import serializers
  3. from adhesions.models import Corporation
  4. class CorporationMiddleware:
  5. def __init__(self, get_response):
  6. self.get_response = get_response
  7. def __call__(self, request):
  8. if request.user.is_authenticated:
  9. if 'corporations' not in request.session:
  10. corporations = Corporation.objects.filter(members=request.user)
  11. request.session['corporations'] = serializers.serialize('json', corporations, fields=('pk', 'social_reason',))
  12. request.corporations = serializers.deserialize('json', request.session['corporations'])
  13. return self.get_response(request)