From 955cace9bda0e5f42a2afab4422b5840b5f5400e Mon Sep 17 00:00:00 2001 From: Gergely Polonkai Date: Thu, 26 Nov 2015 14:18:01 +0100 Subject: [PATCH] Add word drawing support --- words/__init__.py | 1 + words/apps.py | 31 ++++++++++++++++++++ words/migrations/0002_draw_work.py | 45 ++++++++++++++++++++++++++++++ words/models.py | 13 +++++++++ words/tests.py | 19 ++++++++++++- 5 files changed, 108 insertions(+), 1 deletion(-) create mode 100644 words/apps.py create mode 100644 words/migrations/0002_draw_work.py diff --git a/words/__init__.py b/words/__init__.py index e69de29..3c6f912 100644 --- a/words/__init__.py +++ b/words/__init__.py @@ -0,0 +1 @@ +default_app_config = 'words.apps.WordsConfig' diff --git a/words/apps.py b/words/apps.py new file mode 100644 index 0000000..e28e524 --- /dev/null +++ b/words/apps.py @@ -0,0 +1,31 @@ +from django.apps import AppConfig +from django.contrib.auth.models import User + + +def _current_word(self): + from .models import Draw + + # If the user has an undecided draw, return that one + try: + word = Draw.objects.get(user=self, accepted=None).word + + return word + except Draw.DoesNotExist: + pass + + # If the user has an accepted draw that is unfinished (ie. no work + # is uploaded), return that one + try: + word = Draw.objects.get(user=self, accepted=True, work=None).word + + return word + except Draw.DoesNotExist: + pass + + return None + +class WordsConfig(AppConfig): + name = 'words' + + def ready(self): + User.current_word = _current_word diff --git a/words/migrations/0002_draw_work.py b/words/migrations/0002_draw_work.py new file mode 100644 index 0000000..68814fe --- /dev/null +++ b/words/migrations/0002_draw_work.py @@ -0,0 +1,45 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import migrations, models +from django.conf import settings +import django.utils.timezone + + +class Migration(migrations.Migration): + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ('words', '0001_initial'), + ] + + operations = [ + migrations.CreateModel( + name='Draw', + fields=[ + ('id', models.AutoField(verbose_name='ID', primary_key=True, serialize=False, auto_created=True)), + ('accepted', models.NullBooleanField()), + ('timestamp', models.DateTimeField(default=django.utils.timezone.now)), + ], + options={ + 'ordering': ('timestamp',), + }, + ), + migrations.CreateModel( + name='Work', + fields=[ + ('draw', models.OneToOneField(to='words.Draw', serialize=False, primary_key=True, related_name='work')), + ('language', models.CharField(max_length=5, db_index=True)), + ], + ), + migrations.AddField( + model_name='draw', + name='user', + field=models.ForeignKey(to=settings.AUTH_USER_MODEL), + ), + migrations.AddField( + model_name='draw', + name='word', + field=models.ForeignKey(to='words.Word'), + ), + ] diff --git a/words/models.py b/words/models.py index 2d34dec..f9af291 100644 --- a/words/models.py +++ b/words/models.py @@ -47,3 +47,16 @@ class WordTranslation(models.Model): ('word', 'language'), ('language', 'translation'), ) + +class Draw(models.Model): + user = models.ForeignKey(User) + word = models.ForeignKey(Word) + accepted = models.NullBooleanField() + timestamp = models.DateTimeField(default=timezone.now) + + 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) diff --git a/words/tests.py b/words/tests.py index 1a1f582..5f128d5 100644 --- a/words/tests.py +++ b/words/tests.py @@ -3,7 +3,7 @@ from django.core.exceptions import ValidationError from django.utils.translation import activate from django.test import TestCase -from .models import Word, WordTranslation +from .models import Word, WordTranslation, Draw class WordTest(TestCase): def setUp(self): @@ -54,3 +54,20 @@ class WordTest(TestCase): def test_translation_str(self): self.assertEquals('color', self.translation1.__str__()) + +class DrawTest(TestCase): + def setUp(self): + self.user = User.objects.create_user(username='test', password='test') + self.word = Word.objects.create() + + def test_current_word(self): + self.assertIsNone(self.user.current_word()) + + draw = Draw.objects.create(user=self.user, + word=self.word, + accepted=None) + self.assertEquals(self.word, self.user.current_word()) + + draw.accepted = True + draw.save() + self.assertEquals(self.word, self.user.current_word())