views.py 4.38 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

    def get_context_data(self, **kwargs):
        context = super(DetailBuilding, self).get_context_data(**kwargs)
Daniel W Bond's avatar
Daniel W Bond committed
41
        context['floors'] = Floor.objects.filter(building__name=''+self.get_object().name).order_by('number')
42
        return context
43

Daniel W Bond's avatar
Daniel W Bond committed
44

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

51
    def get_context_data(self, **kwargs):
Daniel W Bond's avatar
Daniel W Bond committed
52
        context = super(DetailFloor, self).get_context_data(**kwargs)
Daniel W Bond's avatar
Daniel W Bond committed
53

54
        requesting_student = Student.objects.get(user=self.request.user)
Daniel W Bond's avatar
Daniel W Bond committed
55 56 57 58

        # if self.request.user is on the floor
        def onFloor():
            floor_status = False
Daniel W Bond's avatar
Daniel W Bond committed
59
            if requesting_student.get_floor() == self.get_object():
Daniel W Bond's avatar
Daniel W Bond committed
60 61 62 63 64 65
                floor_status = True
            return floor_status

        # if self.request.user is in the building
        def inBuilding():
            building_status = False
Daniel W Bond's avatar
Daniel W Bond committed
66
            if requesting_student.get_building() == self.get_object().building:
Daniel W Bond's avatar
Daniel W Bond committed
67 68 69
                building_status = True
            return building_status

Daniel W Bond's avatar
Daniel W Bond committed
70
        rooms = Room.objects.filter(floor=self.get_object()).order_by('number')
Daniel W Bond's avatar
Daniel W Bond committed
71 72 73 74 75 76 77 78 79
        floor_students = []
        for room in rooms:
            if onFloor():
                floor_students.extend(Student.objects.filter(room=room).floor_building_students())
            elif inBuilding():
                floor_students.extend(Student.objects.filter(room=room).building_students())
            else:
                floor_students.extend(Student.objects.filter(room=room).students())

80
        context['students'] = floor_students
81 82
        context['notOnFloor'] = not onFloor()
        context['notInBuilding'] = not inBuilding()
83 84
        return context

Daniel W Bond's avatar
Daniel W Bond committed
85

86 87 88
class DetailRoom(LoginRequiredMixin, DetailView):
    model = Room
    context_object_name = 'room'
Daniel W Bond's avatar
Daniel W Bond committed
89
    template_name = 'detail_room.html'
90 91

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

94
        requesting_student = Student.objects.get(user=self.request.user)
Daniel W Bond's avatar
Daniel W Bond committed
95 96 97 98

        # if self.request.user is on the floor
        def onFloor():
            floor_status = False
Daniel W Bond's avatar
Daniel W Bond committed
99
            if requesting_student.get_floor() == self.get_object().floor:
Daniel W Bond's avatar
Daniel W Bond committed
100 101 102 103 104 105
                floor_status = True
            return floor_status

        # if self.request.user is in the building
        def inBuilding():
            building_status = False
Daniel W Bond's avatar
Daniel W Bond committed
106
            if requesting_student.get_building() == self.get_object().floor.building:
Daniel W Bond's avatar
Daniel W Bond committed
107 108 109 110
                building_status = True
            return building_status

        if onFloor():
Daniel W Bond's avatar
Daniel W Bond committed
111
            students = Student.objects.filter(room=self.get_object()).floor_building_students()
Daniel W Bond's avatar
Daniel W Bond committed
112
        elif inBuilding():
Daniel W Bond's avatar
Daniel W Bond committed
113
            students = Student.objects.filter(room=self.get_object()).building_students()
Daniel W Bond's avatar
Daniel W Bond committed
114
        else:
Daniel W Bond's avatar
Daniel W Bond committed
115
            students = Student.objects.filter(room=self.get_object()).students()
Daniel W Bond's avatar
Daniel W Bond committed
116

117
        context['students'] = students
Daniel W Bond's avatar
Daniel W Bond committed
118 119 120
        context['notOnFloor'] = not onFloor()
        context['notInBuilding'] = not inBuilding()

121 122 123
        return context

    login_url = '/'
124

Daniel W Bond's avatar
Daniel W Bond committed
125

126
# deleted 'UpdateRoom' view-- that will be handled on the user's page