<commit_before>from django import forms
from django.core.mail import send_mail
from css.models import CUser, Room, Course, SectionType, Schedule, Section, Availability, FacultyCoursePreferences
from django.http import HttpResponseRedirect
from settings import DEPARTMENT_SETTINGS, HOSTNAME
import re
from django.forms import ModelChoiceField

#  Login Form
class LoginForm(forms.Form):
    email = forms.EmailField()
    password = forms.CharField(label='Password', widget=forms.PasswordInput)

    #@TODO validate pass?
    @staticmethod
    def validate_password(password):
        if re.match(r'^(?=.*\d)(?=.*[A-Za-z])(?=.*[-._!@#$%^&*?+])[A-Za-z0-9-._!@#$%^&*?+]{8,32}$', password) is None:
            raise ValidationError("Attempted CUser creation with invalid password")

#  Invite Form
class InviteUserForm(forms.Form):
    email = forms.EmailField()
    first_name = forms.CharField()
    last_name = forms.CharField()

    def send_invite(self, usertype, request):
        first_name = self.cleaned_data['first_name']
        last_name = self.cleaned_data['last_name']
        name = first_name + ' ' + last_name
        email = self.cleaned_data['email']
        host = request.META['HTTP_HOST']
        if not re.search(r'http', host):
            host = 'http://' + host
        link = host + '/register?first_name=' + first_name +'&last_name=' + last_name +'&user_type='+ usertype + '&email=' + email
        send_mail('Invite to register for CSS', name + ", you have been invited to register for CSS. Please register using the following link:\n\n "
        + link, '<EMAIL>', [self.cleaned_data['email']])

# Registration Form
# @TODO on load, pull fields from query string -> show failure if field not able to be loaded:
#       Fields to pull: email, first_name, last_name, user_type
class RegisterUserForm(forms.Form):
    first_name = forms.CharField()
    last_name = forms.CharField()
    email = forms.EmailField()
    user_type = forms.CharField()
    password1 = forms.CharField(label='Password', widget=forms.PasswordInput)
    password2 = forms.CharField(label='Confirm Password', widget=forms.PasswordInput)

    def __init__(self, *args, **kwargs):
        if kwargs.pop('request') is "GET":
            self.first_name = kwargs.pop('first_name')
            self.last_name = kwargs.pop('last_name')
            self.user_type = kwargs.pop('user_type')
            self.email = kwargs.pop('email')
            self.declared_fields['first_name'].initial = self.first_name
            self.declared_fields['last_name'].initial = self.last_name
            self.declared_fields['email'].initial = self.email
            self.declared_fields['user_type'].initial = self.user_type
            self.declared_fields['user_type'].disabled = True

        super(RegisterUserForm, self).__init__(*args,**kwargs)

    def save(self):
	if CUser.objects.filter(user__email = self.cleaned_data['email']).exists():
	    user = CUser.get_user(email = self.cleaned_data['email'])
	    user.is_active = True
	else:
            user = CUser.create(email=self.cleaned_data['email'],
                                password=self.cleaned_data['<PASSWORD>'],
                                user_type=self.cleaned_data['user_type'],
                                first_name=self.cleaned_data['first_name'],
                                last_name=self.cleaned_data['last_name'])
        user.save()
        return user


# Edit User Form
class EditUserForm(forms.Form):
    user_email = forms.CharField(widget=forms.HiddenInput(), initial='<EMAIL>')
    first_name = forms.CharField()
    last_name = forms.CharField()
    password = forms.CharField()

    def save(self):
        user = CUser.get_user(email=self.cleaned_data['user_email'])
        user.set_first_name(self.cleaned_data['first_name'])
        user.set_last_name(self.cleaned_data['last_name'])
        user.set_password(self.cleaned_data['password'])
	user.save()
	return user

# Delete Form
class DeleteUserForm(forms.Form):
    email = forms.CharField(label='Confirm email')

    def delete_user(self):
        email = self.cleaned_data['email']
        CUser.get_user(email=self.cleaned_data['email']).delete()

class AddRoomForm(forms.Form):
    name = forms.CharField()
    description = forms.CharField()
    capacity = forms.IntegerField()
    notes = forms.CharField()
    equipment = forms.CharField()

    def save(self):
        room = Room.objects.create(name=self.cleaned_data['name'], description=self.cleaned_data['description'], capacity=self.cleaned_data['capacity'], notes=self.cleaned_data['notes'], equipment=self.cleaned_data['equipment'])
        room.save()
        return room

class EditRoomForm(forms.Form):
    name = forms.CharField(widget=forms.HiddenInput(), initial='defaultRoom')
    description = forms.CharField()<commit_msg>really got faculty deletion, duplicate registration bug<commit_after>from django import forms
