views.py 4.62 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
# a list of neighborhoods and their buildings
class ListBuildings(LoginRequiredMixin, ListView):
    model = Building
14 15 16 17 18 19
    queryset = Building.objects.all()
    # paginate_by = 
    context_object_name = 'buildings'
    template_name = 'list_buildings.html'

    login_url = 'login'
20

Daniel W Bond's avatar
Daniel W Bond committed
21 22
    def get_context_data(self, **kwargs):
        context = super(ListBuildings, self).get_context_data(**kwargs)
Daniel W Bond's avatar
Daniel W Bond committed
23 24 25
        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
26 27
        return context

28 29 30
# building floors, other information
class DetailBuilding(LoginRequiredMixin, DetailView):
    model = Building
31
    slug_field = 'slug__iexact'
32
    context_object_name = 'building'
33 34 35
    template_name='detail_building.html'

    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

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

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

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

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

84 85 86
class DetailRoom(LoginRequiredMixin, DetailView):
    model = Room
    context_object_name = 'room'
87
    template_name='detail_room.html'
88 89

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

92 93 94
        #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
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 111 112 113 114 115 116
                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()

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

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