Compare commits

...

5 Commits

Author SHA1 Message Date
Gergely Polonkai 925ed0bb0d Remove WordTranslation model
This seems to cause more trouble than not. We may re-add it with
different concepts
2015-11-27 16:25:21 +01:00
Gergely Polonkai 3c0882df07 Add User.successful_words()
It lists all the words the user has successfully uploaded work for
2015-11-27 15:56:29 +01:00
Gergely Polonkai 65019fd713 Add User.failed_words()
It lists all the words the user has failed to upload work for
2015-11-27 15:50:43 +01:00
Gergely Polonkai 3da4b39860 Move override_settings from function to class level 2015-11-27 15:49:13 +01:00
Gergely Polonkai 3073cb6a93 Moved gitter badge next to the others 2015-11-27 15:10:59 +01:00
5 changed files with 146 additions and 92 deletions

View File

@ -1,11 +1,10 @@
[![Build Status](https://travis-ci.org/gergelypolonkai/word-challenge.svg?branch=master)](https://travis-ci.org/gergelypolonkai/word-challenge)
[![codecov.io](https://codecov.io/github/gergelypolonkai/word-challenge/coverage.svg?branch=master)](https://codecov.io/github/gergelypolonkai/word-challenge?branch=master)
[![Join the chat at https://gitter.im/gergelypolonkai/word-challenge](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/gergelypolonkai/word-challenge?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
100(ish) words challenge
========================
[![Join the chat at https://gitter.im/gergelypolonkai/word-challenge](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/gergelypolonkai/word-challenge?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
This is the main project for 100(ish)word challenge, a challenge for
artists of different kinds.

View File

@ -78,6 +78,20 @@ 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'
@ -85,3 +99,5 @@ 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

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,62 +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',
added_by=user)
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())
@ -76,7 +39,6 @@ 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())
@ -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,16 +128,15 @@ 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())
@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(),
word=Word.objects.create(added_by=self.user),
accepted=True,
timestamp=timezone.now())
self.assertIsNone(draw.successful())
@ -195,3 +158,23 @@ 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())