Add word drawing support

This commit is contained in:
Gergely Polonkai 2015-11-26 14:18:01 +01:00
parent cfaa2b2a50
commit 955cace9bd
5 changed files with 108 additions and 1 deletions

View File

@ -0,0 +1 @@
default_app_config = 'words.apps.WordsConfig'

31
words/apps.py Normal file
View File

@ -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

View File

@ -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'),
),
]

View File

@ -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)

View File

@ -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())