The SSH host key has changed on 8 April, 2022 to this one: SHA256:573uTBSeh74kvOo0HJXi5ijdzRm8me27suzNEDlGyrQ
Browse Source

Allow drawing once every day

The duration between two draws is controlled by settings.DRAW_TIME
gitter-badge
Gergely Polonkai 7 years ago
parent
commit
a53b2827a9
  1. 2
      wordchallenge/settings.py
  2. 10
      words/apps.py
  3. 14
      words/tests.py

2
wordchallenge/settings.py

@ -101,3 +101,5 @@ USE_TZ = True
# https://docs.djangoproject.com/en/1.8/howto/static-files/
STATIC_URL = '/static/'
DRAW_TIME = '1 00:00:00'

10
words/apps.py

@ -1,6 +1,8 @@
from django.apps import AppConfig
from django.conf import settings
from django.contrib.auth.models import User
from django.utils import timezone
from django.utils.dateparse import parse_duration
def _current_word(self):
from .models import Draw
@ -35,6 +37,12 @@ def _draw_word(self):
from .models import Word, Draw
last_draw = self.last_draw()
duration = parse_duration(settings.DRAW_TIME)
if last_draw.timestamp + duration > timezone.now():
return last_draw.word
# Find all words
# Exclude all words that has an accepted draw for this user
# Choose a random one

14
words/tests.py

@ -1,7 +1,9 @@
from datetime import timedelta
from django.conf import settings
from django.contrib.auth.models import User
from django.core.exceptions import ValidationError
from django.utils import timezone
from django.utils.dateparse import parse_duration
from django.utils.translation import activate
from django.test import TestCase
@ -80,6 +82,8 @@ class DrawTest(TestCase):
accepted=None)
self.assertEquals(self.word, self.user.draw_word())
draw.accepted = True
duration = parse_duration(settings.DRAW_TIME)
draw.timestamp -= 2 * duration
draw.save()
Work.objects.create(draw=draw)
@ -91,6 +95,7 @@ class DrawTest(TestCase):
self.assertIsNone(draw.accepted)
draw.accepted = True
draw.timestamp -= 2 * duration
draw.save()
Work.objects.create(draw=draw)
@ -110,3 +115,12 @@ class DrawTest(TestCase):
Work.objects.create(draw=draw)
self.assertEquals(word, self.user.last_draw().word)
def test_draw_per_day(self):
draw = Draw.objects.create(user=self.user,
word=self.word,
accepted=True)
Work.objects.create(draw=draw)
Word.objects.create()
self.assertEquals(self.word, self.user.draw_word())

Loading…
Cancel
Save