Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
SRCT
roomlist
Commits
e3ef8a21
Commit
e3ef8a21
authored
Dec 30, 2015
by
Luke W Faraone
Browse files
Refactor get_context_data to remove verbosity
parent
b17de2c7
Changes
1
Hide whitespace changes
Inline
Side-by-side
roomlist/accounts/views.py
View file @
e3ef8a21
...
@@ -531,53 +531,35 @@ class DetailMajor(LoginRequiredMixin, DetailView):
...
@@ -531,53 +531,35 @@ 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
# retrieve every room that has a student with the major in question
aq_rooms
=
[
neighbourhoods
=
(
"aq"
,
"ra"
,
"sh"
)
room
visible_by_neighbourhood
=
{}
for
room
in
Room
.
objects
.
filter
(
floor__building__neighbourhood
=
'aq'
)
for
neighbourhood
in
neighbourhoods
:
if
room
.
student_set
.
filter
(
major
=
self
.
get_object
())
rooms
=
[
]
room
ra_rooms
=
[
for
room
in
Room
.
objects
.
filter
(
floor__building__neighbourhood
=
neighbourhood
)
room
if
room
.
student_set
.
filter
(
major
=
self
.
get_object
())
for
room
in
Room
.
objects
.
filter
(
floor__building__neighbourhood
=
'ra'
)
]
if
room
.
student_set
.
filter
(
major
=
self
.
get_object
())
]
# identify if the student(s) in that room are visible to the requesting student
sh_rooms
=
[
# 'chain' is necessary if there are multiple students in one room with the same major
room
#
for
room
in
Room
.
objects
.
filter
(
floor__building__neighbourhood
=
'sh'
)
# we sort each of the lists of students by their username
if
room
.
student_set
.
filter
(
major
=
self
.
get_object
())
# 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
# identify if the student(s) in that room are visible to the requesting student
visible_by_neighbourhood
[
neighbourhood
]
=
sorted
(
list
(
chain
(
*
[
# 'chain' is necessary if there are multiple students in one room with the same major
Student
.
objects
.
visible
(
requesting_student
,
room
)
aq_visible
=
list
(
chain
(
*
[
for
room
in
rooms
Student
.
objects
.
visible
(
requesting_student
,
room
)
])),
key
=
attrgetter
(
'user.username'
))
for
room
in
aq_rooms
]))
ra_visible
=
list
(
chain
(
*
[
Student
.
objects
.
visible
(
requesting_student
,
room
)
for
room
in
ra_rooms
]))
sh_visible
=
list
(
chain
(
*
[
Student
.
objects
.
visible
(
requesting_student
,
room
)
for
room
in
sh_rooms
]))
# see what students are left over (aren't visible)
# see what students are left over (aren't visible)
everyone
=
Student
.
objects
.
filter
(
major
=
self
.
get_object
()).
order_by
(
'user__username'
)
hidden
=
Student
.
objects
.
filter
(
major
=
self
.
get_object
()).
order_by
(
'user__username'
)
hidden
=
list
(((
set
(
everyone
)
-
set
(
aq_visible
))
-
set
(
ra_visible
))
-
set
(
sh_visible
))
for
visible
in
neighbourhoods
.
values
():
hidden
-=
set
(
visible
)
# sort each of the lists of students by their username
# as elsewhere, this is imperfect if a student changes their display name
for
neighbourhood
,
visible
in
neighbourhoods
.
items
():
# this is necessary as a separate step because .visible returns a list type
context
[
'%s_location_visible'
%
neighbourhood
]
=
visible
# note we're using '.' instead of '__', because who likes syntactical consistency
sorted_aq_visible
=
sorted
(
aq_visible
,
key
=
attrgetter
(
'user.username'
))
sorted_ra_visible
=
sorted
(
ra_visible
,
key
=
attrgetter
(
'user.username'
))
sorted_sh_visible
=
sorted
(
sh_visible
,
key
=
attrgetter
(
'user.username'
))
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
context
[
'location_hidden'
]
=
hidden
return
context
return
context
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment