views.py 3.93 KB
Newer Older
1
2
# standard library imports
from __future__ import absolute_import, print_function
3
# core django imports
Daniel W Bond's avatar
Daniel W Bond committed
4
from django.views.generic import DetailView, ListView
5
6
# third party imports
from braces.views import LoginRequiredMixin
Daniel W Bond's avatar
Daniel W Bond committed
7
# imports from your apps
8
from .models import Building, Floor, Room
9
from accounts.models import Student
10

11

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

Daniel W Bond's avatar
Daniel W Bond committed
20
21
    def get_context_data(self, **kwargs):
        context = super(ListBuildings, self).get_context_data(**kwargs)
Daniel W Bond's avatar
Daniel W Bond committed
22
23
24
        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')
Daniel W Bond's avatar
Daniel W Bond committed
25
26
        return context

Daniel W Bond's avatar
Daniel W Bond committed
27

28
# building floors, other information
29
class DetailBuilding(DetailView):
30
    model = Building
31
    slug_field = 'slug__iexact'
32
    context_object_name = 'building'
Daniel W Bond's avatar
Daniel W Bond committed
33
    template_name = 'detail_building.html'
34

35
36
37
38
39
40
41
    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

42
43
    def get_context_data(self, **kwargs):
        context = super(DetailBuilding, self).get_context_data(**kwargs)
44
        context['floors'] = Floor.objects.filter(building=self.get_object()).order_by('number')
45
        return context
46

Daniel W Bond's avatar
Daniel W Bond committed
47

48
# this lists the rooms on the floor
Daniel W Bond's avatar
Daniel W Bond committed
49
class DetailFloor(LoginRequiredMixin, DetailView):
50
51
    model = Floor
    context_object_name = 'floor'
Daniel W Bond's avatar
Daniel W Bond committed
52
    template_name = 'detail_floor.html'
Daniel W Bond's avatar
Daniel W Bond committed
53

54
55
    login_url = 'login'

56
57
58
59
    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()
60
        floor_number = url_parts[3]
61
62
63
64
65
        building = Building.objects.get(name=building_name)
        floor = Floor.objects.get(number=floor_number,
                                  building=building)
        return floor

66
    def get_context_data(self, **kwargs):
Daniel W Bond's avatar
Daniel W Bond committed
67
        context = super(DetailFloor, self).get_context_data(**kwargs)
Daniel W Bond's avatar
Daniel W Bond committed
68

69
        requesting_student = Student.objects.get(user=self.request.user)
Daniel W Bond's avatar
Daniel W Bond committed
70

71
72
73
        context['students'] = Student.objects.visible(requesting_student, self.get_object())
        context['notOnFloor'] = not(requesting_student in self.get_object())
        context['notInBuilding'] = not(requesting_student in self.get_object().building)
74
75
        return context

Daniel W Bond's avatar
Daniel W Bond committed
76

77
78
79
class DetailRoom(LoginRequiredMixin, DetailView):
    model = Room
    context_object_name = 'room'
Daniel W Bond's avatar
Daniel W Bond committed
80
    template_name = 'detail_room.html'
81

82
83
    login_url = 'login'

84
85
86
87
    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()
88
89
        floor_number = url_parts[3]
        room_number = url_parts[4].upper()
90
91
92
93
94
95
96
        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

97
    def get_context_data(self, **kwargs):
Daniel W Bond's avatar
Daniel W Bond committed
98
        context = super(DetailRoom, self).get_context_data(**kwargs)
Daniel W Bond's avatar
Daniel W Bond committed
99

100
        requesting_student = Student.objects.get(user=self.request.user)
Daniel W Bond's avatar
Daniel W Bond committed
101

102
103
104
        context['students'] = Student.objects.visible(requesting_student, self.get_object())
        context['notOnFloor'] = not(requesting_student in self.get_object().floor)
        context['notInBuilding'] = not(requesting_student in self.get_object().floor.building)
Daniel W Bond's avatar
Daniel W Bond committed
105

106
        return context