Extend draw_word() so it tries to draw a new one

If the last word is rejected by the user, draw_word() tries to draw a
new one now.
This commit is contained in:
Gergely Polonkai 2015-11-27 14:42:12 +01:00
parent d5e6e4a79c
commit 2f1c8e5850
2 changed files with 61 additions and 10 deletions

View File

@ -40,21 +40,39 @@ def _draw_word(self):
last_draw = self.last_draw() last_draw = self.last_draw()
duration = parse_duration(settings.DRAW_TIME) duration = parse_duration(settings.DRAW_TIME)
if last_draw.timestamp + duration > timezone.now(): if last_draw is not None \
and (last_draw.accepted is None
or last_draw.accepted == True) \
and last_draw.timestamp + duration > timezone.now():
return last_draw.word return last_draw.word
# Find all words # Find all words
# Exclude all words that has an accepted draw for this user # Exclude all words that has an accepted draw for this user
# Choose a random one # Choose a random one
# If there are no more words, return None # If there are no more words, return None
word = Word.objects \ if last_draw is not None:
.exclude(draws__accepted=True, draws__user=self) \ last_word = last_draw.word
.order_by('?') \ else:
.first() last_word = None
if word is None: all_words = Word.objects.exclude(draws__accepted=True, draws__user=self)
all_count = all_words.count()
# If there are no more words, return None
if all_count == 0:
return None return None
# If there is only one word, return it, regardless if its the
# same as the last one
if all_count == 1:
word = all_words.first()
# Otherwise, choose a word different from the last one
else:
word = last_word
while last_word == word:
word = all_words.order_by('?').first()
Draw.objects.create(user=self, word=word, accepted=None) Draw.objects.create(user=self, word=word, accepted=None)
return word return word

View File

@ -79,13 +79,14 @@ class DrawTest(TestCase):
@override_settings(DRAW_TIME='1 00:00:00') @override_settings(DRAW_TIME='1 00:00:00')
def test_draw_word(self): def test_draw_word(self):
# User has no words yet # User has no words yet
draw = Draw.objects.create(user=self.user, self.assertEquals(self.word, self.user.draw_word())
word=self.word,
accepted=None) # User now has an unaccepted draw
self.assertEquals(self.word, self.user.draw_word()) self.assertEquals(self.word, self.user.draw_word())
# Accept the last word and make it appear as if it would be 2 # Accept the last word and make it appear as if it would be 2
# days ago # days ago
draw = Draw.objects.get(user=self.user, word=self.word)
draw.accepted = True draw.accepted = True
draw.timestamp -= timedelta(days=2) draw.timestamp -= timedelta(days=2)
draw.save() draw.save()
@ -106,11 +107,43 @@ class DrawTest(TestCase):
draw.accepted = True draw.accepted = True
draw.timestamp -= timedelta(days=2) draw.timestamp -= timedelta(days=2)
draw.save() draw.save()
Work.objects.create(draw=draw) work = Work.objects.create(draw=draw)
# As we are out of words now, a new draw should return None # As we are out of words now, a new draw should return None
self.assertIsNone(self.user.draw_word()) self.assertIsNone(self.user.draw_word())
# Now set the last draw to fresh again, and remove the associated work.
draw.timestamp = timezone.now()
draw.save()
work.delete()
# Also create a new word
word3 = Word.objects.create()
# A next draw should return the same word in this case
self.assertEquals(word2, self.user.draw_word())
# Now lets reject this draw and draw a new one
draw = Draw.objects.get(user=self.user, word=word2)
draw.accepted = False
draw.save()
# The next draw should be different from the last
self.assertEquals(word3, self.user.draw_word())
# Now make the previous one accepted and completed, and reject
# this last one
draw.accepted = True
draw.save()
Work.objects.create(draw=draw)
draw = Draw.objects.get(user=self.user, word=word3)
draw.accepted = False
draw.save()
# The next draw must be this last, rejected one (as there are
# no other options)
self.assertEquals(word3, self.user.draw_word())
def test_last_draw(self): def test_last_draw(self):
draw = Draw.objects.create( draw = Draw.objects.create(
user=self.user, user=self.user,