views.py 4.14 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 15 16 17 18 19
    def get_context_data(self, **kwargs):
        context = super(ListBuildings, self).get_context_data(**kwargs)
        context['rappahannock'] = Building.objects.filter(neighbourhood='ra')
        context['shenandoah'] = Building.objects.filter(neighbourhood='sh')
        context['aquia'] = Building.objects.filter(neighbourhood='aq')
        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
        return context

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

81 82 83
        #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
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105

        # if self.request.user is on the floor
        def onFloor():
            floor_status = False
            if requesting_student.get_floor == self.get_object().floor:
                floor_status = True
            return floor_status

        # if self.request.user is in the building
        def inBuilding():
            building_status = False
            if requesting_student.get_building == self.get_object().get_building:
                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()

106 107 108 109
        context['students'] = students
        return context

    login_url = '/'
110

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