views.py 4.97 KB
Newer Older
1
2
# standard library imports
from __future__ import absolute_import, print_function
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
# third party imports
from braces.views import LoginRequiredMixin
Daniel W Bond's avatar
Daniel W Bond committed
8
# imports from your apps
9
from .models import Building, Floor, Room
10
from accounts.models import Student
11
from core.utils import shadowbanning
12

13

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

Daniel W Bond's avatar
Daniel W Bond committed
22
23
    def get_context_data(self, **kwargs):
        context = super(ListBuildings, self).get_context_data(**kwargs)
24
25
26
27
28
29
30
31
32
33
34
35
36
37
        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
Daniel W Bond's avatar
Daniel W Bond committed
38
39
        return context

Daniel W Bond's avatar
Daniel W Bond committed
40

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

48
49
50
51
52
53
54
    def get_object(self):
        url_parts = self.request.get_full_path().split('/')
        # [u'', u'housing', u'building',]
        building_name = url_parts[2].replace('-', ' ').title()
        building = Building.objects.get(name=building_name)
        return building

55
56
    def get_context_data(self, **kwargs):
        context = super(DetailBuilding, self).get_context_data(**kwargs)
57
        context['floors'] = Floor.objects.filter(building=self.get_object()).order_by('number')
58
        return context
59

Daniel W Bond's avatar
Daniel W Bond committed
60

61
# this lists the rooms on the floor
Daniel W Bond's avatar
Daniel W Bond committed
62
class DetailFloor(LoginRequiredMixin, DetailView):
63
64
    model = Floor
    context_object_name = 'floor'
Daniel W Bond's avatar
Daniel W Bond committed
65
    template_name = 'detail_floor.html'
Daniel W Bond's avatar
Daniel W Bond committed
66

67
68
    login_url = 'login'

69
70
71
72
    def get_object(self):
        url_parts = self.request.get_full_path().split('/')
        # [u'', u'housing', u'building', u'floor', ]
        building_name = url_parts[2].replace('-', ' ').title()
73
        floor_number = url_parts[3]
74
75
76
77
78
        building = Building.objects.get(name=building_name)
        floor = Floor.objects.get(number=floor_number,
                                  building=building)
        return floor

79
    def get_context_data(self, **kwargs):
Daniel W Bond's avatar
Daniel W Bond committed
80
        context = super(DetailFloor, self).get_context_data(**kwargs)
Daniel W Bond's avatar
Daniel W Bond committed
81

82
        requesting_student = self.request.user.student
Daniel W Bond's avatar
Daniel W Bond committed
83

84
85
        students = Student.objects.visible(requesting_student, self.get_object())

86
87
88
89
90
91
92
        # 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)))

93
        context['students'] = shadowbanning(requesting_student, students)
94
95
96
        # 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)
97
98
        return context

Daniel W Bond's avatar
Daniel W Bond committed
99

100
101
102
class DetailRoom(LoginRequiredMixin, DetailView):
    model = Room
    context_object_name = 'room'
Daniel W Bond's avatar
Daniel W Bond committed
103
    template_name = 'detail_room.html'
104

105
106
    login_url = 'login'

107
108
109
110
    def get_object(self):
        url_parts = self.request.get_full_path().split('/')
        # [u'', u'housing', u'building', u'floor', u'room', ]
        building_name = url_parts[2].replace('-', ' ').title()
111
112
        floor_number = url_parts[3]
        room_number = url_parts[4].upper()
113
114
115
116
117
118
119
        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)
        return room

120
    def get_context_data(self, **kwargs):
Daniel W Bond's avatar
Daniel W Bond committed
121
        context = super(DetailRoom, self).get_context_data(**kwargs)
Daniel W Bond's avatar
Daniel W Bond committed
122

123
        requesting_student = self.request.user.student
Daniel W Bond's avatar
Daniel W Bond committed
124

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

127
        context['students'] = shadowbanning(requesting_student, students)
128
129
        context['notOnFloor'] = not(requesting_student in self.get_object().floor)
        context['notInBuilding'] = not(requesting_student in self.get_object().floor.building)
130
        return context