<commit_before>from __future__ import division, print_function, absolute_import

from django.conf import settings
from django.core.urlresolvers import resolve

import bugsnag


def add_django_request_to_notification(notification):
    if not hasattr(notification.request_config, "django_request"):
        return

    request = notification.request_config.django_request

    if notification.context is None:
        route = resolve(request.path_info)
        if route:
            notification.context = route.url_name
        else:
            notification.context = "%s %s" % (request.method,
                                              request.path_info)

    if hasattr(request, 'user') and request.user.is_authenticated():
        try:
            name = " ".join([request.user.first_name or '',
                             request.user.last_name or ''])
            notification.set_user(id=request.user.username,
                                  email=request.user.email, name=name)
        except Exception as e:
            bugsnag.warn("could not get user data: %s" % e)
    else:
        notification.set_user(id=request.META['REMOTE_ADDR'])

    notification.add_tab("session", dict(request.session))
    notification.add_tab("request", {
        'path': request.path,
        'encoding': request.encoding,
        'GET': dict(request.GET),
        'POST': dict(request.POST),
        'url': request.build_absolute_uri(),
    })
    notification.add_tab("environment", dict(request.META))


def configure():
    # default to development if in DEBUG mode
    if getattr(settings, 'DEBUG'):
        bugsnag.configure(release_stage='development')

    # Import Bugsnag settings from settings.py
    django_bugsnag_settings = getattr(settings, 'BUGSNAG', {})
    bugsnag.configure(**django_bugsnag_settings)

    bugsnag.before_notify(add_django_request_to_notification)<commit_msg>Fix user attributes for custom auth<commit_after>from __future__ import division, print_function, absolute_import

import six
from django.conf import settings
from django.core.urlresolvers import resolve

import bugsnag


def add_django_request_to_notification(notification):
    if not hasattr(notification.request_config, "django_request"):
        return

    request = notification.request_config.django_request

    if notification.context is None:
        route = resolve(request.path_info)
        if route:
            notification.context = route.url_name
        else:
            notification.context = "%s %s" % (request.method,
                                              request.path_info)

    if hasattr(request, 'user') and request.user.is_authenticated():
        try:
            name = request.user.get_full_name()
            email = getattr(request.user, 'email', None)
            username = six.text_type(request.user.get_username())
            notification.set_user(id=username, email=email, name=name)
        except Exception as e:
            bugsnag.warn("could not get user data: %s" % e)
    else:
        notification.set_user(id=request.META['REMOTE_ADDR'])

    notification.add_tab("session", dict(request.session))
    notification.add_tab("request", {
        'path': request.path,
        'encoding': request.encoding,
        'GET': dict(request.GET),
        'POST': dict(request.POST),
        'url': request.build_absolute_uri(),
    })
    notification.add_tab("environment", dict(request.META))


def configure():
    # default to development if in DEBUG mode
    if getattr(settings, 'DEBUG'):
        bugsnag.configure(release_stage='development')

    # Import Bugsnag settings from settings.py
    django_bugsnag_settings = getattr(settings, 'BUGSNAG', {})
    bugsnag.configure(**django_bugsnag_settings)

    bugsnag.before_notify(add_django_request_to_notification)
{'fraction_non_alphanumeric': 0.10225485055060304, 'fraction_numerical': 0.0, 'mean_word_length': 2.877032520325203, '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': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1973495', 'n_tokens_mistral': 1059, 'n_tokens_neox': 1036, 'n_words': 221}