views.py 4.45 KB
Newer Older
1 2 3 4 5 6 7
# core django imports
from django.views.generic import DetailView, ListView, CreateView, UpdateView,\
    DeleteView
# third party imports
from braces.views import LoginRequiredMixin
# imports from your apps 
from .models import Building, Floor, Room
8
from accounts.models import Student
9

10

11 12 13 14 15
# a list of neighborhoods and their buildings
class ListBuildings(LoginRequiredMixin, ListView):
    model = Building
    login_url = '/'

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

23 24 25
# building floors, other information
class DetailBuilding(LoginRequiredMixin, DetailView):
    model = Building
26
    context_object_name = 'building'
27 28 29 30
    template_name='detailBuilding.html'

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

34 35 36
    login_url = '/'

# this lists the rooms on the floor
Daniel W Bond's avatar
Daniel W Bond committed
37
class DetailFloor(LoginRequiredMixin, DetailView): 
38 39
    model = Floor
    context_object_name = 'floor'
Daniel W Bond's avatar
Daniel W Bond committed
40
    template_name = 'detail_floor.html'
Daniel W Bond's avatar
Daniel W Bond committed
41
    
42
    def get_context_data(self, **kwargs):
Daniel W Bond's avatar
Daniel W Bond committed
43
        context = super(DetailFloor, self).get_context_data(**kwargs)
Daniel W Bond's avatar
Daniel W Bond committed
44
    
45
        #requesting_student = Student.objects.get(user=self.request.user)
Daniel W Bond's avatar
Daniel W Bond committed
46 47
        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
48 49 50 51

        # if self.request.user is on the floor
        def onFloor():
            floor_status = False
Daniel W Bond's avatar
Daniel W Bond committed
52
            if requesting_student.get_floor() == self.get_object():
Daniel W Bond's avatar
Daniel W Bond committed
53 54 55 56 57 58
                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
59
            if requesting_student.get_building() == self.get_object().building:
Daniel W Bond's avatar
Daniel W Bond committed
60 61 62
                building_status = True
            return building_status

Daniel W Bond's avatar
Daniel W Bond committed
63
        rooms = Room.objects.filter(floor=self.get_object()).order_by('number')
Daniel W Bond's avatar
Daniel W Bond committed
64 65 66 67 68 69 70 71 72
        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())

73
        context['students'] = floor_students
74 75
        context['notOnFloor'] = not onFloor()
        context['notInBuilding'] = not inBuilding()
76 77
        return context

78 79 80 81 82 83
class DetailRoom(LoginRequiredMixin, DetailView):
    model = Room
    context_object_name = 'room'
    template_name='detailBuilding.html'

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

86 87 88
        #requesting_student = Student.objects.get(user=self.request.user)
        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
89 90 91 92

        # if self.request.user is on the floor
        def onFloor():
            floor_status = False
Daniel W Bond's avatar
Daniel W Bond committed
93
            if requesting_student.get_floor() == self.get_object().floor:
Daniel W Bond's avatar
Daniel W Bond committed
94 95 96 97 98 99
                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
100
            if requesting_student.get_building() == self.get_object().floor.building:
Daniel W Bond's avatar
Daniel W Bond committed
101 102 103 104 105 106 107 108 109 110
                building_status = True
            return building_status

        if onFloor():
             students = Student.objects.filter(room=self.get_object()).floor_building_students()
        elif inBuilding():
             students = Student.objects.filter(room=self.get_object()).building_students()
        else:
             students = Student.objects.filter(room=self.get_object()).students()

111
        context['students'] = students
Daniel W Bond's avatar
Daniel W Bond committed
112 113 114
        context['notOnFloor'] = not onFloor()
        context['notInBuilding'] = not inBuilding()

115 116 117
        return context

    login_url = '/'
118

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