<commit_before># Create your views here.

from django.shortcuts import render
from django.http import HttpResponseRedirect
from django.forms import ModelForm
from django import forms
from scoring.models import Event
from django.contrib.auth.models import User, Group
from django.core.mail import send_mail

def reg_usr(request):
    if request.method == 'POST': # If the form has been submitted...
        form = UserRegForm(request.POST) # A form bound to the POST data
        if form.is_valid(): # All validation rules pass
            info = form.cleaned_data
            password = User.objects.make_random_password()
            new_user = User(username=info['username'],first_name=info['first_name'],last_name=info['last_name'],email=info['email'],is_staff=True)
            new_user.set_password(password)
            for event in info['events']:
                try:
                    grp = Group.objects.get(name=event.name)
                    new_user.groups.add(grp)
                except:
                    print 'err'
            message = 'Thank you '+new_user.first_name + ' ' + new_user.last_name + '\n'
            message = message + 'Your username is '+new_user.email + '\n'   
            message = message + 'Your temporary is '+password + '\n'    
            message = message + 'You may now login at http://kuexpo.cloudapp.net/admin/'+'\n'
            message = message + 'Please remember to change your password(top right corner when logged in)'      
            send_mail('EXPO Scoring System User Info', message, '<EMAIL>',(new_user.email,), fail_silently=False)
            new_user.save()
            return HttpResponseRedirect('/register/thanks/') # Redirect after POST
    else:
        data = {'subject': 'hello'}
        form = UserRegForm()

    return render(request, 'register/register.html', {
        'form': form,
    })
class UserRegForm(ModelForm):
    error_css_class='alert alert-error'

    events = forms.ModelMultipleChoiceField(queryset=Event.objects.all())
    class Meta:
        model = User
        fields = ('username', 'first_name','last_name','email',)
    
    def clean_email(self):
        try:
            
            user = User.objects.get(email=self.cleaned_data['email'])
        except:
           return self.cleaned_data['email'] 
        else:
           raise forms.ValidationError('Email Is Already Registered')
            
    

<commit_msg>Change email to username<commit_after># Create your views here.

from django.shortcuts import render
from django.http import HttpResponseRedirect
from django.forms import ModelForm
from django import forms
from scoring.models import Event
from django.contrib.auth.models import User, Group
from django.core.mail import send_mail

def reg_usr(request):
    if request.method == 'POST': # If the form has been submitted...
        form = UserRegForm(request.POST) # A form bound to the POST data
        if form.is_valid(): # All validation rules pass
            info = form.cleaned_data
            password = <PASSWORD>()
            new_user = User(username=info['username'],first_name=info['first_name'],last_name=info['last_name'],email=info['email'],is_staff=True)
            new_user.set_password(password)
            for event in info['events']:
                try:
                    grp = Group.objects.get(name=event.name)
                    new_user.groups.add(grp)
                except:
                    print 'err'
            message = 'Thank you '+new_user.first_name + ' ' + new_user.last_name + '\n'
            message = message + 'Your username is '+new_user.username + '\n'   
            message = message + 'Your temporary is '+password + '\n'    
            message = message + 'You may now login at http://kuexpo.cloudapp.net/admin/'+'\n'
            message = message + 'Please remember to change your password(top right corner when logged in)'      
            send_mail('EXPO Scoring System User Info', message, '<EMAIL>',(new_user.email,), fail_silently=False)
            new_user.save()
            return HttpResponseRedirect('/register/thanks/') # Redirect after POST
    else:
        data = {'subject': 'hello'}
        form = UserRegForm()

    return render(request, 'register/register.html', {
        'form': form,
    })
class UserRegForm(ModelForm):
    error_css_class='alert alert-error'

    events = forms.ModelMultipleChoiceField(queryset=Event.objects.all())
    class Meta:
        model = User
        fields = ('username', 'first_name','last_name','email',)
    
    def clean_email(self):
        try:
            
            user = User.objects.get(email=self.cleaned_data['email'])
        except:
           return self.cleaned_data['email'] 
        else:
           raise forms.ValidationError('Email Is Already Registered')
            
    


{'fraction_non_alphanumeric': 0.10924891371818746, 'fraction_numerical': 0.0, 'mean_word_length': 2.409026798307475, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, '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': '2530181', 'n_tokens_mistral': 1326, 'n_tokens_neox': 1296, 'n_words': 345}