views.py 3.86 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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
    
        requesting_student = Student.objects.filter(user=self.request.user)

        # if self.request.user is on the floor
        def onFloor():
            floor_status = False
            if requesting_student.get_floor == self.get_object():
                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().building:
                building_status = True
            return building_status

        rooms = Room.objects.filter(floor=self.get_object()).order_by('-number')
        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())

68
        context['students'] = floor_students
69 70
        return context

71 72 73 74 75 76
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
77
        context = super(DetailRoom, self).get_context_data(**kwargs)
Daniel W Bond's avatar
Daniel W Bond committed
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102

        requesting_student = Student.objects.filter(user=self.request.user)


        # 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()

103 104 105 106
        context['students'] = students
        return context

    login_url = '/'
107

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