forms.py 2.07 KB
Newer Older
1
2
3
4
5
6
7
8
# standard library imports
from __future__ import absolute_import, print_function
# core django imports
from django import forms
from django.core.exceptions import ValidationError
# third party imports
from multiselectfield import MultiSelectFormField
# imports from your apps
Daniel W Bond's avatar
pep8 me    
Daniel W Bond committed
9
from accounts.models import Student
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
from accounts.forms import SelectRoomField, BooleanRadioField
from housing.models import Room


class WelcomeNameForm(forms.Form):

    first_name = forms.CharField(required=False)
    last_name = forms.CharField(required=False)
    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)
        if self.instance.recent_changes() > 2:
            self.fields['room'].widget = forms.widgets.HiddenInput()
        else:
            self.fields['room'] = SelectRoomField(queryset=Room.objects.all(), required=False)

    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
38
            # print(students_in_room)
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
            # 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):

    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', )