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

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