Compare commits
1 Commits
Author | SHA1 | Date | |
---|---|---|---|
3142e8d9b1 |
@ -78,20 +78,6 @@ def _draw_word(self):
|
||||
|
||||
return word
|
||||
|
||||
def _failed_words(self):
|
||||
from .models import Draw
|
||||
|
||||
flt = filter(lambda x: not x.successful(), Draw.objects.filter(user=self))
|
||||
|
||||
return [draw.word for draw in flt]
|
||||
|
||||
def _successful_words(self):
|
||||
from .models import Draw
|
||||
|
||||
flt = filter(lambda x: x.successful(), Draw.objects.filter(user=self))
|
||||
|
||||
return [draw.word for draw in flt]
|
||||
|
||||
class WordsConfig(AppConfig):
|
||||
name = 'words'
|
||||
|
||||
@ -99,5 +85,3 @@ class WordsConfig(AppConfig):
|
||||
User.current_word = _current_word
|
||||
User.draw_word = _draw_word
|
||||
User.last_draw = _last_draw
|
||||
User.failed_words = _failed_words
|
||||
User.successful_words = _successful_words
|
||||
|
@ -1,86 +0,0 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import migrations, models
|
||||
import django.utils.timezone
|
||||
from django.conf import settings
|
||||
|
||||
def migrate_words(apps, schema_editor):
|
||||
Word = apps.get_model('words', 'Word')
|
||||
WordTranslation = apps.get_model('words', 'WordTranslation')
|
||||
|
||||
for translation in WordTranslation.objects.all(): # pragma: no cover
|
||||
word = translation.word
|
||||
if word.word is not None:
|
||||
word = Word.objects.create()
|
||||
|
||||
word.word = translation.word
|
||||
word.language = translation.language
|
||||
word.added_by = translation.added_by
|
||||
word.added_at = translation.added_at
|
||||
word.save()
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
|
||||
('words', '0004_work_upload_time'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterUniqueTogether(
|
||||
name='wordtranslation',
|
||||
unique_together=set([]),
|
||||
),
|
||||
migrations.RemoveField(
|
||||
model_name='wordtranslation',
|
||||
name='added_by',
|
||||
),
|
||||
migrations.RemoveField(
|
||||
model_name='wordtranslation',
|
||||
name='word',
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='word',
|
||||
name='added_at',
|
||||
field=models.DateTimeField(default=django.utils.timezone.now),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='word',
|
||||
name='added_by',
|
||||
field=models.ForeignKey(null=True, to=settings.AUTH_USER_MODEL),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='word',
|
||||
name='language',
|
||||
field=models.CharField(null=True, max_length=5),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='word',
|
||||
name='word',
|
||||
field=models.CharField(null=True, max_length=100),
|
||||
),
|
||||
migrations.RunPython(migrate_words),
|
||||
migrations.AlterUniqueTogether(
|
||||
name='word',
|
||||
unique_together=set([('language', 'word')]),
|
||||
),
|
||||
migrations.DeleteModel(
|
||||
name='WordTranslation',
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='word',
|
||||
name='added_by',
|
||||
field=models.ForeignKey(to=settings.AUTH_USER_MODEL),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='word',
|
||||
name='language',
|
||||
field=models.CharField(max_length=5),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='word',
|
||||
name='word',
|
||||
field=models.CharField(max_length=100),
|
||||
),
|
||||
]
|
@ -8,19 +8,49 @@ from django.utils.translation import get_language
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Word(models.Model):
|
||||
language = models.CharField(max_length=5)
|
||||
word = models.CharField(max_length=100, null=False, blank=False)
|
||||
def translation(self, language):
|
||||
try:
|
||||
return self.translations.get(language=language)
|
||||
except WordTranslation.DoesNotExist:
|
||||
return None
|
||||
|
||||
def __str__(self):
|
||||
try:
|
||||
return self.translations.get(language=get_language()).translation
|
||||
except WordTranslation.DoesNotExist:
|
||||
pass
|
||||
|
||||
try:
|
||||
return self.translations \
|
||||
.get(language=settings.LANGUAGE_CODE).translation
|
||||
except WordTranslation.DoesNotExist:
|
||||
pass
|
||||
|
||||
return ""
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class WordTranslation(models.Model):
|
||||
word = models.ForeignKey(Word, related_name='translations')
|
||||
language = models.CharField(max_length=5, db_index=True)
|
||||
translation = models.CharField(max_length=100, null=True, blank=False)
|
||||
added_by = models.ForeignKey(User)
|
||||
added_at = models.DateTimeField(default=timezone.now)
|
||||
|
||||
def __str__(self):
|
||||
return self.word
|
||||
def clean(self):
|
||||
from django.core.exceptions import ValidationError
|
||||
|
||||
def __repr__(self):
|
||||
return '<Word: {} ({})>'.format(self.word, self.language)
|
||||
if self.translation is None or self.translation == '':
|
||||
raise ValidationError('translation must not be empty',
|
||||
code='translation-empty')
|
||||
|
||||
def __str__(self):
|
||||
return self.translation
|
||||
|
||||
class Meta:
|
||||
unique_together = (('language', 'word'),)
|
||||
unique_together = (
|
||||
('word', 'language'),
|
||||
('language', 'translation'),
|
||||
)
|
||||
|
||||
class Draw(models.Model):
|
||||
user = models.ForeignKey(User)
|
||||
|
@ -7,25 +7,62 @@ from django.utils.dateparse import parse_duration
|
||||
from django.utils.translation import activate
|
||||
from django.test import TestCase, override_settings
|
||||
|
||||
from .models import Word, Draw, Work
|
||||
from .models import Word, WordTranslation, Draw, Work
|
||||
|
||||
class WordTest(TestCase):
|
||||
def setUp(self):
|
||||
user = User.objects.create_user(username='test', password='test')
|
||||
self.word1 = Word.objects.create(language='en-us',
|
||||
word='color',
|
||||
added_by=user)
|
||||
self.word1 = Word.objects.create()
|
||||
self.translation1 = WordTranslation.objects.create(
|
||||
word=self.word1,
|
||||
language='en-us',
|
||||
translation='color',
|
||||
added_by=user)
|
||||
self.translation2 = WordTranslation.objects.create(
|
||||
word=self.word1,
|
||||
language='en-gb',
|
||||
translation='colour',
|
||||
added_by=user)
|
||||
self.translation3 = WordTranslation.objects.create(
|
||||
word=self.word1,
|
||||
language='hu-hu',
|
||||
translation='szín',
|
||||
added_by=user)
|
||||
|
||||
def test_word_str(self):
|
||||
self.assertEquals('color', self.word1.__str__())
|
||||
with self.settings(LANGUAGE_CODE='en-us'):
|
||||
self.assertEquals("color", self.word1.__str__())
|
||||
|
||||
with self.settings(LANGUAGE_CODE='en-gb'):
|
||||
self.assertEquals('colour', self.word1.__str__())
|
||||
|
||||
activate('hu-hu')
|
||||
self.assertEquals('szín', self.word1.__str__())
|
||||
|
||||
with self.settings(LANGUAGE_CODE='es-es'):
|
||||
activate('is-is')
|
||||
self.assertEquals('', self.word1.__str__())
|
||||
|
||||
def test_word_translation(self):
|
||||
self.assertEquals('color', self.word1.translation('en-us').translation)
|
||||
self.assertEquals('colour', self.word1.translation('en-gb').translation)
|
||||
self.assertIsNone(self.word1.translation('is-is'))
|
||||
|
||||
def test_translation_validation(self):
|
||||
word = WordTranslation()
|
||||
|
||||
with self.assertRaises(ValidationError) as ctx:
|
||||
word.clean()
|
||||
|
||||
self.assertEquals('translation-empty', ctx.exception.code)
|
||||
|
||||
def test_translation_str(self):
|
||||
self.assertEquals('color', self.translation1.__str__())
|
||||
|
||||
@override_settings(DRAW_TIME='1 00:00:00')
|
||||
class DrawTest(TestCase):
|
||||
def setUp(self):
|
||||
self.user = User.objects.create_user(username='test', password='test')
|
||||
self.word = Word.objects.create(added_by=self.user,
|
||||
language='en-us',
|
||||
word='color')
|
||||
self.word = Word.objects.create()
|
||||
|
||||
def test_current_word(self):
|
||||
self.assertIsNone(self.user.current_word())
|
||||
@ -39,6 +76,7 @@ class DrawTest(TestCase):
|
||||
draw.save()
|
||||
self.assertEquals(self.word, self.user.current_word())
|
||||
|
||||
@override_settings(DRAW_TIME='1 00:00:00')
|
||||
def test_draw_word(self):
|
||||
# User has no words yet
|
||||
self.assertEquals(self.word, self.user.draw_word())
|
||||
@ -55,7 +93,7 @@ class DrawTest(TestCase):
|
||||
Work.objects.create(draw=draw)
|
||||
|
||||
# Create a second word for further testing
|
||||
word2 = Word.objects.create(added_by=self.user)
|
||||
word2 = Word.objects.create()
|
||||
|
||||
# The next word should be different from the previous one
|
||||
self.assertEquals(word2, self.user.draw_word())
|
||||
@ -79,9 +117,7 @@ class DrawTest(TestCase):
|
||||
draw.save()
|
||||
work.delete()
|
||||
# Also create a new word
|
||||
word3 = Word.objects.create(added_by=self.user,
|
||||
language='en-gb',
|
||||
word='colour')
|
||||
word3 = Word.objects.create()
|
||||
|
||||
# A next draw should return the same word in this case
|
||||
self.assertEquals(word2, self.user.draw_word())
|
||||
@ -115,7 +151,7 @@ class DrawTest(TestCase):
|
||||
accepted=True,
|
||||
timestamp=timezone.now() - timedelta(days=1))
|
||||
Work.objects.create(draw=draw)
|
||||
word = Word.objects.create(added_by=self.user)
|
||||
word = Word.objects.create()
|
||||
draw = Draw.objects.create(user=self.user,
|
||||
word=word,
|
||||
accepted=True)
|
||||
@ -128,15 +164,16 @@ class DrawTest(TestCase):
|
||||
word=self.word,
|
||||
accepted=True)
|
||||
Work.objects.create(draw=draw)
|
||||
Word.objects.create(added_by=self.user)
|
||||
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(added_by=self.user),
|
||||
word=Word.objects.create(),
|
||||
accepted=True,
|
||||
timestamp=timezone.now())
|
||||
self.assertIsNone(draw.successful())
|
||||
@ -158,23 +195,3 @@ class DrawTest(TestCase):
|
||||
draw.save()
|
||||
self.assertIsNotNone(draw.successful())
|
||||
self.assertFalse(draw.successful())
|
||||
|
||||
def test_failed_successful_words(self):
|
||||
self.assertEquals([], self.user.failed_words())
|
||||
|
||||
draw = Draw.objects.create(user=self.user,
|
||||
word=self.word,
|
||||
accepted=True,
|
||||
timestamp=timezone.now() - timedelta(days=2))
|
||||
self.assertEquals([draw.word], self.user.failed_words())
|
||||
self.assertEquals([], self.user.successful_words())
|
||||
|
||||
draw2 = Draw.objects.create(user=self.user,
|
||||
word=Word.objects.create(added_by=self.user),
|
||||
accepted=True,
|
||||
timestamp=timezone.now() - timedelta(days=3))
|
||||
Work.objects.create(draw=draw2,
|
||||
upload_time=timezone.now() - timedelta(days=3))
|
||||
|
||||
self.assertEquals([draw.word], self.user.failed_words())
|
||||
self.assertEquals([draw2.word], self.user.successful_words())
|
||||
|
Loading…
Reference in New Issue
Block a user