views.py 4.38 KB
Newer Older
1 2
from django.views.generic import DetailView, ListView, CreateView, UpdateView, DeleteView

Daniel W Bond's avatar
Daniel W Bond committed
3
from housing.models import Building, Floor, Room
4
from accounts.models import Student
5

6 7
from braces.views import LoginRequiredMixin

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

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

20 21 22
# building floors, other information
class DetailBuilding(LoginRequiredMixin, DetailView):
    model = Building
23
    context_object_name = 'building'
24 25 26 27
    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
28
        context['floors'] = Floor.objects.filter(building__name=''+self.get_object().name).order_by('number')
29
        return context
30

31 32 33
    login_url = '/'

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

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

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

70
        context['students'] = floor_students
71 72
        context['notOnFloor'] = not onFloor()
        context['notInBuilding'] = not inBuilding()
73 74
        return context

75 76 77 78 79 80
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
81
        context = super(DetailRoom, self).get_context_data(**kwargs)
Daniel W Bond's avatar
Daniel W Bond committed
82

83 84 85
        #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
86 87 88 89

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

108
        context['students'] = students
Daniel W Bond's avatar
Daniel W Bond committed
109 110 111
        context['notOnFloor'] = not onFloor()
        context['notInBuilding'] = not inBuilding()

112 113 114
        return context

    login_url = '/'
115

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