<commit_before>import sys
import logging
from django.contrib.auth import authenticate
from wsgidav.wsgidav_app import DEFAULT_CONFIG
from wsgidav.wsgidav_app import WsgiDAVApp
from django.views import debug
from .settings import get_resource_access_model, import_from_path, \
    DEBUG, DUMP_REPORT_STRATEGY, DUMP_EXCEPTIONS, DAV_ROOT, PROVIDER_NAME
from .models import ResourceAccess
from .locals import thread_locals

logger = logging.getLogger(__name__)


class TemplationDomainController(object):
    def requireAuthentication(self, realmname, environ):
        return True

    def getDomainRealm(self, inputRelativeURL, environ):
        return environ.get('PATH_INFO').split('/')[1]

    def authDomainUser(self, realmname, username, password, environ):
        user = authenticate(username=username, password=password)
        try:
            return ResourceAccess.objects.get(user=user, resource__id=realmname)
        except ResourceAccess.DoesNotExist:
            return False

config = DEFAULT_CONFIG.copy()
config.update({
    'provider_mapping': {PROVIDER_NAME: DAV_ROOT},
    'user_mapping': {DAV_ROOT: {None: None}},
    'domaincontroller': TemplationDomainController(),
    'acceptdigest': False,
    'defaultdigest': False,
})

wsgidav_app = WsgiDAVApp(config)


class WsgiDAVMiddleware(object):
    def __init__(self, django_app):
        self.django_app = django_app

    def __call__(self, environ, start_response):
        if environ.get('PATH_INFO').startswith('/' + PROVIDER_NAME):
            return wsgidav_app(environ, start_response)
        return self.django_app(environ, start_response)


def dump_report_strategy(request):
    """
    Default Strategy to show errors only if django authenticated user is on
    templation Resource Model. You can customize this behavior with
    `settings.TEMPLATION_DUMP_REPORT_STRATEGY` and point to a custom function
    like:

    def customReportStrategy(request):
        return True
    """
    return bool(get_resource_access_model().objects.filter(user=request.user))


class TemplationMiddleware(object):
    def __init__(self):
        self.strategy = import_from_path(DUMP_REPORT_STRATEGY)

    def process_request(self, request):
        thread_locals.user = request.user

    def process_response(self, request, response):
        thread_locals.clear()
        return response

    def process_exception(self, request, exception):
        thread_locals.clear()
        if DEBUG or exception in DUMP_EXCEPTIONS and self.strategy(request):
            exc_info = sys.exc_info()
            exc_info.update({
                # remove the following info.
            })
            return debug.technical_500_response(request, *exc_info)<commit_msg>fix AttribureError (request.user isn't defined allways)<commit_after>import sys
import logging
from django.contrib.auth import authenticate
from wsgidav.wsgidav_app import DEFAULT_CONFIG
from wsgidav.wsgidav_app import WsgiDAVApp
from django.views import debug
from .settings import get_resource_access_model, import_from_path, \
    DEBUG, DUMP_REPORT_STRATEGY, DUMP_EXCEPTIONS, DAV_ROOT, PROVIDER_NAME
from .models import ResourceAccess
from .locals import thread_locals

logger = logging.getLogger(__name__)


class TemplationDomainController(object):
    def requireAuthentication(self, realmname, environ):
        return True

    def getDomainRealm(self, inputRelativeURL, environ):
        return environ.get('PATH_INFO').split('/')[1]

    def authDomainUser(self, realmname, username, password, environ):
        user = authenticate(username=username, password=password)
        try:
            return ResourceAccess.objects.get(user=user, resource__id=realmname)
        except ResourceAccess.DoesNotExist:
            return False

config = DEFAULT_CONFIG.copy()
config.update({
    'provider_mapping': {PROVIDER_NAME: DAV_ROOT},
    'user_mapping': {DAV_ROOT: {None: None}},
    'domaincontroller': TemplationDomainController(),
    'acceptdigest': False,
    'defaultdigest': False,
})

wsgidav_app = WsgiDAVApp(config)


class WsgiDAVMiddleware(object):
    def __init__(self, django_app):
        self.django_app = django_app

    def __call__(self, environ, start_response):
        if environ.get('PATH_INFO').startswith('/' + PROVIDER_NAME):
            return wsgidav_app(environ, start_response)
        return self.django_app(environ, start_response)


def dump_report_strategy(request):
    """
    Default Strategy to show errors only if django authenticated user is on
    templation Resource Model. You can customize this behavior with
    `settings.TEMPLATION_DUMP_REPORT_STRATEGY` and point to a custom function
    like:

    def customReportStrategy(request):
        return True
    """
    return bool(get_resource_access_model().objects.filter(user=request.user))


class TemplationMiddleware(object):
    def __init__(self):
        self.strategy = import_from_path(DUMP_REPORT_STRATEGY)

    def process_request(self, request):
        thread_locals.user = getattr(request, 'user', None)

    def process_response(self, request, response):
        thread_locals.clear()
        return response

    def process_exception(self, request, exception):
        thread_locals.clear()
        if DEBUG or exception in DUMP_EXCEPTIONS and self.strategy(request):
            exc_info = sys.exc_info()
            exc_info.update({
                # remove the following info.
            })
            return debug.technical_500_response(request, *exc_info)
{'fraction_non_alphanumeric': 0.08436139332365747, 'fraction_numerical': 0.001451378809869376, 'mean_word_length': 3.81064572425829, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '206381', 'n_tokens_mistral': 1633, 'n_tokens_neox': 1575, 'n_words': 380}