forms.py 2.55 KB
Newer Older
1 2 3 4 5
# standard library imports
from __future__ import absolute_import, print_function
# core django imports
from django import forms
from django.core.exceptions import ValidationError
Daniel W Bond's avatar
Daniel W Bond committed
6
from django.utils.translation import ugettext as _
7 8 9
# third party imports
from multiselectfield import MultiSelectFormField
# imports from your apps
Daniel W Bond's avatar
pep8 me  
Daniel W Bond committed
10
from accounts.models import Student
11 12 13 14 15 16
from accounts.forms import SelectRoomField, BooleanRadioField
from housing.models import Room


class WelcomeNameForm(forms.Form):

17
    first_name = forms.CharField(required=False, max_length=30)
18
    first_name.widget.attrs['class'] = 'form-control'
19
    last_name = forms.CharField(required=False, max_length=30)
20
    last_name.widget.attrs['class'] = 'form-control'
21 22 23 24 25 26 27 28
    gender = MultiSelectFormField(choices=Student.GENDER_CHOICES, required=False)
    show_gender = BooleanRadioField()


class WelcomePrivacyForm(forms.ModelForm):

    def __init__(self, *args, **kwargs):
        super(WelcomePrivacyForm, self).__init__(*args, **kwargs)
29
        self.fields['privacy'].widget.attrs['class'] = 'form-control'
30 31 32
        if self.instance.recent_changes() > 2:
            self.fields['room'].widget = forms.widgets.HiddenInput()
        else:
Daniel W Bond's avatar
Daniel W Bond committed
33 34
            self.fields['room'] = SelectRoomField(queryset=Room.objects.all(),
                                                  required=False)
35 36 37 38 39 40 41 42

    on_campus = BooleanRadioField()

    def clean(self):
        cleaned_data = super(WelcomePrivacyForm, self).clean()
        form_room = cleaned_data.get('room')
        if not(form_room is None):
            students_in_room = Student.objects.filter(room=form_room).count()
Daniel W Bond's avatar
pep8 me  
Daniel W Bond committed
43
            # print(students_in_room)
44 45 46 47 48 49 50 51 52 53 54
            # like in bookshare, I have no idea why the form errors don't display.
            if students_in_room > 12:
                raise ValidationError(_('Too many students in room (%d).' % students_in_room), code='invalid')

    class Meta:
        model = Student
        fields = ('room', 'privacy', 'on_campus')


class WelcomeMajorForm(forms.ModelForm):

55 56 57 58
    def __init__(self, *args, **kwargs):
        super(WelcomeMajorForm, self).__init__(*args, **kwargs)
        self.fields['graduating_year'].widget.attrs['class'] = 'form-control'

59 60 61 62 63 64 65 66 67 68 69 70 71 72
    class Meta:
        model = Student
        fields = ('major', 'graduating_year', )


class WelcomeSocialForm(forms.ModelForm):

    def __init__(self, *args, **kwargs):
        super(WelcomeSocialForm, self).__init__(*args, **kwargs)
        self.fields['completedSocial'].widget = forms.widgets.HiddenInput()

    class Meta:
        model = Student
        fields = ('completedSocial', )