views.py 4.58 KB
Newer Older
1
# core django imports
Daniel W Bond's avatar
Daniel W Bond committed
2
from django.views.generic import DetailView, ListView
3 4
# third party imports
from braces.views import LoginRequiredMixin
Daniel W Bond's avatar
Daniel W Bond committed
5
# imports from your apps
6
from .models import Building, Floor, Room
7
from accounts.models import Student
8

9

10 11 12
# a list of neighborhoods and their buildings
class ListBuildings(LoginRequiredMixin, ListView):
    model = Building
13
    queryset = Building.objects.all()
Daniel W Bond's avatar
Daniel W Bond committed
14
    # paginate_by
15 16 17 18
    context_object_name = 'buildings'
    template_name = 'list_buildings.html'

    login_url = 'login'
19

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

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

Daniel W Bond's avatar
Daniel W Bond committed
42

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

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

        # requesting_student = Student.objects.get(user=self.request.user)
Daniel W Bond's avatar
Daniel W Bond committed
53 54
        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
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

Daniel W Bond's avatar
Daniel W Bond committed
94
        # requesting_student = Student.objects.get(user=self.request.user)
95 96
        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
97 98 99 100

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

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

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

123 124 125
        return context

    login_url = '/'
126

Daniel W Bond's avatar
Daniel W Bond committed
127

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