Commit f9a93d2d authored by Daniel W Bond's avatar Daniel W Bond
Browse files

began implementation of separate room select widget

parent 131743f0
...@@ -2,14 +2,36 @@ ...@@ -2,14 +2,36 @@
from __future__ import absolute_import, print_function from __future__ import absolute_import, print_function
# core django imports # core django imports
from django import forms from django import forms
from django.utils.safestring import mark_safe
from django.template.loader import render_to_string
# third party imports # third party imports
from crispy_forms.helper import FormHelper from crispy_forms.helper import FormHelper
from crispy_forms.layout import Submit, Layout from crispy_forms.layout import Submit, Layout
from crispy_forms.bootstrap import PrependedText, AppendedText from crispy_forms.bootstrap import PrependedText, AppendedText
from multiselectfield import MultiSelectFormField from multiselectfield import MultiSelectFormField
# imports from your apps # imports from your apps
from .models import Student, Room, Major from .models import Student, Major
from housing.models import Building 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))
class StudentUpdateForm(forms.Form): class StudentUpdateForm(forms.Form):
...@@ -19,10 +41,7 @@ class StudentUpdateForm(forms.Form): ...@@ -19,10 +41,7 @@ class StudentUpdateForm(forms.Form):
gender = MultiSelectFormField(choices=Student.GENDER_CHOICES, gender = MultiSelectFormField(choices=Student.GENDER_CHOICES,
label='Gender Identity (please choose all that apply)') label='Gender Identity (please choose all that apply)')
#neighborhood = forms.ChoiceField(choices=Building.NEIGHBOURHOOD_CHOICES) room = forms.ModelChoiceField(widget=SelectRoomWidget())
#building = forms.ModelChoiceField(queryset=Building.objects.filter(neighbourhood=neighborhood)
#floor = forms.ModelChoiceField(queryset=Floor.objects.filter(building=building)
#room = forms.ModelChoiceField(queryset=Room.objects.filter(floor=floor))
privacy = forms.ChoiceField(choices=Student.PRIVACY_CHOICES) privacy = forms.ChoiceField(choices=Student.PRIVACY_CHOICES)
major = forms.ModelChoiceField(queryset=Major.objects.all()) major = forms.ModelChoiceField(queryset=Major.objects.all())
...@@ -33,7 +52,8 @@ class WelcomeNameForm(forms.Form): ...@@ -33,7 +52,8 @@ class WelcomeNameForm(forms.Form):
first_name = forms.CharField(label='First Name') first_name = forms.CharField(label='First Name')
last_name = forms.CharField(label='Last Name') last_name = forms.CharField(label='Last Name')
gender = MultiSelectFormField(choices=Student.GENDER_CHOICES, label='Gender Identity (please choose all that apply)') gender = MultiSelectFormField(choices=Student.GENDER_CHOICES,
label='Gender Identity (please choose all that apply)')
class WelcomePrivacyForm(forms.ModelForm): class WelcomePrivacyForm(forms.ModelForm):
......
{% load humanize %}
<div class="form-group">
<label>Neighborhood</label>
<select id="neighborhood" name="neighborhood">
{% for neighborhood in neighborhoods %}
<option value="{{ neighborhood.0 }}">{{ neighborhood.1 }}</option>
{% endfor %}
</select>
<label>Building</label>
<select id="building" name="building">
<option value="">---</option>
{% for building in buildings %}
<option value="{{ building.name }}" class="{{ building.neighbourhood }}">{{ building.name }}</option>
{% endfor %}
</select>
<label>Floor</label>
<select id="floor" name="floor">
<option value="">---</option>
{% for floor in floors %}
<option value="{{ floor.slug }}" class="{{ floor.building.name }}">{{ floor.number|ordinal }}</option>
{% endfor %}
</select>
<label>Room</label>
<select id="room" name="room" class="roompicker">
<option value="">---</option>
{% for room in rooms %}
<option value="{{ room.number }}" class="{{ room.floor.slug }}">{{ room.number }}</option>
{% endfor %}
</select>
</div>
...@@ -80,8 +80,6 @@ ...@@ -80,8 +80,6 @@
</div> </div>
</div> </div>
{% load humanize %}
<div class="col-md-8"> <div class="col-md-8">
<div class="panel panel-default"> <div class="panel panel-default">
...@@ -93,35 +91,6 @@ ...@@ -93,35 +91,6 @@
<form class="form-horizontal" action="" method="post">{% csrf_token %} <form class="form-horizontal" action="" method="post">{% csrf_token %}
{{ my_form.as_p }} {{ my_form.as_p }}
<div class="form-group">
<label>Neighborhood</label>
<select id="neighborhood" name="neighborhood">
{% for neighborhood in neighborhoods %}
<option value="{{ neighborhood.0 }}">{{ neighborhood.1 }}</option>
{% endfor %}
</select>
<label>Building</label>
<select id="building" name="building">
<option value="">---</option>
{% for building in buildings %}
<option value="{{ building.name }}" class="{{ building.neighbourhood }}">{{ building.name }}</option>
{% endfor %}
</select>
<label>Floor</label>
<select id="floor" name="floor">
<option value="">---</option>
{% for floor in floors %}
<option value="{{ floor.slug }}" class="{{ floor.building.name }}">{{ floor.number|ordinal }}</option>
{% endfor %}
</select>
<label>Room</label>
<select id="room" name="room">
<option value="">---</option>
{% for room in rooms %}
<option value="{{ room.number }}" class="{{ room.floor.slug }}">{{ room.number }}</option>
{% endfor %}
</select>
</div>
<input type="submit" value="Save" class="btn btn-primary"/> <input type="submit" value="Save" class="btn btn-primary"/>
</form> </form>
......
...@@ -225,10 +225,6 @@ class UpdateStudent(LoginRequiredMixin, FormValidMessageMixin, FormView): ...@@ -225,10 +225,6 @@ class UpdateStudent(LoginRequiredMixin, FormValidMessageMixin, FormView):
context['my_form'] = form context['my_form'] = form
context['neighborhoods'] = Building.NEIGHBOURHOOD_CHOICES
context['buildings'] = Building.objects.all()
context['floors'] = Floor.objects.all()
context['rooms'] = Room.objects.all()
return context return context
def form_valid(self, form): def form_valid(self, form):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment