Commit 4c050d73 authored by Daniel W Bond's avatar Daniel W Bond
Browse files

return an ordered dictionary to the list buildings page so that we don't have...

return an ordered dictionary to the list buildings page so that we don't have to have the same coded repeated thrice on the template for each neighbourhood
parent 9cb93aca
......@@ -34,43 +34,11 @@
</div>
</div>
<legend><strong><a name="Rappahannock" href="#Rappahannock">Rappahannock</a></strong></legend>
{% for neighbourhood, buildings in buildings_by_neighbourhood.items %}
<legend><strong><a name="{{ neighbourhood }}" href="#{{ neighbourhood }}">{{ neighbourhood }}</a></strong></legend>
<div class="row">
{% for building in rappahannock %}
<div class="col-sm-3 text-center">
<a href="{{ building.get_absolute_url }}" class="btn btn-primary btn-lg btn-block">{{ building.name }}</a>
</div>
{% if forloop.counter|divisibleby:4 %}
</div>
<br />
<div class="row">
{% endif %}
{% endfor %}
</div>
<br />
<legend><strong><a name ="Shenandoah" href="#Shenandoah">Shenandoah</a></strong></legend>
<div class="row">
{% for building in shenandoah %}
<div class="col-sm-3 text-center">
<a href="{{ building.get_absolute_url }}" class="btn btn-primary btn-lg btn-block">{{ building.name }}</a>
</div>
{% if forloop.counter|divisibleby:4 %}
</div>
<br />
<div class="row">
{% endif %}
{% endfor %}
</div>
<br />
<legend><strong><a name="Aquia" href="#Aquia">Aquia</a></strong></legend>
<div class="row">
{% for building in aquia %}
{% for building in buildings %}
<div class="col-sm-3 text-center">
<a href="{{ building.get_absolute_url }}" class="btn btn-primary btn-lg btn-block">{{ building.name }}</a>
</div>
......@@ -83,6 +51,7 @@
</div>
<br />
{% endfor %}
<legend><a name="Unsupported" href="#Unsupported">Unsupported Buildings</a></legend>
<div class="row">
......
# standard library imports
from __future__ import absolute_import, print_function
from collections import OrderedDict
# core django imports
from django.views.generic import DetailView, ListView
# third party imports
......@@ -34,9 +35,20 @@ class ListBuildings(ListView):
def get_context_data(self, **kwargs):
context = super(ListBuildings, self).get_context_data(**kwargs)
context['rappahannock'] = Building.objects.filter(neighbourhood='ra').order_by('name')
context['shenandoah'] = Building.objects.filter(neighbourhood='sh').order_by('name')
context['aquia'] = Building.objects.filter(neighbourhood='aq').order_by('name')
neighbourhoods = (('aq', 'Aquia'),
('ra', 'Rappahannock'),
('sh', 'Shenandoah'))
# we want our neighbourhoods in alphabetical order
buildings_by_neighbourhood = OrderedDict()
# create an ordered dictionary with neighbourhood name, building in said
# neighbourhood pairings
for neighbourhood in neighbourhoods:
# the tuple matrix was necessary because what we'll render for humans
# is not the string for filtering in the database
buildings_by_neighbourhood[neighbourhood[1]] = Building.objects.filter(neighbourhood=neighbourhood[0]).order_by('name')
# this whole process is done so we don't have template code in triplicate
# for each neighbourhood
context['buildings_by_neighbourhood'] = buildings_by_neighbourhood
return context
......
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