Remove WordTranslation model

This seems to cause more trouble than not. We may re-add it with
different concepts
This commit is contained in:
Gergely Polonkai 2015-11-27 16:25:21 +01:00
parent 3c0882df07
commit 925ed0bb0d
3 changed files with 109 additions and 89 deletions

View File

@ -0,0 +1,86 @@
# -*- 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),
),
]

View File

@ -8,49 +8,19 @@ from django.utils.translation import get_language
@python_2_unicode_compatible
class Word(models.Model):
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)
language = models.CharField(max_length=5)
word = models.CharField(max_length=100, null=False, blank=False)
added_by = models.ForeignKey(User)
added_at = models.DateTimeField(default=timezone.now)
def clean(self):
from django.core.exceptions import ValidationError
if self.translation is None or self.translation == '':
raise ValidationError('translation must not be empty',
code='translation-empty')
def __str__(self):
return self.translation
return self.word
def __repr__(self):
return '<Word: {} ({})>'.format(self.word, self.language)
class Meta:
unique_together = (
('word', 'language'),
('language', 'translation'),
)
unique_together = (('language', 'word'),)
class Draw(models.Model):
user = models.ForeignKey(User)

View File

@ -7,63 +7,25 @@ from django.utils.dateparse import parse_duration
from django.utils.translation import activate
from django.test import TestCase, override_settings
from .models import Word, WordTranslation, Draw, Work
from .models import Word, Draw, Work
class WordTest(TestCase):
def setUp(self):
user = User.objects.create_user(username='test', password='test')
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',
self.word1 = Word.objects.create(language='en-us',
word='color',
added_by=user)
def test_word_str(self):
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__())
self.assertEquals('color', self.word1.__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()
self.word = Word.objects.create(added_by=self.user,
language='en-us',
word='color')
def test_current_word(self):
self.assertIsNone(self.user.current_word())
@ -93,7 +55,7 @@ class DrawTest(TestCase):
Work.objects.create(draw=draw)
# Create a second word for further testing
word2 = Word.objects.create()
word2 = Word.objects.create(added_by=self.user)
# The next word should be different from the previous one
self.assertEquals(word2, self.user.draw_word())
@ -117,7 +79,9 @@ class DrawTest(TestCase):
draw.save()
work.delete()
# Also create a new word
word3 = Word.objects.create()
word3 = Word.objects.create(added_by=self.user,
language='en-gb',
word='colour')
# A next draw should return the same word in this case
self.assertEquals(word2, self.user.draw_word())
@ -151,7 +115,7 @@ class DrawTest(TestCase):
accepted=True,
timestamp=timezone.now() - timedelta(days=1))
Work.objects.create(draw=draw)
word = Word.objects.create()
word = Word.objects.create(added_by=self.user)
draw = Draw.objects.create(user=self.user,
word=word,
accepted=True)
@ -164,7 +128,7 @@ class DrawTest(TestCase):
word=self.word,
accepted=True)
Work.objects.create(draw=draw)
Word.objects.create()
Word.objects.create(added_by=self.user)
self.assertEquals(self.word, self.user.draw_word())
@ -172,7 +136,7 @@ class DrawTest(TestCase):
# If there is no work, but we are within the time range
draw = Draw.objects.create(
user=self.user,
word=Word.objects.create(),
word=Word.objects.create(added_by=self.user),
accepted=True,
timestamp=timezone.now())
self.assertIsNone(draw.successful())
@ -206,7 +170,7 @@ class DrawTest(TestCase):
self.assertEquals([], self.user.successful_words())
draw2 = Draw.objects.create(user=self.user,
word=Word.objects.create(),
word=Word.objects.create(added_by=self.user),
accepted=True,
timestamp=timezone.now() - timedelta(days=3))
Work.objects.create(draw=draw2,