Add User.last_draw()

This commit is contained in:
Gergely Polonkai 2015-11-26 16:56:33 +01:00
parent 0c16d9a73b
commit 3a1e234176
2 changed files with 23 additions and 0 deletions

View File

@ -24,6 +24,11 @@ def _current_word(self):
return None return None
def _last_draw(self):
from .models import Draw
return Draw.objects.filter(user=self).order_by('-timestamp').first()
def _draw_word(self): def _draw_word(self):
if self.current_word() is not None: if self.current_word() is not None:
return self.current_word() return self.current_word()
@ -52,3 +57,4 @@ class WordsConfig(AppConfig):
def ready(self): def ready(self):
User.current_word = _current_word User.current_word = _current_word
User.draw_word = _draw_word User.draw_word = _draw_word
User.last_draw = _last_draw

View File

@ -1,5 +1,7 @@
from datetime import timedelta
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
from django.utils import timezone
from django.utils.translation import activate from django.utils.translation import activate
from django.test import TestCase from django.test import TestCase
@ -93,3 +95,18 @@ class DrawTest(TestCase):
Work.objects.create(draw=draw) Work.objects.create(draw=draw)
self.assertIsNone(self.user.draw_word()) 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)