import os
import sys
from io import StringIO

from django.conf import settings
from django.http import HttpResponseRedirect, HttpResponse
from django.utils import timezone


class AjaxRedirect:

    def __init__(self, get_response=None):
        self.get_response = get_response

    def __call__(self, request):
        response = self.get_response(request)

        if request.is_ajax():
            if type(response) == HttpResponseRedirect:
                # This is our own AJAX friend redirect to allow
                # the calling Javascript the opportunity to deal
                # with redirect responses in its own way
                response = HttpResponse(response["Location"])
                response.status_code = 302
        return response


class ProfileMiddleware:
    def __init__(self, get_response=None):
        self.get_response = get_response

    def __call__(self, request):
        response = self.get_response(request)
        return response

    def process_view(self, request, view, *args, **kwargs):
        import hotshot, hotshot.stats  # noqa

        for item in request.META['QUERY_STRING'].split('&'):
            if item.split('=')[0] == 'profile':  # profile in query string

                # catch the output, must happen before stats object is created
                # see https://bugs.launchpad.net/webpy/+bug/133080 for the details
                std_old, std_new = sys.stdout, StringIO.StringIO()
                sys.stdout = std_new

                # now let's do some profiling
                tmpfile = '/tmp/%s' % request.COOKIES['sessionid']
                prof = hotshot.Profile(tmpfile)

                # make a call to the actual view function with the given arguments
                prof.runcall(view, request, *args[0], **args[1])
                prof.close()

                # and then statistical reporting
                stats = hotshot.stats.load(tmpfile)
                stats.strip_dirs()
                stats.sort_stats('time')

                # do the output
                stats.print_stats(1.0)

                # restore default output
                sys.stdout = std_old

                # delete file
                os.remove(tmpfile)

                return HttpResponse('<pre>%s</pre>' % std_new.getvalue())

        return None


class TimezoneMiddleware:

    def __init__(self, get_response=None):
        self.get_response = get_response

    def __call__(self, request):
        response = self.get_response(request)

        user_tz = getattr(settings, 'USER_TIME_ZONE', None)

        if user_tz:
            timezone.activate(user_tz)

        return response

@@ -1,2317 +1,67 @@
-import os%0Aimport sys%0Afrom io import StringIO%0A%0Afrom django.conf import settings%0Afrom django.http import HttpResponseRedirect, HttpResponse%0Afrom django.utils import timezone%0A%0A%0Aclass AjaxRedirect:%0A%0A    def __init__(self, get_response=None):%0A        self.get_response = get_response%0A%0A    def __call__(self, request):%0A        response = self.get_response(request)%0A%0A        if request.is_ajax():%0A            if type(response) == HttpResponseRedirect:%0A                # This is our own AJAX friend redirect to allow%0A                # the calling Javascript the opportunity to deal%0A                # with redirect responses in its own way%0A                response = HttpResponse(response%5B%22Location%22%5D)%0A                response.status_code = 302%0A        return response%0A%0A%0Aclass ProfileMiddleware:%0A    def __init__(self, get_response=None):%0A        self.get_response = get_response%0A%0A    def __call__(self, request):%0A        response = self.get_response(request)%0A        return response%0A%0A    def process_view(self, request, view, *args, **kwargs):%0A        import hotshot, hotshot.stats  # noqa%0A%0A        for item in request.META%5B'QUERY_STRING'%5D.split('&'):%0A            if item.split('=')%5B0%5D == 'profile':  # profile in query string%0A%0A                # catch the output, must happen before stats object is created%0A                # see https://bugs.launchpad.net/webpy/+bug/133080 for the details%0A                std_old, std_new = sys.stdout, StringIO.StringIO()%0A                sys.stdout = std_new%0A%0A                # now let's do some profiling%0A                tmpfile = '/tmp/%25s' %25 request.COOKIES%5B'sessionid'%5D%0A                prof = hotshot.Profile(tmpfile)%0A%0A                # make a call to the actual view function with the given arguments%0A                prof.runcall(view, request, *args%5B0%5D, **args%5B1%5D)%0A                prof.close()%0A%0A                # and then statistical reporting%0A                stats = hotshot.stats.load(tmpfile)%0A                stats.strip_dirs()%0A                stats.sort_stats('time')%0A%0A                # do the output%0A                stats.print_stats(1.0)%0A%0A                # restore default output%0A                sys.stdout = std_old%0A%0A                # delete file%0A                os.remove(tmpfile)%0A%0A                return HttpResponse('%3Cpre%3E%25s%3C/pre%3E' %25 std_new.getvalue())%0A%0A        return N
+from django.conf import settings%0Afrom django.utils import timez
 one%0A

Remove broken and unused middleware classes