forms.py 4.21 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
from django.core.exceptions import ValidationError
8
# third party imports
Daniel W Bond's avatar
Daniel W Bond committed
9
from multiselectfield import MultiSelectFormField
10
# imports from your apps
11
12
13
14
15
from .models import Student, Major
from housing.models import Building, Floor, Room


class SelectRoomWidget(forms.widgets.Select):
16
    """A series of dropdowns in which a student can filter through housing options."""
17
18
19

    template_name = 'room_select_widget.html'

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

Daniel W Bond's avatar
Daniel W Bond committed
49

Daniel W Bond's avatar
Daniel W Bond committed
50
class SelectRoomField(forms.models.ModelChoiceField):
51
    """A special field for room selection, using the room selection widget."""
Daniel W Bond's avatar
Daniel W Bond committed
52
53
    widget = SelectRoomWidget

54
55
#    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
56

57
58

class BooleanRadioField(forms.TypedChoiceField):
59
    """Displays booleans as a radio selector, rather than checkboxes."""
60
61
62
63
64
65
66
67
68
69
70

    def __init__(self, *args, **kwargs):
        boolean_choices = ((True, 'Yes'), (False, 'No'))

        kwargs['widget'] = forms.RadioSelect
        kwargs['choices'] = boolean_choices
        kwargs['coerce'] = bool
        kwargs['required'] = True
        super(BooleanRadioField, self).__init__(*args, **kwargs)


71
class StudentUpdateForm(forms.Form):
72

73
74
    first_name = forms.CharField(required=False)
    last_name = forms.CharField(required=False)
75
    gender = MultiSelectFormField(choices=Student.GENDER_CHOICES,
76
                                  required=False)
77
    show_gender = BooleanRadioField()
78

79
80
    on_campus = BooleanRadioField()
    room = SelectRoomField(queryset=Room.objects.all(), required=False)
81

82
    privacy = forms.TypedChoiceField(choices=Student.PRIVACY_CHOICES)
Daniel W Bond's avatar
Daniel W Bond committed
83
84
85
86
    # exclude self? .exclude(user=self)
    blocked_kids = forms.ModelMultipleChoiceField(queryset=Student.objects.all(),
                                                  required=False)

87
    major = forms.ModelMultipleChoiceField(queryset=Major.objects.all(), required=False)
88
    graduating_year = forms.IntegerField()
89

90
91
92
    def clean(self):
        cleaned_data = super(StudentUpdateForm, self).clean()
        form_room = cleaned_data.get('room')
93
94
        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
95
            # print(students_in_room)
96
97
98
            # 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')
99

Daniel W Bond's avatar
Daniel W Bond committed
100
    def is_valid(self):
101
        # errors are not printed in form.as_p?
Daniel W Bond's avatar
pep8 me    
Daniel W Bond committed
102
103
104
        # 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
105
        valid = super(StudentUpdateForm, self).is_valid()
Daniel W Bond's avatar
pep8 me    
Daniel W Bond committed
106
        # print(valid)
Daniel W Bond's avatar
Daniel W Bond committed
107
        return valid