<commit_before># Create your views here.
import django.core.exceptions
import settings
from accounts.forms import UserPasswordResetForm
from amcat.models import AmCAT
from amcat.models import ArticleSet
from amcat.models.authorisation import ROLE_PROJECT_READER
from amcat.tools.usage import log_request_usage
from django.contrib.auth import authenticate, login as auth_login, logout as auth_logout
from django.contrib.auth import signals
from django.contrib.auth.views import password_reset, password_reset_confirm
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect
from django.shortcuts import render, redirect
from navigator.forms import AddUserForm, AddUserFormWithPassword
from navigator.utils.auth import create_user


def _login(request, error, username, announcement):
    """
    Render shortcut
    """
    allow_anonymous = not settings.REQUIRE_LOGON
    allow_register = settings.ALLOW_REGISTER

    if allow_anonymous:
        
        featured_sets = [(aset, aset.project.get_role_id(user=request.user) >= ROLE_PROJECT_READER)
                         for aset in ArticleSet.objects.filter(featured=True)]
    
    return render(request, "accounts/login.html", locals())

def _redirect_login(request):
    """
    Redirect a successful login
    """
    next1 = request.GET.get("next")
    if next1 is not None:
        return redirect(next1)

    # Redirect to frontpage
    return redirect("navigator:index")


def login(request):
    """
    Try to log a user in. If method is GET, show empty login-form
    """
    if not request.user.is_authenticated() and request.user.is_active:
        # User already logged in
        return _redirect_login(request)

    system = AmCAT.get_instance()
    announcements = [system.server_warning, system.global_announcement]

    announcement = "<hr/>".join(a for a in announcements if a is not None)

    if request.method == "POST":
        username = request.POST.get("username")
        passwd = request.POST.get("password")

        user = authenticate(username=username, password=<PASSWORD>)

        if user is None or not user.is_active:
            # Credentials wrong or account disabled
            return _login(request, True, username, announcement)

        # Credentials OK, log user in
        auth_login(request, user)
        signals.user_logged_in.send(sender=user.__class__, request=request, user=user)

        log_request_usage(request, "account" ,"login")
        return _redirect_login(request)

    # GET request, send empty form
    return _login(request, False, None, announcement)

def logout(request):
    log_request_usage(request, "account" ,"logout")
    auth_logout(request)
    signals.user_logged_out.send(sender=request.user.__class__, request=request, user=request.user)

    return redirect(login)

def register(request):
    """
    Let the user fill in a registration form or process such a form.
    """

    if not settings.ALLOW_REGISTER:
        
        raise django.core.exceptions.PermissionDenied("Please use the admin dashboard to create users") 
        
    form_class = AddUserForm if settings.REGISTER_REQUIRE_VALIDATION else AddUserFormWithPassword
    form = form_class(request, data=request.POST or None)

    del form.fields['language']

    user = None
    if form.is_valid():
        user = create_user(
            form.cleaned_data['username'],
            form.cleaned_data['first_name'],
            form.cleaned_data['last_name'],
            form.cleaned_data['email'],
            password=form.cleaned_data.get('password'),
        )

        log_request_usage(request, "account" ,"register")
        if not settings.REGISTER_REQUIRE_VALIDATION:
            new_user = authenticate(username=user.username,
                                    password=form.cleaned_data['password'])
            auth_login(request, new_user)
            return HttpResponseRedirect("/")
        
    return render(request, "accounts/register.html", locals())
<commit_msg>Language is not a field on User anymore. Delete!<commit_after># Create your views here.
import django.core.exceptions
import settings
from accounts.forms import UserPasswordResetForm
from amcat.models import AmCAT
from amcat.models import ArticleSet
from amcat.models.authorisation import ROLE_PROJECT_READER
from amcat.tools.usage import log_request_usage
from django.contrib.auth import authenticate, login as auth_login, logout as auth_logout
from django.contrib.auth import signals
from django.contrib.auth.views import password_reset, password_reset_confirm
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect
from django.shortcuts import render, redirect
from navigator.forms import AddUserForm, AddUserFormWithPassword
from navigator.utils.auth import create_user


def _login(request, error, username, announcement):
    """
    Render shortcut
    """
    allow_anonymous = not settings.REQUIRE_LOGON
    allow_register = settings.ALLOW_REGISTER

    if allow_anonymous:
        
        featured_sets = [(aset, aset.project.get_role_id(user=request.user) >= ROLE_PROJECT_READER)
                         for aset in ArticleSet.objects.filter(featured=True)]
    
    return render(request, "accounts/login.html", locals())

def _redirect_login(request):
    """
    Redirect a successful login
    """
    next1 = request.GET.get("next")
    if next1 is not None:
        return redirect(next1)

    # Redirect to frontpage
    return redirect("navigator:index")


def login(request):
    """
    Try to log a user in. If method is GET, show empty login-form
    """
    if not request.user.is_authenticated() and request.user.is_active:
        # User already logged in
        return _redirect_login(request)

    system = AmCAT.get_instance()
    announcements = [system.server_warning, system.global_announcement]

    announcement = "<hr/>".join(a for a in announcements if a is not None)

    if request.method == "POST":
        username = request.POST.get("username")
        passwd = request.POST.get("password")

        user = authenticate(username=username, password=<PASSWORD>)

        if user is None or not user.is_active:
            # Credentials wrong or account disabled
            return _login(request, True, username, announcement)

        # Credentials OK, log user in
        auth_login(request, user)
        signals.user_logged_in.send(sender=user.__class__, request=request, user=user)

        log_request_usage(request, "account" ,"login")
        return _redirect_login(request)

    # GET request, send empty form
    return _login(request, False, None, announcement)

def logout(request):
    log_request_usage(request, "account" ,"logout")
    auth_logout(request)
    signals.user_logged_out.send(sender=request.user.__class__, request=request, user=request.user)

    return redirect(login)

def register(request):
    """
    Let the user fill in a registration form or process such a form.
    """

    if not settings.ALLOW_REGISTER:
        
        raise django.core.exceptions.PermissionDenied("Please use the admin dashboard to create users") 
        
    form_class = AddUserForm if settings.REGISTER_REQUIRE_VALIDATION else AddUserFormWithPassword
    form = form_class(request, data=request.POST or None)

    user = None
    if form.is_valid():
        user = create_user(
            form.cleaned_data['username'],
            form.cleaned_data['first_name'],
            form.cleaned_data['last_name'],
            form.cleaned_data['email'],
            password=form.cleaned_data.get('password'),
        )

        log_request_usage(request, "account" ,"register")
        if not settings.REGISTER_REQUIRE_VALIDATION:
            new_user = authenticate(username=user.username,
                                    password=form.cleaned_data['password'])
            auth_login(request, new_user)
            return HttpResponseRedirect("/")
        
    return render(request, "accounts/register.html", locals())

{'fraction_non_alphanumeric': 0.08729763387297634, 'fraction_numerical': 0.0007471980074719801, 'mean_word_length': 3.258218451749735, 'pattern_counts': {'":': 2, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3576566', 'n_tokens_mistral': 2255, 'n_tokens_neox': 2199, 'n_words': 584}