<commit_before>from django.core.exceptions import ValidationError, PermissionDenied
from django.db import transaction
from django.shortcuts import render, redirect
from django.contrib.auth import authenticate, login as auth_login, logout
from django.contrib.auth.decorators import login_required
from .forms import ChangeUserInfoForm, AddWhiteListForm
from .models import IUser
from django.core.urlresolvers import reverse
from django.contrib import messages
from utils.text import random_string_generator
from django.contrib.auth.views import (
    password_reset_confirm,
    password_reset,
)
from utils.kobra import get_user_by_liu_id, LiuGetterError, LiuNotFoundError


def logout_view(request):
    logout(request)
    return redirect('/')  # TODO: Where should this redirect?

def login(request):
    if request.method == 'POST':
        username = request.POST['username']
        password = request.POST['password']
        user = authenticate(username=username.lower(), password=password)
        if user is not None:
            # the password verified for the user
            if user.is_active:
                not_member = False
                if user.last_login is None:
                    not_member = True
                auth_login(request, user)
                if not_member:
                    user.is_active = False
                    user.save()
                    return render(request, "user_managements/membership.html")
                try:
                    return redirect(request.GET['next'])
                except:
                    return redirect('/')
            else:
                messages.error(request, "Lösenordet är korrekt, men kontot är avstängt! Om detta inte bör vara fallet var god kontakta <EMAIL>")
                return render(request, "user_managements/login.html")
                # The password is valid, but the account has been disabled!
        else:
            # the authentication system was unable to verify the username and password
            messages.error(request, "Fel Liu-id eller lösenord.")
            return render(request, "user_managements/login.html")<commit_msg>Added Kobra call on first login<commit_after>from django.core.exceptions import ValidationError, PermissionDenied
from django.db import transaction
from django.shortcuts import render, redirect
from django.contrib.auth import authenticate, login as auth_login, logout
from django.contrib.auth.decorators import login_required
from .forms import ChangeUserInfoForm, AddWhiteListForm
from .models import IUser
from django.core.urlresolvers import reverse
from django.contrib import messages
from utils.text import random_string_generator
from django.contrib.auth.views import (
    password_reset_confirm,
    password_reset,
)
from utils.kobra import get_user_by_liu_id, LiuGetterError, LiuNotFoundError


def logout_view(request):
    logout(request)
    return redirect('/')  # TODO: Where should this redirect?

def login(request):
    if request.method == 'POST':
        username = request.POST['username']
        password = request.POST['password']
        user = authenticate(username=username.lower(), password=password)
        if user is not None:
            # the password verified for the user
            if user.is_active:
                not_member = False
                if user.last_login is None:
                    not_member = True
                auth_login(request, user)
                if not_member:
                    user.is_active = False
                    try:
                        kobra_dict = get_user_by_liu_id(user.username)
                        user.email = kobra_dict['email'].lower()
                        user.last_name = kobra_dict['last_name'].lower()
                        user.first_name = kobra_dict['first_name'].lower()
                        user.rfid_number = kobra_dict['rfid_number']
                        user.p_nr = kobra_dict['personal_number']
                        user.save()
                    except:
                        pass
                    user.save()
                    return render(request, "user_managements/membership.html")
                try:
                    return redirect(request.GET['next'])
                except:
                    return redirect('/')
            else:
                messages.error(request, "Lösenordet är korrekt, men kontot är avstängt! Om detta inte bör vara fallet var god kontakta <EMAIL>")
                return render(request, "user_managements/login.html")
                # The password is valid, but the account has been disabled!
        else:
            # the authentication system was unable to verify the username and password
            messages.error(request, "Fel Liu-id eller lösenord.")
            return render(request, "user_managements/login.html")
{'fraction_non_alphanumeric': 0.07256235827664399, 'fraction_numerical': 0.0, 'mean_word_length': 2.208994708994709, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '5893033', 'n_tokens_mistral': 1258, 'n_tokens_neox': 1200, 'n_words': 358}