def write_audit_record(request, user, event, audit_updates=None, **kwargs): try: audit_record: types.AuditRecord = get_audit_login_dict( request, user, event) if audit_updates: audit_record.update(audit_updates) if audit_record: if AUDIT_TO_FILE: write_entry(audit_record) AuditEvent.objects.create(**audit_record) except Exception: logger.exception("user_logged_in signal failed to create audit event", exc_info=True)
def user_logged_in(sender, request, user, **kwargs): """ signal to catch logins and log them in the audit log """ try: event = 'login' d = get_audit_login_dict(request, user, event) if d: if AUDIT_TO_FILE: write_entry(d) login_event = AuditEvent( event=event, username=d['user_details']['username'], ip=d['user_details']['ip'], email=d['user_details']['email'], fullname=d['user_details']['fullname'], superuser=d['user_details']['superuser'], staff=d['user_details']['staff'], ) login_event.save() except: pass