from django.core.mail import send_mail
from css.models import CUser, Room, Course, SectionType, Schedule, Section, Availability, FacultyCoursePreferences
from django.http import HttpResponseRedirect
from settings import DEPARTMENT_SETTINGS, HOSTNAME
import re
from django.forms import ModelChoiceField
from django.contrib.auth.models import User

#  Login Form
class LoginForm(forms.Form):
    email = forms.EmailField()
    password = forms.CharField(label='Password', widget=forms.PasswordInput)

    #@TODO validate pass?
    @staticmethod
    def validate_password(password):
        if re.match(r'^(?=.*\d)(?=.*[A-Za-z])(?=.*[-._!@#$%^&*?+])[A-Za-z0-9-._!@#$%^&*?+]{8,32}$', password) is None:
            raise ValidationError("Attempted CUser creation with invalid password")

#  Invite Form
class InviteUserForm(forms.Form):
    email = forms.EmailField()
    first_name = forms.CharField()
    last_name = forms.CharField()

    def send_invite(self, usertype, request):
        first_name = self.cleaned_data['first_name']
        last_name = self.cleaned_data['last_name']
        name = first_name + ' ' + last_name
        email = self.cleaned_data['email']
        host = request.META['HTTP_HOST']
        if not re.search(r'http', host):
            host = 'http://' + host
        link = host + '/register?first_name=' + first_name +'&last_name=' + last_name +'&user_type='+ usertype + '&email=' + email
        send_mail('Invite to register for CSS', name + ", you have been invited to register for CSS. Please register using the following link:\n\n "
        + link, '<EMAIL>', [self.cleaned_data['email']])

# Registration Form
# @TODO on load, pull fields from query string -> show failure if field not able to be loaded:
#       Fields to pull: email, first_name, last_name, user_type
class RegisterUserForm(forms.Form):
    first_name = forms.CharField()
    last_name = forms.CharField()
    email = forms.EmailField()
    user_type = forms.CharField()
    password1 = forms.CharField(label='Password', widget=forms.PasswordInput)
    password2 = forms.CharField(label='<PASSWORD>', widget=forms.PasswordInput)

    def __init__(self, *args, **kwargs):
        if kwargs.pop('request') is "GET":
            self.first_name = kwargs.pop('first_name')
            self.last_name = kwargs.pop('last_name')
            self.user_type = kwargs.pop('user_type')
            self.email = kwargs.pop('email')
            self.declared_fields['first_name'].initial = self.first_name
            self.declared_fields['last_name'].initial = self.last_name
            self.declared_fields['email'].initial = self.email
            self.declared_fields['user_type'].initial = self.user_type
            self.declared_fields['user_type'].disabled = True

        super(RegisterUserForm, self).__init__(*args,**kwargs)

    def save(self):
        user = CUser.create(email=self.cleaned_data['email'],
                            password=self.cleaned_data['<PASSWORD>'],
                            user_type=self.cleaned_data['user_type'],
                            first_name=self.cleaned_data['first_name'],
                            last_name=self.cleaned_data['last_name'])
        user.save()
        return user


# Edit User Form
class EditUserForm(forms.Form):
    user_email = forms.CharField(widget=forms.HiddenInput(), initial='<EMAIL>')
    first_name = forms.CharField()
    last_name = forms.CharField()
    password = forms.CharField()

    def save(self):
        user = CUser.get_user(email=self.cleaned_data['user_email'])
        user.set_first_name(self.cleaned_data['first_name'])
        user.set_last_name(self.cleaned_data['last_name'])
        user.set_password(self.cleaned_data['password'])
	user.save()
	return user

# Delete Form
class DeleteUserForm(forms.Form):
    email = forms.CharField(label='Confirm email')

    def delete_user(self):
        email = self.cleaned_data['email']
        User.objects.filter(username = self.cleaned_data['email']).delete()

class AddRoomForm(forms.Form):
    name = forms.CharField()
    description = forms.CharField()
    capacity = forms.IntegerField()
    notes = forms.CharField()
    equipment = forms.CharField()

    def save(self):
        room = Room.objects.create(name=self.cleaned_data['name'], description=self.cleaned_data['description'], capacity=self.cleaned_data['capacity'], notes=self.cleaned_data['notes'], equipment=self.cleaned_data['equipment'])
        room.save()
        return room

class EditRoomForm(forms.Form):
    name = forms.CharField(widget=forms.HiddenInput(), initial='defaultRoom')
    description = forms.CharField()
{'fraction_non_alphanumeric': 0.1318428750522357, 'fraction_numerical': 0.0014625992478061011, 'mean_word_length': 3.4278445883441258, 'pattern_counts': {'":': 2, '<': 10, '<?xml version=': 0, '>': 12, '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': '3160826', 'n_tokens_mistral': 2893, 'n_tokens_neox': 2854, 'n_words': 566}