From 3a1e234176baf16913a92b709e6e85a542cba4f5 Mon Sep 17 00:00:00 2001 From: Gergely Polonkai Date: Thu, 26 Nov 2015 16:56:33 +0100 Subject: [PATCH] Add User.last_draw() --- words/apps.py | 6 ++++++ words/tests.py | 17 +++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/words/apps.py b/words/apps.py index 7a52dca..17eacbf 100644 --- a/words/apps.py +++ b/words/apps.py @@ -24,6 +24,11 @@ def _current_word(self): return None +def _last_draw(self): + from .models import Draw + + return Draw.objects.filter(user=self).order_by('-timestamp').first() + def _draw_word(self): if self.current_word() is not None: return self.current_word() @@ -52,3 +57,4 @@ class WordsConfig(AppConfig): def ready(self): User.current_word = _current_word User.draw_word = _draw_word + User.last_draw = _last_draw diff --git a/words/tests.py b/words/tests.py index 5f7da38..39f2e6c 100644 --- a/words/tests.py +++ b/words/tests.py @@ -1,5 +1,7 @@ +from datetime import timedelta from django.contrib.auth.models import User from django.core.exceptions import ValidationError +from django.utils import timezone from django.utils.translation import activate from django.test import TestCase @@ -93,3 +95,18 @@ class DrawTest(TestCase): Work.objects.create(draw=draw) self.assertIsNone(self.user.draw_word()) + + def test_last_draw(self): + draw = Draw.objects.create( + user=self.user, + word=self.word, + accepted=True, + timestamp=timezone.now() - timedelta(days=1)) + Work.objects.create(draw=draw) + word = Word.objects.create() + draw = Draw.objects.create(user=self.user, + word=word, + accepted=True) + Work.objects.create(draw=draw) + + self.assertEquals(word, self.user.last_draw().word)