diff --git a/booking/models.py b/booking/models.py index d17d29c..b2fb6ea 100644 --- a/booking/models.py +++ b/booking/models.py @@ -87,6 +87,17 @@ class Duck(models.Model): return Booking.duck_booking_time(self) / Booking.total_booking_time() + def booked_by(self): + l = self.booking_set.filter(end_ts = None) + + if len(l) == 0: + return None + + if len(l) > 1: + raise RuntimeError(u"Duck is booked more than once!") + + return l[0].user + class DuckName(models.Model): """Model to hold name suggestions for Ducks""" diff --git a/booking/tests.py b/booking/tests.py index eff4f09..b16406f 100644 --- a/booking/tests.py +++ b/booking/tests.py @@ -249,3 +249,32 @@ class SimilarCompTest(TestCase): def test_bad_similar_competence(self): l = Competence.get_similar_comps('development') self.assertEqual(len(l), 0) + +class BookingTest(TestCase): + def setUp(self): + spec = Species(name = 'test') + spec.save() + + loc = Location(name = 'test') + loc.save() + + user = User(username = 'test') + user.save() + + self.booked_duck = Duck(species = spec, location = loc, donated_by = user) + self.booked_duck.save() + + comp = Competence(name = 'test', added_by = user) + comp.save() + + booking = Booking(duck = self.booked_duck, user = user, comp_req = comp) + booking.save() + + self.unbooked_duck = Duck(species = spec, location = loc, donated_by = user) + self.unbooked_duck.save() + + def test_booked_duck(self): + self.assertNotEqual(self.booked_duck.booked_by(), None) + + def test_unbooked_duck(self): + self.assertEqual(self.unbooked_duck.booked_by(), None)