views.py 4.66 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
        requesting_student_filter = Student.objects.filter(user=self.request.user)
        requesting_student = requesting_student_filter[0]
Daniel W Bond's avatar
Daniel W Bond committed
57 58 59 60

        # if self.request.user is on the floor
        def onFloor():
            floor_status = False
Daniel W Bond's avatar
Daniel W Bond committed
61
            if requesting_student.get_floor() == self.get_object():
Daniel W Bond's avatar
Daniel W Bond committed
62 63 64 65 66 67
                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
68
            if requesting_student.get_building() == self.get_object().building:
Daniel W Bond's avatar
Daniel W Bond committed
69 70 71
                building_status = True
            return building_status

Daniel W Bond's avatar
Daniel W Bond committed
72
        rooms = Room.objects.filter(floor=self.get_object()).order_by('number')
Daniel W Bond's avatar
Daniel W Bond committed
73 74 75 76 77 78 79 80 81
        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())

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

Daniel W Bond's avatar
Daniel W Bond committed
87

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

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

Daniel W Bond's avatar
Daniel W Bond committed
96
        # requesting_student = Student.objects.get(user=self.request.user)
97 98
        requesting_student_filter = Student.objects.filter(user=self.request.user)
        requesting_student = requesting_student_filter[0]
Daniel W Bond's avatar
Daniel W Bond committed
99 100 101 102

        # if self.request.user is on the floor
        def onFloor():
            floor_status = False
Daniel W Bond's avatar
Daniel W Bond committed
103
            if requesting_student.get_floor() == self.get_object().floor:
Daniel W Bond's avatar
Daniel W Bond committed
104 105 106 107 108 109
                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
110
            if requesting_student.get_building() == self.get_object().floor.building:
Daniel W Bond's avatar
Daniel W Bond committed
111 112 113 114
                building_status = True
            return building_status

        if onFloor():
Daniel W Bond's avatar
Daniel W Bond committed
115
            students = Student.objects.filter(room=self.get_object()).floor_building_students()
Daniel W Bond's avatar
Daniel W Bond committed
116
        elif inBuilding():
Daniel W Bond's avatar
Daniel W Bond committed
117
            students = Student.objects.filter(room=self.get_object()).building_students()
Daniel W Bond's avatar
Daniel W Bond committed
118
        else:
Daniel W Bond's avatar
Daniel W Bond committed
119
            students = Student.objects.filter(room=self.get_object()).students()
Daniel W Bond's avatar
Daniel W Bond committed
120

121
        context['students'] = students
Daniel W Bond's avatar
Daniel W Bond committed
122 123 124
        context['notOnFloor'] = not onFloor()
        context['notInBuilding'] = not inBuilding()

125 126 127
        return context

    login_url = '/'
128

Daniel W Bond's avatar
Daniel W Bond committed
129

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