Allow drawing once every day

The duration between two draws is controlled by settings.DRAW_TIME
This commit is contained in:
Gergely Polonkai 2015-11-26 16:56:55 +01:00
parent 3a1e234176
commit a53b2827a9
3 changed files with 25 additions and 1 deletions

View File

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

View File

@ -1,6 +1,8 @@
from django.apps import AppConfig from django.apps import AppConfig
from django.conf import settings
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.utils import timezone
from django.utils.dateparse import parse_duration
def _current_word(self): def _current_word(self):
from .models import Draw from .models import Draw
@ -35,6 +37,12 @@ def _draw_word(self):
from .models import Word, Draw 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 # 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

View File

@ -1,7 +1,9 @@
from datetime import timedelta from datetime import timedelta
from django.conf import settings
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 import timezone
from django.utils.dateparse import parse_duration
from django.utils.translation import activate from django.utils.translation import activate
from django.test import TestCase from django.test import TestCase
@ -80,6 +82,8 @@ class DrawTest(TestCase):
accepted=None) accepted=None)
self.assertEquals(self.word, self.user.draw_word()) self.assertEquals(self.word, self.user.draw_word())
draw.accepted = True draw.accepted = True
duration = parse_duration(settings.DRAW_TIME)
draw.timestamp -= 2 * duration
draw.save() draw.save()
Work.objects.create(draw=draw) Work.objects.create(draw=draw)
@ -91,6 +95,7 @@ class DrawTest(TestCase):
self.assertIsNone(draw.accepted) self.assertIsNone(draw.accepted)
draw.accepted = True draw.accepted = True
draw.timestamp -= 2 * duration
draw.save() draw.save()
Work.objects.create(draw=draw) Work.objects.create(draw=draw)
@ -110,3 +115,12 @@ class DrawTest(TestCase):
Work.objects.create(draw=draw) Work.objects.create(draw=draw)
self.assertEquals(word, self.user.last_draw().word) 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())