Commit c033a177 authored by Daniel W Bond's avatar Daniel W Bond
Browse files

used new .visible(student, housing) queryset on DetailMajor view

parent e135951d
# standard library imports # standard library imports
from __future__ import absolute_import, print_function from __future__ import absolute_import, print_function
import random import random
from operator import attrgetter
from itertools import chain
# core django imports # core django imports
from django.shortcuts import get_object_or_404 from django.shortcuts import get_object_or_404
from django.http import HttpResponseForbidden from django.http import HttpResponseForbidden
...@@ -527,67 +529,56 @@ class DetailMajor(LoginRequiredMixin, DetailView): ...@@ -527,67 +529,56 @@ class DetailMajor(LoginRequiredMixin, DetailView):
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
context = super(DetailMajor, self).get_context_data(**kwargs) context = super(DetailMajor, self).get_context_data(**kwargs)
me = Student.objects.get(user=self.request.user) requesting_student = Student.objects.get(user=self.request.user)
students = Student.objects.filter(major=self.get_object()).order_by('room__floor__building__name', 'user__last_name', 'user__first_name')
def onFloor(me, student):
floor_status = False
if me.get_floor() == student.get_floor():
floor_status = True
return floor_status
def inBuilding(me, student):
floor_status = False
if me.get_building() == student.get_building():
floor_status = True
return floor_status
aq_location_visible = [] # retrieve every room that has a student with the major in question
ra_location_visible = [] aq_rooms = [
sh_location_visible = [] room
location_hidden = [] for room in Room.objects.filter(floor__building__neighbourhood='aq')
if room.student_set.filter(major=self.get_object())
aq_students = students.filter(room__floor__building__neighbourhood='aq') ]
ra_rooms = [
for student in aq_students: room
if student.privacy == u'students': for room in Room.objects.filter(floor__building__neighbourhood='ra')
aq_location_visible.append(student) if room.student_set.filter(major=self.get_object())
elif (student.privacy == u'building') and inBuilding(me, student): ]
aq_location_visible.append(student) sh_rooms = [
elif (student.privacy == u'floor') and onFloor(me, student): room
aq_location_visible.append(student) for room in Room.objects.filter(floor__building__neighbourhood='sh')
else: if room.student_set.filter(major=self.get_object())
location_hidden.append(student) ]
ra_students = students.filter(room__floor__building__neighbourhood='ra') # identify if the student(s) in that room are visible to the requesting student
# 'chain' is necessary if there are multiple students in one room with the same major
for student in ra_students: aq_visible = list(chain(*[
if student.privacy == u'students': Student.objects.visible(requesting_student, room)
ra_location_visible.append(student) for room in aq_rooms
elif (student.privacy == u'building') and inBuilding(me, student): ]))
ra_location_visible.append(student) ra_visible = list(chain(*[
elif (student.privacy == u'floor') and onFloor(me, student): Student.objects.visible(requesting_student, room)
ra_location_visible.append(student) for room in ra_rooms
else: ]))
location_hidden.append(student) sh_visible = list(chain(*[
Student.objects.visible(requesting_student, room)
sh_students = students.filter(room__floor__building__neighbourhood='sh') for room in sh_rooms
for student in sh_students:
if student.privacy == u'students': # see what students are left over (aren't visible)
sh_location_visible.append(student) everyone = Student.objects.filter(major=self.get_object()).order_by('user__username')
elif (student.privacy == u'building') and inBuilding(me, student): hidden = list(((set(everyone) - set(aq_visible)) - set(ra_visible)) - set(sh_visible))
elif (student.privacy == u'floor') and onFloor(me, student): # sort each of the lists of students by their username
sh_location_visible.append(student) # as elsewhere, this is imperfect if a student changes their display name
else: # this is necessary as a separate step because .visible returns a list type
location_hidden.append(student) # note we're using '.' instead of '__', because who likes syntactical consistency
sorted_aq_visible = sorted(aq_visible, key=attrgetter('user.username'))
context['aq_location_visible'] = aq_location_visible sorted_ra_visible = sorted(ra_visible, key=attrgetter('user.username'))
context['ra_location_visible'] = ra_location_visible sorted_sh_visible = sorted(sh_visible, key=attrgetter('user.username'))
context['sh_location_visible'] = sh_location_visible
context['location_hidden'] = location_hidden context['aq_location_visible'] = sorted_aq_visible
context['ra_location_visible'] = sorted_ra_visible
context['sh_location_visible'] = sorted_sh_visible
context['location_hidden'] = hidden
return context return context
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment