views.py 3.92 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 29 30
# building floors, other information
class DetailBuilding(LoginRequiredMixin, DetailView):
    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

    login_url = 'login'
36

37 38 39 40 41 42 43
    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

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

Daniel W Bond's avatar
Daniel W Bond committed
49

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

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

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

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

100 101 102
        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
103

104
        return context