from django.conf import settings
from django.core.urlresolvers import reverse
from django.template.loader import render_to_string
from django.utils.encoding import smart_unicode

_HTML_TYPES = ('text/html', 'application/xhtml+xml')


def replace_insensitive(string, target, replacement):
    """
    Similar to string.replace() but is case insensitive code borrowed
    from Django Debug Toolbar.
    """
    no_case = string.lower()
    index = no_case.index(target.lower())
    return string[:index] + replacement + string[index + len(target):]


class DjohnoMiddleware(object):
    tag = '</body>'

    def get_djohno(self):
        return render_to_string('djohno/_djohno_dose.html',
                                {'STATIC_URL': settings.STATIC_URL})

    def should_process_response(self, request, response):
        if not request.user.is_superuser:
            return False

        if response.streaming:
            return False

        if not request.path.startswith(reverse('djohno_index')):
            return False

        if 'gzip' in response.get('Content-Encoding', ''):
            return False

        content_type = response.get('Content-Type',
                                    '').split(';')[0]

        if content_type not in _HTML_TYPES:
            return False

        return True

    def process_response(self, request, response):
        if not self.should_process_response(request, response):
            return response

        response.content = replace_insensitive(
            smart_unicode(response.content),
            self.tag,
            smart_unicode(self.get_djohno() + self.tag)
        )

        if 'Content-Length' in response:
            response['Content-Length'] = len(response.content)

        return response

from django.conf import settings
from django.core.urlresolvers import reverse
from django.template.loader import render_to_string
from django.utils.encoding import smart_unicode

_HTML_TYPES = ('text/html', 'application/xhtml+xml')


def replace_insensitive(string, target, replacement):
    """
    Similar to string.replace() but is case insensitive code borrowed
    from Django Debug Toolbar.
    """
    no_case = string.lower()
    index = no_case.index(target.lower())
    return string[:index] + replacement + string[index + len(target):]


class DjohnoMiddleware(object):
    def get_djohno(self):
        return render_to_string('djohno/_djohno_dose.html',
                                {'STATIC_URL': settings.STATIC_URL})

    def should_process_response(self, request, response):
        if not request.user.is_superuser:
            return False

        if response.streaming:
            return False

        if not request.path.startswith(reverse('djohno_index')):
            return False

        if 'gzip' in response.get('Content-Encoding', ''):
            return False

        content_type = response.get('Content-Type',
                                    '').split(';')[0]

        if content_type not in _HTML_TYPES:
            return False

        return True

    def process_response(self, request, response):
        if not self.should_process_response(request, response):
            return response

        tag = '</body>'

        response.content = replace_insensitive(
            smart_unicode(response.content),
            tag,
            smart_unicode(self.get_djohno() + tag)
        )

        if 'Content-Length' in response:
            response['Content-Length'] = len(response.content)

        return response

tag doesn't need to be a member of DjohnoMiddleware