forms.py 2.78 KB
Newer Older
1
2
# standard library imports
from __future__ import absolute_import, print_function
3
# core django imports
4
from django import forms
5
6
from django.utils.safestring import mark_safe
from django.template.loader import render_to_string
7
# third party imports
8
from crispy_forms.helper import FormHelper
Daniel W Bond's avatar
Daniel W Bond committed
9
10
from crispy_forms.layout import Submit, Layout
from crispy_forms.bootstrap import PrependedText, AppendedText
Daniel W Bond's avatar
Daniel W Bond committed
11
from multiselectfield import MultiSelectFormField
12
# imports from your apps
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
from .models import Student, Major
from housing.models import Building, Floor, Room


class SelectRoomWidget(forms.widgets.Select):

    template_name = 'room_select_widget.html'

    def __init__(self, rooms=Room.objects.all(), floors=Floor.objects.all(),
                 buildings=Building.objects.all(), neighborhoods=Building.NEIGHBOURHOOD_CHOICES):
        # should probably type check the other fields too
        if not all(isinstance(thing, Room) for thing in rooms):
            raise TypeError("Rooms in a SelectRoomWidget must all be Rooms!")

    def render(self, rooms, floors, buildings, neighborhoods):
        context = {
            'neighborhoods': neighborhoods,
            'buildings': buildings,
            'floors': floors,
            'rooms': rooms,
        }
        return mark_safe(render_to_string(self.template_name, context))
35

Daniel W Bond's avatar
Daniel W Bond committed
36

37
class StudentUpdateForm(forms.Form):
38

39
40
41
42
    first_name = forms.CharField(label='First Name')
    last_name = forms.CharField(label='Last Name')
    gender = MultiSelectFormField(choices=Student.GENDER_CHOICES,
                                  label='Gender Identity (please choose all that apply)')
43

44
    room = forms.ModelChoiceField(widget=SelectRoomWidget())
45

46
47
    privacy = forms.ChoiceField(choices=Student.PRIVACY_CHOICES)
    major = forms.ModelChoiceField(queryset=Major.objects.all())
48
    graduating_year = forms.IntegerField(label='Graduating Year')
49

Daniel W Bond's avatar
Daniel W Bond committed
50
51
52
53
54

class WelcomeNameForm(forms.Form):

    first_name = forms.CharField(label='First Name')
    last_name = forms.CharField(label='Last Name')
55
56
    gender = MultiSelectFormField(choices=Student.GENDER_CHOICES,
                                  label='Gender Identity (please choose all that apply)')
57
58
59
60


class WelcomePrivacyForm(forms.ModelForm):

Daniel W Bond's avatar
Daniel W Bond committed
61
62
63
64
65
    def __init__(self, *args, **kwargs):
        super(WelcomePrivacyForm, self).__init__(*args, **kwargs)
        if self.instance.recent_changes() >= 2:
            self.fields['room'].widget = forms.widgets.HiddenInput()

66
67
    class Meta:
        model = Student
Daniel W Bond's avatar
Daniel W Bond committed
68
        fields = ('room', 'privacy', )
69
70
71
72


class WelcomeSocialForm(forms.ModelForm):

Daniel W Bond's avatar
Daniel W Bond committed
73
74
75
76
    def __init__(self, *args, **kwargs):
        super(WelcomeSocialForm, self).__init__(*args, **kwargs)
        self.fields['completedSocial'].widget = forms.widgets.HiddenInput()

77
78
    class Meta:
        model = Student
Daniel W Bond's avatar
Daniel W Bond committed
79
        fields = ('completedSocial', )