From e71eb452600aa6b1d93ae0281d87594609c16268 Mon Sep 17 00:00:00 2001 From: Gergely Polonkai Date: Fri, 27 Nov 2015 14:58:16 +0100 Subject: [PATCH] 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 --- words/migrations/0004_work_upload_time.py | 20 ++++++++++++++++ words/models.py | 23 +++++++++++++++++++ words/tests.py | 28 +++++++++++++++++++++++ 3 files changed, 71 insertions(+) create mode 100644 words/migrations/0004_work_upload_time.py diff --git a/words/migrations/0004_work_upload_time.py b/words/migrations/0004_work_upload_time.py new file mode 100644 index 0000000..31e4429 --- /dev/null +++ b/words/migrations/0004_work_upload_time.py @@ -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), + ), + ] diff --git a/words/models.py b/words/models.py index 15f8f54..4e799a7 100644 --- a/words/models.py +++ b/words/models.py @@ -2,6 +2,7 @@ from django.conf import settings from django.contrib.auth.models import User from django.db import models from django.utils import timezone +from django.utils.dateparse import parse_duration from django.utils.encoding import python_2_unicode_compatible from django.utils.translation import get_language @@ -57,9 +58,31 @@ class Draw(models.Model): accepted = models.NullBooleanField() 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: ordering = ('timestamp',) class Work(models.Model): draw = models.OneToOneField(Draw, related_name='work', primary_key=True) language = models.CharField(max_length=5, db_index=True) + upload_time = models.DateTimeField(default=timezone.now) diff --git a/words/tests.py b/words/tests.py index cf69c80..1adc8f4 100644 --- a/words/tests.py +++ b/words/tests.py @@ -167,3 +167,31 @@ class DrawTest(TestCase): Word.objects.create() 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 wasn’t submitted on time + draw.timestamp = timezone.now() - timedelta(days=2) + draw.save() + self.assertIsNotNone(draw.successful()) + self.assertFalse(draw.successful())