# Path: easyaudit/middleware/easyaudit.py
# def get_current_request():
#     return getattr(_thread_locals, 'request', None)
# 
# Path: easyaudit/models.py
# class LoginEvent(models.Model):
#     LOGIN = 0
#     LOGOUT = 1
#     FAILED = 2
#     TYPES = (
#         (LOGIN, _('Login')),
#         (LOGOUT, _('Logout')),
#         (FAILED, _('Failed login')),
#     )
#     login_type = models.SmallIntegerField(choices=TYPES, verbose_name=_('Event type'))
#     username = models.CharField(max_length=255, null=True, blank=True, verbose_name=_('Username'))
#     user = models.ForeignKey(settings.AUTH_USER_MODEL, null=True, blank=True,
#                              on_delete=models.SET_NULL, db_constraint=False,
#                              verbose_name=_('User'))
#     remote_ip = models.CharField(max_length=50, null=True, db_index=True, verbose_name=_('Remote IP'))
#     datetime = models.DateTimeField(auto_now_add=True, verbose_name=_('Date time'))
# 
#     class Meta:
#         verbose_name = _('login event')
#         verbose_name_plural = _('login events')
#         ordering = ['-datetime']
# 
# Path: easyaudit/settings.py
# REMOTE_ADDR_HEADER = getattr(settings, 'DJANGO_EASY_AUDIT_REMOTE_ADDR_HEADER', 'REMOTE_ADDR')
# 
# WATCH_AUTH_EVENTS = getattr(settings, 'DJANGO_EASY_AUDIT_WATCH_AUTH_EVENTS', True)
# 
# LOGGING_BACKEND = getattr(settings, 'DJANGO_EASY_AUDIT_LOGGING_BACKEND', 'easyaudit.backends.ModelBackend')


from django.contrib.auth import signals, get_user_model
from django.db import transaction
from django.utils.module_loading import import_string
from easyaudit.middleware.easyaudit import get_current_request
from easyaudit.models import LoginEvent
from easyaudit.settings import REMOTE_ADDR_HEADER, WATCH_AUTH_EVENTS, LOGGING_BACKEND


audit_logger = import_string(LOGGING_BACKEND)()


def user_logged_in(sender, request, user, **kwargs):
    try:
        with transaction.atomic():
            login_event = audit_logger.login({
                'login_type': LoginEvent.LOGIN,
                'username': getattr(user, user.USERNAME_FIELD),
                'user_id': getattr(user, 'id', None),
                'remote_ip': request.META[REMOTE_ADDR_HEADER]
            })
    except:
        pass


def user_logged_out(sender, request, user, **kwargs):
    try:
        with transaction.atomic():
            login_event = audit_logger.login({

# Path: easyaudit/middleware/easyaudit.py
# def get_current_request():
#     return getattr(_thread_locals, 'request', None)
# 
# Path: easyaudit/models.py
# class LoginEvent(models.Model):
#     LOGIN = 0
#     LOGOUT = 1
#     FAILED = 2
#     TYPES = (
#         (LOGIN, _('Login')),
#         (LOGOUT, _('Logout')),
#         (FAILED, _('Failed login')),
#     )
#     login_type = models.SmallIntegerField(choices=TYPES, verbose_name=_('Event type'))
#     username = models.CharField(max_length=255, null=True, blank=True, verbose_name=_('Username'))
#     user = models.ForeignKey(settings.AUTH_USER_MODEL, null=True, blank=True,
#                              on_delete=models.SET_NULL, db_constraint=False,
#                              verbose_name=_('User'))
#     remote_ip = models.CharField(max_length=50, null=True, db_index=True, verbose_name=_('Remote IP'))
#     datetime = models.DateTimeField(auto_now_add=True, verbose_name=_('Date time'))
# 
#     class Meta:
#         verbose_name = _('login event')
#         verbose_name_plural = _('login events')
#         ordering = ['-datetime']
# 
# Path: easyaudit/settings.py
# REMOTE_ADDR_HEADER = getattr(settings, 'DJANGO_EASY_AUDIT_REMOTE_ADDR_HEADER', 'REMOTE_ADDR')
# 
# WATCH_AUTH_EVENTS = getattr(settings, 'DJANGO_EASY_AUDIT_WATCH_AUTH_EVENTS', True)
# 
# LOGGING_BACKEND = getattr(settings, 'DJANGO_EASY_AUDIT_LOGGING_BACKEND', 'easyaudit.backends.ModelBackend')


# Path: easyaudit/signals/auth_signals.py
from django.contrib.auth import signals, get_user_model
from django.db import transaction
from django.utils.module_loading import import_string
from easyaudit.middleware.easyaudit import get_current_request
from easyaudit.models import LoginEvent
from easyaudit.settings import REMOTE_ADDR_HEADER, WATCH_AUTH_EVENTS, LOGGING_BACKEND



audit_logger = import_string(LOGGING_BACKEND)()


def user_logged_in(sender, request, user, **kwargs):
    try:
        with transaction.atomic():
            login_event = audit_logger.login({
                'login_type': LoginEvent.LOGIN,
                'username': getattr(user, user.USERNAME_FIELD),
                'user_id': getattr(user, 'id', None),
                'remote_ip': request.META[REMOTE_ADDR_HEADER]
            })
    except:
        pass


def user_logged_out(sender, request, user, **kwargs):
    try:
        with transaction.atomic():
            login_event = audit_logger.login({

                'login_type': LoginEvent.LOGOUT,