forms.py 3.36 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
from .models import Student, Major
from housing.models import Building, Floor, Room


class SelectRoomWidget(forms.widgets.Select):

    template_name = 'room_select_widget.html'

21
22
23
24
25
    def __init__(self, attrs=None, choices=None, floors=None, buildings=None, neighborhoods=None):
        super(SelectRoomWidget, self).__init__(attrs)
        # attrs to be implemented later (allows specifying css class, for example)
        if attrs:
            print("Sorry about that, but we're currently ignoring your fancy attrs.")
26
        # should probably type check the other fields too
27
28
29
30
31
32
33
34
35
36
37
38
39
        if choices is None:
            self.rooms = Room.objects.all()
        else:
            if not all(isinstance(thing, Room) for thing in rooms):
                raise TypeError("Rooms in a SelectRoomWidget must all be Rooms!")
        if floors is None:
            self.floors = Floor.objects.all()
        if buildings is None:
            self.buildings = Building.objects.all()
        if neighborhoods is None:
            self.neighborhoods = Building.NEIGHBOURHOOD_CHOICES

    def render(self, name, value, attrs=None):
40
        context = {
41
42
43
44
            'neighborhoods': self.neighborhoods,
            'buildings': self.buildings,
            'floors': self.floors,
            'rooms': self.rooms,
45
46
        }
        return mark_safe(render_to_string(self.template_name, context))
47

Daniel W Bond's avatar
Daniel W Bond committed
48

49
class StudentUpdateForm(forms.Form):
50

51
52
53
54
    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)')
55

56
57
    room = forms.ModelChoiceField(queryset=Room.objects.all(), widget=SelectRoomWidget(),
                                  label='')
58

59
60
    privacy = forms.ChoiceField(choices=Student.PRIVACY_CHOICES)
    major = forms.ModelChoiceField(queryset=Major.objects.all())
61
    graduating_year = forms.IntegerField(label='Graduating Year')
62

Daniel W Bond's avatar
Daniel W Bond committed
63
64
65
66
67

class WelcomeNameForm(forms.Form):

    first_name = forms.CharField(label='First Name')
    last_name = forms.CharField(label='Last Name')
68
69
    gender = MultiSelectFormField(choices=Student.GENDER_CHOICES,
                                  label='Gender Identity (please choose all that apply)')
70
71
72
73


class WelcomePrivacyForm(forms.ModelForm):

Daniel W Bond's avatar
Daniel W Bond committed
74
75
76
77
78
    def __init__(self, *args, **kwargs):
        super(WelcomePrivacyForm, self).__init__(*args, **kwargs)
        if self.instance.recent_changes() >= 2:
            self.fields['room'].widget = forms.widgets.HiddenInput()

79
80
    class Meta:
        model = Student
Daniel W Bond's avatar
Daniel W Bond committed
81
        fields = ('room', 'privacy', )
82
83
84
85


class WelcomeSocialForm(forms.ModelForm):

Daniel W Bond's avatar
Daniel W Bond committed
86
87
88
89
    def __init__(self, *args, **kwargs):
        super(WelcomeSocialForm, self).__init__(*args, **kwargs)
        self.fields['completedSocial'].widget = forms.widgets.HiddenInput()

90
91
    class Meta:
        model = Student
Daniel W Bond's avatar
Daniel W Bond committed
92
        fields = ('completedSocial', )