from models import Schema

class SchemaMiddleware:
    def process_request(self, request):
        if request.user.is_anonymous():
            return None
        if request.user.is_superuser and '__schema' in request.GET:
            request.session['schema'] = request.GET['__schema']
        if request.user.is_superuser and 'schema' in request.session:
            Schema.objects.get(pk=request.session['schema']).activate()
        else:
            request.user.schema.schema.activate()
    
    def process_response(self, request):
        pass
from django.core.exceptions import ObjectDoesNotExist

from models import Schema

class SchemaMiddleware:
    def process_request(self, request):
        if request.user.is_anonymous():
            return None
        if request.user.is_superuser:
            if '__schema' in request.GET:
                request.session['schema'] = request.GET['__schema']
            if 'schema' in request.session:
                Schema.objects.get(pk=request.session['schema']).activate()
        else:
            try:
                request.user.schema.schema.activate()
            except ObjectDoesNotExist:
                pass
            
    
    def process_template_response(self, request, response):
        if request.user.is_superuser:
            response.context_data['schemata'] = Schema.objects.all()
            response.context_data['selected_schema'] = request.session['schema']
        return response
Add some data into the request context. Better handling of missing Schema objects when logging in (should we raise an error?).
Add some data into the request context.
Better handling of missing Schema objects when logging in (should we raise an error?).

+ from django.core.exceptions import ObjectDoesNotExist
+ 
  from models import Schema
  
  class SchemaMiddleware:
      def process_request(self, request):
          if request.user.is_anonymous():
              return None
-         if request.user.is_superuser and '__schema' in request.GET:
+         if request.user.is_superuser:
+             if '__schema' in request.GET:
-             request.session['schema'] = request.GET['__schema']
+                 request.session['schema'] = request.GET['__schema']
-         if request.user.is_superuser and 'schema' in request.session:
+             if 'schema' in request.session:
-             Schema.objects.get(pk=request.session['schema']).activate()
+                 Schema.objects.get(pk=request.session['schema']).activate()
          else:
+             try:
-             request.user.schema.schema.activate()
+                 request.user.schema.schema.activate()
+             except ObjectDoesNotExist:
+                 pass
+             
      
-     def process_response(self, request):
+     def process_template_response(self, request, response):
-         pass
+         if request.user.is_superuser:
+             response.context_data['schemata'] = Schema.objects.all()
+             response.context_data['selected_schema'] = request.session['schema']
+         return response
Add some data into the request context. Better handling of missing Schema objects when logging in (should we raise an error?).
## Code Before:
from models import Schema

class SchemaMiddleware:
    def process_request(self, request):
        if request.user.is_anonymous():
            return None
        if request.user.is_superuser and '__schema' in request.GET:
            request.session['schema'] = request.GET['__schema']
        if request.user.is_superuser and 'schema' in request.session:
            Schema.objects.get(pk=request.session['schema']).activate()
        else:
            request.user.schema.schema.activate()
    
    def process_response(self, request):
        pass
## Instruction:
Add some data into the request context. Better handling of missing Schema objects when logging in (should we raise an error?).
## Code After:
from django.core.exceptions import ObjectDoesNotExist

from models import Schema

class SchemaMiddleware:
    def process_request(self, request):
        if request.user.is_anonymous():
            return None
        if request.user.is_superuser:
            if '__schema' in request.GET:
                request.session['schema'] = request.GET['__schema']
            if 'schema' in request.session:
                Schema.objects.get(pk=request.session['schema']).activate()
        else:
            try:
                request.user.schema.schema.activate()
            except ObjectDoesNotExist:
                pass
            
    
    def process_template_response(self, request, response):
        if request.user.is_superuser:
            response.context_data['schemata'] = Schema.objects.all()
            response.context_data['selected_schema'] = request.session['schema']
        return response