Initial commit
This commit is contained in:
0
words/__init__.py
Normal file
0
words/__init__.py
Normal file
37
words/migrations/0001_initial.py
Normal file
37
words/migrations/0001_initial.py
Normal file
@@ -0,0 +1,37 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import migrations, models
|
||||
import django.utils.timezone
|
||||
from django.conf import settings
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='Word',
|
||||
fields=[
|
||||
('id', models.AutoField(verbose_name='ID', primary_key=True, auto_created=True, serialize=False)),
|
||||
],
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name='WordTranslation',
|
||||
fields=[
|
||||
('id', models.AutoField(verbose_name='ID', primary_key=True, auto_created=True, serialize=False)),
|
||||
('language', models.CharField(db_index=True, max_length=5)),
|
||||
('translation', models.CharField(null=True, max_length=100)),
|
||||
('added_at', models.DateTimeField(default=django.utils.timezone.now)),
|
||||
('added_by', models.ForeignKey(to=settings.AUTH_USER_MODEL)),
|
||||
('word', models.ForeignKey(related_name='translations', to='words.Word')),
|
||||
],
|
||||
),
|
||||
migrations.AlterUniqueTogether(
|
||||
name='wordtranslation',
|
||||
unique_together=set([('language', 'translation'), ('word', 'language')]),
|
||||
),
|
||||
]
|
0
words/migrations/__init__.py
Normal file
0
words/migrations/__init__.py
Normal file
49
words/models.py
Normal file
49
words/models.py
Normal file
@@ -0,0 +1,49 @@
|
||||
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.translation import get_language
|
||||
|
||||
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 ""
|
||||
|
||||
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 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
|
||||
|
||||
class Meta:
|
||||
unique_together = (
|
||||
('word', 'language'),
|
||||
('language', 'translation'),
|
||||
)
|
56
words/tests.py
Normal file
56
words/tests.py
Normal file
@@ -0,0 +1,56 @@
|
||||
from django.contrib.auth.models import User
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.utils.translation import activate
|
||||
from django.test import TestCase
|
||||
|
||||
from .models import Word, WordTranslation
|
||||
|
||||
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',
|
||||
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__())
|
Reference in New Issue
Block a user