Commit ad63dc10 authored by Daniel W Bond's avatar Daniel W Bond

this does little to reduce complexity, but it's marginally more readable to go...

this does little to reduce complexity, but it's marginally more readable to go building by building rather than room by room
parent 9840db18
......@@ -17,7 +17,7 @@ from cas.views import login as cas_login
from ratelimit.decorators import ratelimit
# imports from your apps
from .models import Student, Major, Confirmation
from housing.models import Room
from housing.models import Building
from .forms import StudentUpdateForm
from .student_messages import return_messages
......@@ -320,31 +320,36 @@ class DetailMajor(LoginRequiredMixin, DetailView):
context = super(DetailMajor, self).get_context_data(**kwargs)
requesting_student = Student.objects.get(user=self.request.user)
# retrieve every room that has a student with the major in question
# all students in the major
major_students = Student.objects.filter(major__in=[self.get_object()]).order_by('user__username')
neighbourhoods = ("aq", "ra", "sh")
visible_by_neighbourhood = {}
for neighbourhood in neighbourhoods:
rooms = [
room
for room in Room.objects.filter(floor__building__neighbourhood=neighbourhood)
if room.student_set.filter(major__in=[self.get_object()])
]
# print(neighbourhood)
# grab all buildings in each neighborhood
buildings_by_neighbourhood = Building.objects.filter(neighbourhood=neighbourhood)
# print(buildings_by_neighbourhood)
# 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
#
# go building by building and assess if the students in that building
# with the current major are visible to the requesting student
# chain is a function that flattens arrays, and is neccesary when combining
# students from each building
# we sort each of the lists of students by their username
# as elsewhere, this is imperfect if a student changes their display name
# this is necessary as a separate step because .visible returns a list type
# note we're using '.' instead of '__', because who likes syntactical consistency
visible_by_neighbourhood[neighbourhood] = sorted(list(chain(*[
Student.objects.visible(requesting_student, room)
for room in rooms
major_students.visible(requesting_student, building)
for building in buildings_by_neighbourhood
])), key=attrgetter('user.username'))
# see what students are left over (aren't visible)
hidden = set(Student.objects.filter(major__in=[self.get_object()]).order_by('user__username'))
# print(hidden)
hidden = set(major_students)
for visible in visible_by_neighbourhood.values():
# print('visible', visible)
hidden = hidden.difference(set(visible))
......
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