views.py 3.71 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
34 35 36
class DetailFloor(LoginRequiredMixin, DetailView):
    model = Floor
    context_object_name = 'floor'
Daniel W Bond's avatar
Daniel W Bond committed
37
    template_name = 'detail_floor.html'
38

39 40 41
    requesting_student = Student.objects.filter(user=self.request.user)

    # if self.request.user is on the floor
42
    def onFloor():
43 44 45 46
        floor_status = False
        if requesting_student.get_floor == self.get_object():
            floor_status = True
        return floor_status
47

48
    # if self.request.user is in the building
49
    def inBuilding():
50 51 52 53
        building_status = False
        if requesting_student.get_building == self.get_object().building:
            building_status = True
        return building_status
54 55 56 57 58 59 60 61 62 63 64

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

65
    def get_context_data(self, **kwargs):
Daniel W Bond's avatar
Daniel W Bond committed
66
        context = super(DetailFloor, self).get_context_data(**kwargs)
67
        context['students'] = floor_students
68 69
        return context

70 71
    login_url = '/'

72 73 74 75 76
class DetailRoom(LoginRequiredMixin, DetailView):
    model = Room
    context_object_name = 'room'
    template_name='detailBuilding.html'

77 78 79
    requesting_student = Student.objects.filter(user=self.request.user)

    # if self.request.user is on the floor
80
    def onFloor():
81 82 83 84
        floor_status = False
        if requesting_student.get_floor == self.get_object().floor:
            floor_status = True
        return floor_status
85

86
    # if self.request.user is in the building
87
    def inBuilding():
88 89 90 91
        building_status = False
        if requesting_student.get_building == self.get_object().get_building:
            building_status = True
        return building_status
92 93 94 95 96 97 98 99 100

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

    def get_context_data(self, **kwargs):
Daniel W Bond's avatar
Daniel W Bond committed
101
        context = super(DetailRoom, self).get_context_data(**kwargs)
102 103 104 105
        context['students'] = students
        return context

    login_url = '/'
106

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