Add Draw.successful()

It can decide if the given draw was successful:

* The word is accepted
* Work was submitted for the word
* The work was submitted on time
This commit is contained in:
Gergely Polonkai 2015-11-27 14:58:16 +01:00
parent 51035af3d0
commit e71eb45260
3 changed files with 71 additions and 0 deletions

View File

@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models
import django.utils.timezone
class Migration(migrations.Migration):
dependencies = [
('words', '0003_draw_word_related'),
]
operations = [
migrations.AddField(
model_name='work',
name='upload_time',
field=models.DateTimeField(default=django.utils.timezone.now),
),
]

View File

@ -2,6 +2,7 @@ from django.conf import settings
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.db import models from django.db import models
from django.utils import timezone from django.utils import timezone
from django.utils.dateparse import parse_duration
from django.utils.encoding import python_2_unicode_compatible from django.utils.encoding import python_2_unicode_compatible
from django.utils.translation import get_language from django.utils.translation import get_language
@ -57,9 +58,31 @@ class Draw(models.Model):
accepted = models.NullBooleanField() accepted = models.NullBooleanField()
timestamp = models.DateTimeField(default=timezone.now) timestamp = models.DateTimeField(default=timezone.now)
def successful(self):
max_duration = parse_duration(settings.DRAW_TIME)
try:
work = self.work
except Work.DoesNotExist:
work = None
if work is None:
elapsed_time = timezone.now() - self.timestamp
if elapsed_time >= max_duration:
return False
return None
if self.work.upload_time - self.timestamp > max_duration:
return False
return True
class Meta: class Meta:
ordering = ('timestamp',) ordering = ('timestamp',)
class Work(models.Model): class Work(models.Model):
draw = models.OneToOneField(Draw, related_name='work', primary_key=True) draw = models.OneToOneField(Draw, related_name='work', primary_key=True)
language = models.CharField(max_length=5, db_index=True) language = models.CharField(max_length=5, db_index=True)
upload_time = models.DateTimeField(default=timezone.now)

View File

@ -167,3 +167,31 @@ class DrawTest(TestCase):
Word.objects.create() Word.objects.create()
self.assertEquals(self.word, self.user.draw_word()) self.assertEquals(self.word, self.user.draw_word())
@override_settings(DRAW_TIME='1 00:00:00')
def test_draw_successful(self):
# If there is no work, but we are within the time range
draw = Draw.objects.create(
user=self.user,
word=Word.objects.create(),
accepted=True,
timestamp=timezone.now())
self.assertIsNone(draw.successful())
# If there is no work and we are out of time
draw.timestamp -= timedelta(days=2)
draw.save()
self.assertIsNotNone(draw.successful())
self.assertFalse(draw.successful())
# If there is work and it was submitted on time
draw.timestamp = timezone.now() + timedelta(minutes=1)
draw.save()
Work.objects.create(draw=draw)
self.assertTrue(draw.successful())
# If there is work and it wasnt submitted on time
draw.timestamp = timezone.now() - timedelta(days=2)
draw.save()
self.assertIsNotNone(draw.successful())
self.assertFalse(draw.successful())