views.py 3.97 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 13 14
# a list of neighborhoods and their buildings
class ListBuildings(LoginRequiredMixin, ListView):
    model = Building
15
    queryset = Building.objects.all()
Daniel W Bond's avatar
Daniel W Bond committed
16
    # paginate_by
17 18 19 20
    context_object_name = 'buildings'
    template_name = 'list_buildings.html'

    login_url = 'login'
21

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)
Daniel W Bond's avatar
Daniel W Bond committed
24 25 26
        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
27 28
        return context

Daniel W Bond's avatar
Daniel W Bond committed
29

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

    login_url = 'login'
38

39 40 41 42 43 44 45
    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

46 47
    def get_context_data(self, **kwargs):
        context = super(DetailBuilding, self).get_context_data(**kwargs)
48
        context['floors'] = Floor.objects.filter(building=self.get_object()).order_by('number')
49
        return context
50

Daniel W Bond's avatar
Daniel W Bond committed
51

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

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

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

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

73 74 75
        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)
76 77
        return context

Daniel W Bond's avatar
Daniel W Bond committed
78

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

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