Commit 1b093d66 authored by Daniel W Bond's avatar Daniel W Bond
Browse files

created new 'visible' queryset for Students to return everyone a student can...

created new 'visible' queryset for Students to return everyone a student can see in a Room, Floor, or Building
parent 0b8b9b62
...@@ -16,7 +16,7 @@ from randomslugfield import RandomSlugField ...@@ -16,7 +16,7 @@ from randomslugfield import RandomSlugField
from multiselectfield import MultiSelectField from multiselectfield import MultiSelectField
from allauth.socialaccount.models import SocialAccount from allauth.socialaccount.models import SocialAccount
# imports from your apps # imports from your apps
from housing.models import Room from housing.models import Room, Floor, Building
class Major(TimeStampedModel): class Major(TimeStampedModel):
...@@ -71,6 +71,28 @@ class StudentQuerySet(models.query.QuerySet): ...@@ -71,6 +71,28 @@ class StudentQuerySet(models.query.QuerySet):
return list(floor) + list(set(building_students) - set(floor)) return list(floor) + list(set(building_students) - set(floor))
def visible(self, student, housing):
if isinstance(housing, Room):
rooms = [housing]
elif isinstance(housing, Floor):
rooms = Room.objects.filter(floor=housing).order_by('number')
elif isinstance(housing, Building):
rooms = Room.objects.filter(floor__building=housing).order_by('floor__building__name', 'number')
else:
raise TypeError("'housing' arg must be Building, Floor, or Room")
visible_students = []
for room in rooms:
if student in room.floor:
visible_students.extend(self.filter(room=room).floor_building_students())
elif student in room.floor.building:
visible_students.extend(self.filter(room=room).building_students())
else:
visible_students.extend(self.filter(room=room).students())
return visible_students
class StudentManager(models.Manager): class StudentManager(models.Manager):
...@@ -94,6 +116,9 @@ class StudentManager(models.Manager): ...@@ -94,6 +116,9 @@ class StudentManager(models.Manager):
def floor_building_students(self): def floor_building_students(self):
return self.get_queryset().floor_building_students() return self.get_queryset().floor_building_students()
def visible(self, student, housing):
return self.get_queryset().visible(student, housing)
class Student(TimeStampedModel): class Student(TimeStampedModel):
user = models.OneToOneField(User) user = models.OneToOneField(User)
......
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