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

merge commit for querysets into amherst

parents 2fbcc4ad 48c53e83
......@@ -103,7 +103,7 @@ Though you can use an existing user to access this database, here's how to creat
For local development, password strength is less important, but use a strong passphrase for deployment. You can choose a different username.
``GRANT ALL ON roomlist.* TO 'roommate'@'localhost';``
This allows your database user to create all the tables it needs on the bookshare database. (Each model in each app's models.py is a separate table, and each attribute a column, and each instance a row.)
This allows your database user to create all the tables it needs on the roomlist database. (Each model in each app's models.py is a separate table, and each attribute a column, and each instance a row.)
``GRANT ALL ON test_roomlist.* TO 'roommate'@'localhost';`` ``FLUSH PRIVILEGES;``
When running test cases, django creates a test database so your 'real' database doesn't get screwed up. This database is called 'test_' + whatever your normal database is named. Note that for permissions it doesn't matter that this database hasn't yet been created.
......
......@@ -16,7 +16,7 @@ from randomslugfield import RandomSlugField
from multiselectfield import MultiSelectField
from allauth.socialaccount.models import SocialAccount
# imports from your apps
from housing.models import Room
from housing.models import Room, Floor, Building
class Major(TimeStampedModel):
......@@ -71,6 +71,28 @@ class StudentQuerySet(models.query.QuerySet):
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('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):
......@@ -94,6 +116,9 @@ class StudentManager(models.Manager):
def floor_building_students(self):
return self.get_queryset().floor_building_students()
def visible(self, student, housing):
return self.get_queryset().visible(student, housing)
class Student(TimeStampedModel):
user = models.OneToOneField(User)
......
# standard library imports
from __future__ import absolute_import, print_function
# core django imports
from django.test import Client, TestCase
from django.contrib.auth.models import User
# imports from your apps
from accounts.models import Student, Major
from housing.models import Building, Floor, Room
class MajorTest(TestCase):
def setUp(self):
Major.objects.create(name='Government and International Politics, BA')
def test_major_first_letter(self):
govt = Major.objects.get(name='Government and International Politics, BA')
self.assertEqual(govt.first_letter(), 'G')
class StudentTest(TestCase):
def setUp(self):
global wilson
global wilson_third
global wilson_313
wilson = Building.objects.create(name='Wilson', neighbourhood='sh', campus='ff')
wilson_third = Floor.objects.create(building=wilson, number=3)
wilson_313 = Room.objects.create(floor=wilson_third, number=313)
wilson_307 = Room.objects.create(floor=wilson_third, number=307)
wilson_first = Floor.objects.create(building=wilson, number=1)
wilson_102 = Room.objects.create(floor=wilson_first, number=102)
wilson.save()
wilson_third.save()
wilson_313.save()
wilson_307.save()
wilson_first.save()
wilson_102.save()
harrison = Building.objects.create(name='Harrison', neighbourhood='sh', campus='ff')
harrison_second = Floor.objects.create(building=harrison, number=2)
harrison_207 = Room.objects.create(floor=harrison_second, number=207)
harrison.save()
harrison_second.save()
harrison_207.save()
gmason = User.objects.create_user(username='gmason',
first_name='George',
last_name='Mason',
email='gmason@masonlive.gmu.edu',
password='eagle_bank')
tjefferson = User.objects.create_user(username='tjefferson',
first_name='Thomas',
last_name='Jefferson',
email='tjefferson@masonlive.gmu.edu',
password='louisiana')
dmadison = User.objects.create_user(username='dmadison',
# note lack of first or last name!
email='dmadison@masonlive.gmu.edu',
password='white_house')
jtyler = User.objects.create_user(username='jtyler',
first_name='John',
last_name='Tyler',
email='jtyler@masonlive.gmu.edu',
password='canhaztexas')
hclinton = User.objects.create_user(username='hclinton',
first_name='Hillary',
last_name='Clinton',
email='hildawg@mytotallynotsketchysite.biz',
password='')
spalin = User.objects.create_user(username='spalin',
first_name='Sarah',
last_name='Palin',
email='grizzlymama@mccain08.com',
password='youbetcha')
cfiorina = User.objects.create_user(username='cfiorina',
first_name='Carly',
last_name='Fiorina',
email='carly@hp.com',
password='lookatthisface')
# create_user includes .save()
global george
global dolley
global thomas
global sarah
global hillary
global john
global carly
george = Student.objects.create(user=gmason, room=wilson_313, privacy=u'floor')
thomas = Student.objects.create(user=tjefferson, room=wilson_313, privacy=u'building')
dolley = Student.objects.create(user=dmadison, room=harrison_207, privacy=u'floor')
sarah = Student.objects.create(user=spalin, room=wilson_307, privacy=u'building')
hillary = Student.objects.create(user=hclinton, room=wilson_102, privacy=u'students')
john = Student.objects.create(user=jtyler, room=wilson_313, privacy=u'students')
carly = Student.objects.create(user=cfiorina, privacy=u'students') # note lack of room!
george.save()
thomas.save()
dolley.save()
sarah.save()
hillary.save()
john.save()
carly.save()
def test_recent_changes(self):
self.assertEqual(george.recent_changes(), 0)
def test_totally_done(self):
# one assert per test?
self.assertEqual(george.totally_done(), False)
george.completedName = True
self.assertEqual(george.totally_done(), False)
george.completedPrivacy = True
self.assertEqual(george.totally_done(), False)
george.completedMajor = True
self.assertEqual(george.totally_done(), False)
george.completedSocial = True
self.assertEqual(george.totally_done(), True)
def test_get_flag_count(self):
self.assertEqual(george.get_flag_count(), 0)
def test_get_first_name_or_uname(self):
self.assertEqual(george.get_first_name_or_uname(), 'George')
self.assertEqual(dolley.get_first_name_or_uname(), 'dmadison')
def test_get_last_name_or_uname(self):
self.assertEqual(george.get_last_name_or_uname(), 'Mason')
self.assertEqual(dolley.get_last_name_or_uname(), 'dmadison')
def test_get_full_name_or_uname(self):
self.assertEqual(george.get_full_name_or_uname(), 'George Mason')
self.assertEqual(dolley.get_full_name_or_uname(), 'dmadison')
def test_qset_floor(self):
students = Student.objects.all().floor()
self.assertEqual(len(students), 2)
self.assertTrue(george in students)
self.assertTrue(dolley in students)
def test_qset_building(self):
students = Student.objects.all().building()
self.assertEqual(len(students), 2)
self.assertTrue(thomas in students)
self.assertTrue(sarah in students)
def test_qset_students(self):
students = Student.objects.all().students()
self.assertEqual(len(students), 3)
self.assertTrue(hillary in students)
self.assertTrue(john in students)
self.assertTrue(carly in students)
def test_qset_building_students(self):
students = Student.objects.all().building_students()
self.assertEqual(len(students), 5)
self.assertTrue(thomas in students)
self.assertTrue(sarah in students)
self.assertTrue(john in students)
self.assertTrue(hillary in students)
self.assertTrue(carly in students)
def test_qset_floor_building_students(self):
students = Student.objects.all().floor_building_students()
self.assertEqual(len(students), 7)
self.assertTrue(george in students)
self.assertTrue(dolley in students)
self.assertTrue(thomas in students)
self.assertTrue(sarah in students)
self.assertTrue(hillary in students)
self.assertTrue(john in students)
self.assertTrue(carly in students)
def test_visible_room_same_room(self):
students = Student.objects.visible(student=george, housing=wilson_313)
self.assertEqual(len(students), 3)
self.assertTrue(george in students) # george's privacy is floor
self.assertTrue(thomas in students) # thomas's privacy is building
self.assertTrue(john in students) # john's privacy is students
def test_visible_room_same_floor(self):
students = Student.objects.visible(student=sarah, housing=wilson_313)
self.assertEqual(len(students), 3)
self.assertTrue(george in students)
self.assertTrue(thomas in students)
self.assertTrue(john in students)
def test_visible_room_different_floor_same_building(self):
students = Student.objects.visible(student=hillary, housing=wilson_313)
self.assertEqual(len(students), 2)
self.assertTrue(thomas in students)
self.assertTrue(john in students)
def test_visible_room_different_building(self):
students = Student.objects.visible(student=dolley, housing=wilson_313)
self.assertEqual(len(students), 1)
self.assertTrue(john in students)
def test_visible_floor_same_floor(self):
students = Student.objects.visible(student=sarah, housing=wilson_third)
self.assertEqual(len(students), 4)
self.assertTrue(george in students)
self.assertTrue(thomas in students)
self.assertTrue(john in students)
self.assertTrue(sarah in students)
def test_visible_floor_different_floor_same_building(self):
students = Student.objects.visible(student=hillary, housing=wilson_third)
self.assertEqual(len(students), 3)
self.assertTrue(thomas in students)
self.assertTrue(john in students)
self.assertTrue(sarah in students)
def test_visible_floor_different_building(self):
students = Student.objects.visible(student=dolley, housing=wilson_third)
self.assertEqual(len(students), 1)
self.assertTrue(john in students)
def test_visible_building_same_building(self):
students = Student.objects.visible(student=hillary, housing=wilson)
self.assertEqual(len(students), 4)
self.assertTrue(thomas in students)
self.assertTrue(john in students)
self.assertTrue(sarah in students)
self.assertTrue(hillary in students)
def test_visible_building_different_building(self):
students = Student.objects.visible(student=dolley, housing=wilson)
self.assertEqual(len(students), 2)
self.assertTrue(john in students)
self.assertTrue(hillary in students)
def test_visible_none_room(self):
students = Student.objects.visible(student=carly, housing=wilson_313)
self.assertEqual(len(students), 1)
self.assertTrue(john in students)
def test_visible_none_floor(self):
students = Student.objects.visible(student=carly, housing=wilson_third)
self.assertEqual(len(students), 1)
self.assertTrue(john in students)
def test_visible_none_building(self):
students = Student.objects.visible(student=carly, housing=wilson)
self.assertEqual(len(students), 2)
self.assertTrue(john in students)
self.assertTrue(hillary in students)
# standard library imports
from __future__ import absolute_import, print_function
# core django imports
from django.test import TestCase
# Create your tests here.
......@@ -2,6 +2,8 @@
from __future__ import absolute_import, print_function
import random
from distutils.util import strtobool
from operator import attrgetter
from itertools import chain
# core django imports
from django.shortcuts import get_object_or_404
from django.http import HttpResponseForbidden, HttpResponseRedirect
......@@ -563,67 +565,43 @@ class DetailMajor(LoginRequiredMixin, DetailView):
def get_context_data(self, **kwargs):
context = super(DetailMajor, self).get_context_data(**kwargs)
me = 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
requesting_student = Student.objects.get(user=self.request.user)
aq_location_visible = []
ra_location_visible = []
sh_location_visible = []
location_hidden = []
aq_students = students.filter(room__floor__building__neighbourhood='aq')
for student in aq_students:
if student.privacy == u'students':
aq_location_visible.append(student)
elif (student.privacy == u'building') and inBuilding(me, student):
aq_location_visible.append(student)
elif (student.privacy == u'floor') and onFloor(me, student):
aq_location_visible.append(student)
else:
location_hidden.append(student)
ra_students = students.filter(room__floor__building__neighbourhood='ra')
for student in ra_students:
if student.privacy == u'students':
ra_location_visible.append(student)
elif (student.privacy == u'building') and inBuilding(me, student):
ra_location_visible.append(student)
elif (student.privacy == u'floor') and onFloor(me, student):
ra_location_visible.append(student)
else:
location_hidden.append(student)
sh_students = students.filter(room__floor__building__neighbourhood='sh')
for student in sh_students:
if student.privacy == u'students':
sh_location_visible.append(student)
elif (student.privacy == u'building') and inBuilding(me, student):
sh_location_visible.append(student)
elif (student.privacy == u'floor') and onFloor(me, student):
sh_location_visible.append(student)
else:
location_hidden.append(student)
context['aq_location_visible'] = aq_location_visible
context['ra_location_visible'] = ra_location_visible
context['sh_location_visible'] = sh_location_visible
context['location_hidden'] = location_hidden
# retrieve every room that has a student with the major in question
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=self.get_object())
]
# 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
#
# 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
])), key=attrgetter('user.username'))
# print(visible_by_neighbourhood)
# see what students are left over (aren't visible)
hidden = set(Student.objects.filter(major=self.get_object()).order_by('user__username'))
# print(hidden)
for visible in visible_by_neighbourhood.values():
# print('visible', visible)
hidden = hidden.difference(set(visible))
# print(hidden)
for neighbourhood, visible in visible_by_neighbourhood.iteritems():
context['%s_location_visible' % neighbourhood] = visible
context['location_hidden'] = hidden
return context
......
......@@ -56,6 +56,26 @@ class Building(TimeStampedModel):
def __unicode__(self): # __unicode__ on Python 2
return unicode(self.name)
def __contains__(self, obj):
if isinstance(obj, Floor):
if obj.building == self: # floor.building == building
return True
else:
return False
elif isinstance(obj, Room):
if obj.floor.building == self: # room.floor.building == building
return True
else:
return False
else:
try:
if obj.room.floor.building == self: # student.room.floor.building == building
return True
else:
return False
except:
return False
class Meta:
ordering = ['name']
......@@ -78,6 +98,21 @@ class Floor(TimeStampedModel):
def __str__(self): # __unicode__ on Python 2
return self.building.__str__()+" "+self.number.__str__()
def __contains__(self, obj): # circular imports with Student
if isinstance(obj, Room):
if obj.floor == self: # room.floor == room
return True
else:
return False
else:
try:
if obj.room.floor == self: # student.room.floor == floor
return True
else:
return False
except:
return False
class Meta:
ordering = ['building', 'number']
......@@ -101,5 +136,14 @@ class Room(TimeStampedModel):
def __str__(self): # __unicode__ on Python 2
return self.floor.building.__str__()+" "+self.number.__str__()
def __contains__(self, obj): # circular imports with Student
try:
if obj.room == self: # student.room == room
return True
else:
return False
except:
return False
class Meta:
ordering = ['number']
# standard library imports
from __future__ import absolute_import, print_function
# core django imports
from django.test import TestCase
from django.contrib.auth.models import User
# imports from your apps
from accounts.models import Student
from housing.models import Building, Floor, Room
class BuildingTest(TestCase):
def setUp(self):
global wilson
global wilson_third
global wilson_313
wilson = Building.objects.create(name='Wilson', neighbourhood='sh', campus='ff')
wilson_third = Floor.objects.create(building=wilson, number=3)
wilson_313 = Room.objects.create(floor=wilson_third, number=313)
wilson.save()
wilson_third.save()
wilson_313.save()
harrison = Building.objects.create(name='Harrison', neighbourhood='sh', campus='ff')
harrison_third = Floor.objects.create(building=harrison, number=3)
harrison_313 = Room.objects.create(floor=harrison_third, number=313)
global harrison
global harrison_third
global harrison_313
harrison.save()
harrison_third.save()
harrison_313.save()
gmason = User.objects.create_user(username='gmason',
first_name='George',
last_name='Mason',
email='gmason@masonlive.gmu.edu',
password='eagle_bank')
global george
george = Student.objects.create(user=gmason, room=wilson_313)
george.save()
def test_building_contains_room(self):
self.assertTrue(wilson_313 in wilson)
self.assertFalse(harrison_313 in wilson)
def test_building_contains_floor(self):
self.assertTrue(wilson_third in wilson)
self.assertFalse(harrison_third in wilson)
def test_building_contains_student(self):
self.assertTrue(george in wilson)
self.assertFalse(george in harrison)
class FloorTest(TestCase):
def test_floor_contains_room(self):
self.assertTrue(wilson_313 in wilson_third)
self.assertFalse(harrison_313 in wilson_third)
def test_floor_contains_student(self):
self.assertTrue(george in wilson_third)
self.assertFalse(george in harrison_third)
class RoomTest(TestCase):
def test_room_contains_student(self):
self.assertTrue(george in wilson_313)
self.assertFalse(george in harrison_313)
......@@ -38,7 +38,7 @@ class DetailBuilding(LoginRequiredMixin, DetailView):
def get_context_data(self, **kwargs):
context = super(DetailBuilding, self).get_context_data(**kwargs)
context['floors'] = Floor.objects.filter(building__name=''+self.get_object().name).order_by('number')
context['floors'] = Floor.objects.filter(building=self.get_object()).order_by('number')
return context
......@@ -53,33 +53,9 @@ class DetailFloor(LoginRequiredMixin, DetailView):
requesting_student = Student.objects.get(user=self.request.user)
# if self.request.user is on the floor
def onFloor():
floor_status = False
if requesting_student.get_floor() == self.get_object():
floor_status = True
return floor_status
# if self.request.user is in the building
def inBuilding():
building_status = False
if requesting_student.get_building() == self.get_object().building: