from django.http import HttpResponseRedirect

class RedirectToHomeMiddleware:
    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request):
        if request.path != '/' and (not request.session or request.session.get('visited_home', False)):
            return HttpResponseRedirect('/')

        elif request.path == '/':
            if not request.session:
                request.session = {}

            request.session['visited_home'] = True

        response = self.get_response(request)
        return response