views.py 2.72 KB
Newer Older
1 2
# standard library imports
from __future__ import absolute_import, print_function
3
# core django imports
Daniel W Bond's avatar
Daniel W Bond committed
4
from django.views.generic import DetailView, ListView
5 6
# third party imports
from braces.views import LoginRequiredMixin
Daniel W Bond's avatar
Daniel W Bond committed
7
# imports from your apps
8
from .models import Building, Floor, Room
9
from accounts.models import Student
10

11

12 13 14
# a list of neighborhoods and their buildings
class ListBuildings(LoginRequiredMixin, ListView):
    model = Building
15
    queryset = Building.objects.all()
Daniel W Bond's avatar
Daniel W Bond committed
16
    # paginate_by
17 18 19 20
    context_object_name = 'buildings'
    template_name = 'list_buildings.html'

    login_url = 'login'
21

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

Daniel W Bond's avatar
Daniel W Bond committed
29

30 31 32
# building floors, other information
class DetailBuilding(LoginRequiredMixin, DetailView):
    model = Building
33
    slug_field = 'slug__iexact'
34
    context_object_name = 'building'
Daniel W Bond's avatar
Daniel W Bond committed
35
    template_name = 'detail_building.html'
36 37

    login_url = 'login'
38 39 40

    def get_context_data(self, **kwargs):
        context = super(DetailBuilding, self).get_context_data(**kwargs)
41
        context['floors'] = Floor.objects.filter(building=self.get_object()).order_by('number')
42
        return context
43

Daniel W Bond's avatar
Daniel W Bond committed
44

45
# this lists the rooms on the floor
Daniel W Bond's avatar
Daniel W Bond committed
46
class DetailFloor(LoginRequiredMixin, DetailView):
47 48
    model = Floor
    context_object_name = 'floor'
Daniel W Bond's avatar
Daniel W Bond committed
49
    template_name = 'detail_floor.html'
Daniel W Bond's avatar
Daniel W Bond committed
50

51
    def get_context_data(self, **kwargs):
Daniel W Bond's avatar
Daniel W Bond committed
52
        context = super(DetailFloor, self).get_context_data(**kwargs)
Daniel W Bond's avatar
Daniel W Bond committed
53

54
        requesting_student = Student.objects.get(user=self.request.user)
Daniel W Bond's avatar
Daniel W Bond committed
55

56 57 58
        context['students'] = Student.objects.visible(requesting_student, self.get_object())
        context['notOnFloor'] = not(requesting_student in self.get_object())
        context['notInBuilding'] = not(requesting_student in self.get_object().building)
59 60
        return context

Daniel W Bond's avatar
Daniel W Bond committed
61

62 63 64
class DetailRoom(LoginRequiredMixin, DetailView):
    model = Room
    context_object_name = 'room'
Daniel W Bond's avatar
Daniel W Bond committed
65
    template_name = 'detail_room.html'
66 67

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

70
        requesting_student = Student.objects.get(user=self.request.user)
Daniel W Bond's avatar
Daniel W Bond committed
71

72 73 74
        context['students'] = Student.objects.visible(requesting_student, self.get_object())
        context['notOnFloor'] = not(requesting_student in self.get_object().floor)
        context['notInBuilding'] = not(requesting_student in self.get_object().floor.building)
Daniel W Bond's avatar
Daniel W Bond committed
75

76
        return context