<commit_before>from django.db.models import Q
from django.conf import settings
from django.template import RequestContext
from django.contrib.auth.models import User
from django.contrib.auth.decorators import login_required
from django.shortcuts import redirect, render_to_response
from django.contrib.auth import logout, login, authenticate
from social.backends.utils import load_backends

from psa.utils import render_to
from psa.models import SecondaryEmail


def context(**extra):
    """Adding default context to rendered page"""
    return dict({
        'available_backends': load_backends(settings.AUTHENTICATION_BACKENDS),
    }, **extra)


@render_to('psa/custom_login.html')
def validation_sent(request):
    """View to handle validation_send action"""
    user = request.user
    social_propose = False
    by_secondary = []
    email = request.session.get('email_validation_address')
    if user and user.is_anonymous():
        by_secondary = [i.provider.provider for i in
                        SecondaryEmail.objects.filter(email=email)
                        if not i.provider.provider == u'email']
        if by_secondary:
            social_propose = True

        users_by_email = User.objects.filter(email=email)
        for user_by_email in users_by_email:
            by_primary = [i.provider for i in
                          user_by_email.social_auth.all()
                          if not i.provider == u'email' and
                          not SecondaryEmail.objects.filter(
                              ~Q(email=email), provider=i, user=user_by_email
                          ).exists()]
            by_secondary.extend(by_primary)
            social_propose = True

    return context(
        validation_sent=True,
        email=email,
        social_propose=social_propose,
        social_list=by_secondary
    )


def custom_login(request):
    """Custom login to integrate social auth and default login"""
    username = password = ''
    logout(request)
    if request.POST:
        username = request.POST['username']
        password = request.POST['password']

        user = authenticate(username=username, password=password)
        if user is not None:
            if user.is_active:
                login(request, user)
                return redirect('/ct/')
    return render_to_response('psa/custom_login.html',
                              context_instance=RequestContext(request,
                                                              {
                                                                  'available_backends': load_backends(
                                                                      settings.AUTHENTICATION_BACKENDS),
                                                              }))

<commit_msg>made custom_login() redirect to next URL if provided per issue #46<commit_after>from django.db.models import Q
from django.conf import settings
from django.template import RequestContext
from django.contrib.auth.models import User
from django.contrib.auth.decorators import login_required
from django.shortcuts import redirect, render_to_response
from django.contrib.auth import logout, login, authenticate
from social.backends.utils import load_backends

from psa.utils import render_to
from psa.models import SecondaryEmail


def context(**extra):
    """Adding default context to rendered page"""
    return dict({
        'available_backends': load_backends(settings.AUTHENTICATION_BACKENDS),
    }, **extra)


@render_to('psa/custom_login.html')
def validation_sent(request):
    """View to handle validation_send action"""
    user = request.user
    social_propose = False
    by_secondary = []
    email = request.session.get('email_validation_address')
    if user and user.is_anonymous():
        by_secondary = [i.provider.provider for i in
                        SecondaryEmail.objects.filter(email=email)
                        if not i.provider.provider == u'email']
        if by_secondary:
            social_propose = True

        users_by_email = User.objects.filter(email=email)
        for user_by_email in users_by_email:
            by_primary = [i.provider for i in
                          user_by_email.social_auth.all()
                          if not i.provider == u'email' and
                          not SecondaryEmail.objects.filter(
                              ~Q(email=email), provider=i, user=user_by_email
                          ).exists()]
            by_secondary.extend(by_primary)
            social_propose = True

    return context(
        validation_sent=True,
        email=email,
        social_propose=social_propose,
        social_list=by_secondary
    )


def custom_login(request):
    """Custom login to integrate social auth and default login"""
    username = password = ''
    logout(request)
    if request.POST:
        username = request.POST['username']
        password = request.POST['password']

        user = authenticate(username=username, password=password)
        if user is not None:
            if user.is_active:
                login(request, user)
                return redirect(request.POST.get('next', '/ct/'))
    return render_to_response('psa/custom_login.html',
                              context_instance=RequestContext(request,
                                                              {
                                                                  'available_backends': load_backends(
                                                                      settings.AUTHENTICATION_BACKENDS),
                                                              }))


{'fraction_non_alphanumeric': 0.0775633079511245, 'fraction_numerical': 0.0003541703559412077, 'mean_word_length': 1.875763747454175, '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': 20, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2805556', 'n_tokens_mistral': 1377, 'n_tokens_neox': 1340, 'n_words': 333}