<commit_before>from django.http import (HttpResponseForbidden, HttpResponseNotAllowed,
                         HttpResponseRedirect)

import commonware.log
from funfactory.urlresolvers import reverse

from mozillians.urls import error_page


log = commonware.log.getLogger('m.phonebook')


class PermissionDeniedMiddleware(object):
    """Add a generic 40x "not allowed" handler.

    TODO: Currently uses the 500.html error template, but in the future should
    display a more tailored-to-the-actual-error "not allowed" page."""
    def process_response(self, request, response):
        if isinstance(response, (HttpResponseForbidden,
                                 HttpResponseNotAllowed)):
            if request.user.is_authenticated():
                log.debug('Permission denied middleware, user was '
                         'authenticated, sending 500')
                return error_page(request, 500, status=response.status_code)
            else:
                if isinstance(response, (HttpResponseForbidden)):
                    log.debug('Response was forbidden')
                elif isinstance(response, (HttpResponseNotAllowed)):
                    log.debug('Response was not allowed')
                log.debug('Permission denied middleware, redirecting home')
                return HttpResponseRedirect(reverse('home'))
        return response
<commit_msg>Fix middleware to not hardcode mozillians<commit_after>import os

from django.http import (HttpResponseForbidden, HttpResponseNotAllowed,
                         HttpResponseRedirect)

import commonware.log
from funfactory.manage import ROOT
from funfactory.urlresolvers import reverse

# TODO: this is hackish. Once we update mozillians to the newest playdoh layout
error_page = __import__(os.path.basename(ROOT)).urls.error_page
log = commonware.log.getLogger('m.phonebook')


class PermissionDeniedMiddleware(object):
    """Add a generic 40x "not allowed" handler.

    TODO: Currently uses the 500.html error template, but in the future should
    display a more tailored-to-the-actual-error "not allowed" page."""
    def process_response(self, request, response):
        if isinstance(response, (HttpResponseForbidden,
                                 HttpResponseNotAllowed)):
            if request.user.is_authenticated():
                log.debug('Permission denied middleware, user was '
                         'authenticated, sending 500')
                return error_page(request, 500, status=response.status_code)
            else:
                if isinstance(response, (HttpResponseForbidden)):
                    log.debug('Response was forbidden')
                elif isinstance(response, (HttpResponseNotAllowed)):
                    log.debug('Response was not allowed')
                log.debug('Permission denied middleware, redirecting home')
                return HttpResponseRedirect(reverse('home'))
        return response

{'fraction_non_alphanumeric': 0.07731434384537131, 'fraction_numerical': 0.007460155985079688, 'mean_word_length': 2.5931790499390988, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3801225', 'n_tokens_mistral': 734, 'n_tokens_neox': 693, 'n_words': 204}