forms.py 4.58 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
Daniel W Bond's avatar
Daniel W Bond committed
7
8
from django.utils.encoding import force_text
from django.core.exceptions import ValidationError
9
# third party imports
10
from crispy_forms.helper import FormHelper
Daniel W Bond's avatar
Daniel W Bond committed
11
12
from crispy_forms.layout import Submit, Layout
from crispy_forms.bootstrap import PrependedText, AppendedText
Daniel W Bond's avatar
Daniel W Bond committed
13
from multiselectfield import MultiSelectFormField
14
# imports from your apps
15
16
17
18
19
20
21
22
from .models import Student, Major
from housing.models import Building, Floor, Room


class SelectRoomWidget(forms.widgets.Select):

    template_name = 'room_select_widget.html'

23
    def __init__(self, user=None, attrs=None, rooms=None, floors=None, buildings=None, neighborhoods=None):
24
25
26
27
        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.")
28
        # should probably type check the other fields too
29
30
        if rooms is None:
            self.rooms = Room.objects.all()
31
32
33
34
35
36
37
38
39
40
41
        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):
42
        context = {
43
44
45
            'neighborhoods': self.neighborhoods,
            'buildings': self.buildings,
            'floors': self.floors,
46
            'rooms': self.rooms,
47
        }
48
49
        if self.user is not None:
            context['user'] = self.user
50
        return mark_safe(render_to_string(self.template_name, context))
51

Daniel W Bond's avatar
Daniel W Bond committed
52

Daniel W Bond's avatar
Daniel W Bond committed
53
54
55
class SelectRoomField(forms.models.ModelChoiceField):
    widget = SelectRoomWidget

56
57
#    should raise error if user hasn't actually selected room, made it to end of selectors
#    def clean(self, value):
Daniel W Bond's avatar
Daniel W Bond committed
58

59
class StudentUpdateForm(forms.Form):
60

61
62
    first_name = forms.CharField(label='First Name', required=False)
    last_name = forms.CharField(label='Last Name', required=False)
63
    gender = MultiSelectFormField(choices=Student.GENDER_CHOICES,
64
65
                                  label='Gender Identity (please choose all that apply)',
                                  required=False)
66
67
    show_gender = forms.BooleanField(label='Show your gender on your profile?',
                                     required=False)
68

69
    room = SelectRoomField(queryset=Room.objects.all(), label='', required=False)
70

71
    privacy = forms.ChoiceField(choices=Student.PRIVACY_CHOICES)
72
    major = forms.ModelChoiceField(queryset=Major.objects.all(), required=False)
73
    graduating_year = forms.IntegerField(label='Graduating Year')
74

Daniel W Bond's avatar
Daniel W Bond committed
75

Daniel W Bond's avatar
Daniel W Bond committed
76
    def is_valid(self):
77
78
79
80
        # errors are not printed in form.as_p?
        #print("In is_valid.")
        #print(self.is_bound, 'is bound')
        #print(self.errors, type(self.errors), 'errors')
Daniel W Bond's avatar
Daniel W Bond committed
81
        valid = super(StudentUpdateForm, self).is_valid()
82
        #print(valid)
Daniel W Bond's avatar
Daniel W Bond committed
83
84
        return valid

Daniel W Bond's avatar
Daniel W Bond committed
85
86
class WelcomeNameForm(forms.Form):

87
88
    first_name = forms.CharField(label='First Name', required=False)
    last_name = forms.CharField(label='Last Name', required=False)
89
    gender = MultiSelectFormField(choices=Student.GENDER_CHOICES,
90
91
92
93
                                  label='Gender Identity (please choose all that apply)',
                                  required=False)
    show_gender = forms.BooleanField(label='Show your gender on your profile?',
                                     required=False)
94
95
96
97


class WelcomePrivacyForm(forms.ModelForm):

Daniel W Bond's avatar
Daniel W Bond committed
98
99
100
101
    def __init__(self, *args, **kwargs):
        super(WelcomePrivacyForm, self).__init__(*args, **kwargs)
        if self.instance.recent_changes() >= 2:
            self.fields['room'].widget = forms.widgets.HiddenInput()
102
103
104
        else:
            self.fields['room'] = SelectRoomField(queryset=Room.objects.all(),
                                                  label='', required=False)
Daniel W Bond's avatar
Daniel W Bond committed
105

106
107
    class Meta:
        model = Student
Daniel W Bond's avatar
Daniel W Bond committed
108
        fields = ('room', 'privacy', )
109
110
111
112


class WelcomeSocialForm(forms.ModelForm):

Daniel W Bond's avatar
Daniel W Bond committed
113
114
115
116
    def __init__(self, *args, **kwargs):
        super(WelcomeSocialForm, self).__init__(*args, **kwargs)
        self.fields['completedSocial'].widget = forms.widgets.HiddenInput()

117
118
    class Meta:
        model = Student
Daniel W Bond's avatar
Daniel W Bond committed
119
        fields = ('completedSocial', )