<commit_before>from __future__ import absolute_import
from django.views.generic import CreateView
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse
from django.forms.models import modelform_factory
from django.contrib.auth import login, authenticate, get_user_model
from django.utils.translation import gettext_lazy as _
from django.utils.encoding import force_text

from .. import settings as djangui_settings
from ..django_compat import JsonResponse

class DjanguiRegister(CreateView):
    template_name = 'djangui/registration/register.html'
    model = get_user_model()
    fields = ('username', 'email', 'password')

    def dispatch(self, request, *args, **kwargs):
        if djangui_settings.DJANGUI_AUTH is False:
            return HttpResponseRedirect(djangui_settings.DJANGUI_REGISTER_URL)
        return super(DjanguiRegister, self).dispatch(request, *args, **kwargs)

    def post(self, request, *args, **kwargs):
        self.object = None
        form = self.get_form_class()
        post = request.POST.copy()
        post['username'] = post['username'].lower()
        form = form(post)
        if request.POST['password'] != request.POST['<PASSWORD>']:
            form.add_error('password', _('Passwords do not match.'))
        if request.POST['username'].lower() == 'admin':
            form.add_error('username', _('Reserved username.'))
        if not request.POST['email']:
            form.add_error('email', _('Please enter your email address.'))
        if form.is_valid():
            return self.form_valid(form)
        else:
            return self.form_invalid(form)

    def get_success_url(self):
        next_url = self.request.POST.get('next')
        # for some bizarre reason the password isn't setting by the modelform
        self.object.set_password(self.request.POST['password'])
        self.object.save()
        auser = authenticate(username=self.object.username, password=self.request.POST['password'])
        login(self.request, auser)
        return reverse(next_url) if next_url else reverse('djangui_home')

def djangui_login(request):
    if djangui_settings.DJANGUI_AUTH is False:
        return HttpResponseRedirect(djangui_settings.DJANGUI_LOGIN_URL)
    User = get_user_model()
    form = modelform_factory(User, fields=('username', 'password'))
    user = User.objects.filter(username=request.POST.get('username'))
    if user:
        user = user[0]
    else:
        user = None
    form = form(request.POST, instance=user)
    if form.is_valid():
        data = form.cleaned_data
        user = authenticate(username=data['username'], password=data['password'])
        if user is None:
            pass
        return JsonResponse({'valid': False, 'errors': {'__all__': [force_text(_('You have entered an invalid username or password.'))]}})
        login(request, user)
        return JsonResponse({'valid': True, 'redirect': request.POST['next']})
    else:
        return JsonResponse({'valid': False, 'errors': form.errors})<commit_msg>authentication bug fix (code left in from testing jsonresponse)<commit_after>from __future__ import absolute_import
from django.views.generic import CreateView
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse
from django.forms.models import modelform_factory
from django.contrib.auth import login, authenticate, get_user_model
from django.utils.translation import gettext_lazy as _
from django.utils.encoding import force_text

from .. import settings as djangui_settings
from ..django_compat import JsonResponse

class DjanguiRegister(CreateView):
    template_name = 'djangui/registration/register.html'
    model = get_user_model()
    fields = ('username', 'email', 'password')

    def dispatch(self, request, *args, **kwargs):
        if djangui_settings.DJANGUI_AUTH is False:
            return HttpResponseRedirect(djangui_settings.DJANGUI_REGISTER_URL)
        return super(DjanguiRegister, self).dispatch(request, *args, **kwargs)

    def post(self, request, *args, **kwargs):
        self.object = None
        form = self.get_form_class()
        post = request.POST.copy()
        post['username'] = post['username'].lower()
        form = form(post)
        if request.POST['password'] != request.POST['password2']:
            form.add_error('password', _('Passwords do not match.'))
        if request.POST['username'].lower() == 'admin':
            form.add_error('username', _('Reserved username.'))
        if not request.POST['email']:
            form.add_error('email', _('Please enter your email address.'))
        if form.is_valid():
            return self.form_valid(form)
        else:
            return self.form_invalid(form)

    def get_success_url(self):
        next_url = self.request.POST.get('next')
        # for some bizarre reason the password isn't setting by the modelform
        self.object.set_password(self.request.POST['password'])
        self.object.save()
        auser = authenticate(username=self.object.username, password=self.request.POST['password'])
        login(self.request, auser)
        return reverse(next_url) if next_url else reverse('djangui_home')

def djangui_login(request):
    if djangui_settings.DJANGUI_AUTH is False:
        return HttpResponseRedirect(djangui_settings.DJANGUI_LOGIN_URL)
    User = get_user_model()
    form = modelform_factory(User, fields=('username', 'password'))
    user = User.objects.filter(username=request.POST.get('username'))
    if user:
        user = user[0]
    else:
        user = None
    form = form(request.POST, instance=user)
    if form.is_valid():
        data = form.cleaned_data
        user = authenticate(username=data['username'], password=data['password'])
        if user is None:
            return JsonResponse({'valid': False, 'errors': {'__all__': [force_text(_('You have entered an invalid username or password.'))]}})
        login(request, user)
        return JsonResponse({'valid': True, 'redirect': request.POST['next']})
    else:
        return JsonResponse({'valid': False, 'errors': form.errors})
{'fraction_non_alphanumeric': 0.1173202614379085, 'fraction_numerical': 0.0004901960784313725, 'mean_word_length': 3.507363770250368, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, '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': '4915171', 'n_tokens_mistral': 1762, 'n_tokens_neox': 1717, 'n_words': 420}