views.py 5.31 KB
Newer Older
1
# standard library imports
2
from __future__ import absolute_import, print_function, unicode_literals
3
from collections import OrderedDict
4
# core django imports
Daniel W Bond's avatar
Daniel W Bond committed
5
from django.views.generic import DetailView, ListView
6 7
from django.core.exceptions import ObjectDoesNotExist
from django.http import Http404
8 9
# third party imports
from braces.views import LoginRequiredMixin
Daniel W Bond's avatar
Daniel W Bond committed
10
# imports from your apps
11
from .models import Building, Floor, Room
12
from accounts.models import Student
13
from core.utils import shadowbanning
14 15


16
# a list of neighborhoods and their buildings
17
class ListBuildings(ListView):
18
    model = Building
19
    queryset = Building.objects.all()
Daniel W Bond's avatar
Daniel W Bond committed
20
    # paginate_by
21 22 23
    context_object_name = 'buildings'
    template_name = 'list_buildings.html'

Daniel W Bond's avatar
Daniel W Bond committed
24 25
    def get_context_data(self, **kwargs):
        context = super(ListBuildings, self).get_context_data(**kwargs)
26 27 28 29 30 31 32 33 34 35
        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
Daniel W Bond's avatar
Daniel W Bond committed
36
            buildings_by_neighbourhood[neighbourhood[1]] = Building.objects.filter(neighbourhood=neighbourhood[0]).order_by('name')
37 38 39
        # this whole process is done so we don't have template code in triplicate
        # for each neighbourhood
        context['buildings_by_neighbourhood'] = buildings_by_neighbourhood
Daniel W Bond's avatar
Daniel W Bond committed
40 41
        return context

Daniel W Bond's avatar
Daniel W Bond committed
42

43
# building floors, other information
44
class DetailBuilding(DetailView):
45
    model = Building
46
    slug_field = 'slug__iexact'
47
    context_object_name = 'building'
Daniel W Bond's avatar
Daniel W Bond committed
48
    template_name = 'detail_building.html'
49

50 51
    def get_object(self):
        url_parts = self.request.get_full_path().split('/')
52
        # ['', 'housing', 'building',]
53
        building_name = url_parts[2].replace('-', ' ').title()
54 55 56 57
        try:
            building = Building.objects.get(name=building_name)
        except ObjectDoesNotExist:
            raise Http404
58 59
        return building

60 61
    def get_context_data(self, **kwargs):
        context = super(DetailBuilding, self).get_context_data(**kwargs)
62
        context['floors'] = Floor.objects.filter(building=self.get_object()).order_by('number')
63
        return context
64

Daniel W Bond's avatar
Daniel W Bond committed
65

66
# this lists the rooms on the floor
Daniel W Bond's avatar
Daniel W Bond committed
67
class DetailFloor(LoginRequiredMixin, DetailView):
68 69
    model = Floor
    context_object_name = 'floor'
Daniel W Bond's avatar
Daniel W Bond committed
70
    template_name = 'detail_floor.html'
Daniel W Bond's avatar
Daniel W Bond committed
71

72 73
    login_url = 'login'

74 75
    def get_object(self):
        url_parts = self.request.get_full_path().split('/')
76
        # ['', 'housing', 'building', 'floor', ]
77
        building_name = url_parts[2].replace('-', ' ').title()
78
        floor_number = url_parts[3]
79 80 81 82 83 84
        try:
            building = Building.objects.get(name=building_name)
            floor = Floor.objects.get(number=floor_number,
                                      building=building)
        except ObjectDoesNotExist:
            raise Http404
85 86
        return floor

87
    def get_context_data(self, **kwargs):
Daniel W Bond's avatar
Daniel W Bond committed
88
        context = super(DetailFloor, self).get_context_data(**kwargs)
Daniel W Bond's avatar
Daniel W Bond committed
89

90
        requesting_student = self.request.user.student
Daniel W Bond's avatar
Daniel W Bond committed
91

92 93
        students = Student.objects.visible(requesting_student, self.get_object())

94 95 96 97 98 99 100
        # list RAs first
        staff = [student
                 for student in students
                 if student.is_staff()]
        for s in staff:
            students.insert(0, students.pop(students.index(s)))

101
        context['students'] = shadowbanning(requesting_student, students)
102 103 104
        # boolean values; helps cut down on if/else block complexity on the template
        context['notOnFloor'] = not(requesting_student in self.get_object())
        context['notInBuilding'] = not(requesting_student in self.get_object().building)
105 106
        return context

Daniel W Bond's avatar
Daniel W Bond committed
107

108 109 110
class DetailRoom(LoginRequiredMixin, DetailView):
    model = Room
    context_object_name = 'room'
Daniel W Bond's avatar
Daniel W Bond committed
111
    template_name = 'detail_room.html'
112

113 114
    login_url = 'login'

115 116
    def get_object(self):
        url_parts = self.request.get_full_path().split('/')
117
        # ['', 'housing', 'building', 'floor', 'room', ]
118
        building_name = url_parts[2].replace('-', ' ').title()
119 120
        floor_number = url_parts[3]
        room_number = url_parts[4].upper()
121 122 123 124 125 126 127 128
        try:
            building = Building.objects.get(name=building_name)
            floor = Floor.objects.get(number=floor_number,
                                      building=building)
            room = Room.objects.get(floor=floor,
                                    number=room_number)
        except ObjectDoesNotExist:
            raise Http404
129 130
        return room

131
    def get_context_data(self, **kwargs):
Daniel W Bond's avatar
Daniel W Bond committed
132
        context = super(DetailRoom, self).get_context_data(**kwargs)
Daniel W Bond's avatar
Daniel W Bond committed
133

134
        requesting_student = self.request.user.student
Daniel W Bond's avatar
Daniel W Bond committed
135

136
        students = Student.objects.visible(requesting_student, self.get_object())
Daniel W Bond's avatar
Daniel W Bond committed
137

138
        context['students'] = shadowbanning(requesting_student, students)
139 140
        context['notOnFloor'] = not(requesting_student in self.get_object().floor)
        context['notInBuilding'] = not(requesting_student in self.get_object().floor.building)
141
        return context