Compare commits
No commits in common. "master" and "gitter-badge" have entirely different histories.
master
...
gitter-bad
@ -1,10 +1,11 @@
|
|||||||
[![Build Status](https://travis-ci.org/gergelypolonkai/word-challenge.svg?branch=master)](https://travis-ci.org/gergelypolonkai/word-challenge)
|
[![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)
|
[![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
|
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
|
This is the main project for 100(ish)word challenge, a challenge for
|
||||||
artists of different kinds.
|
artists of different kinds.
|
||||||
|
|
||||||
|
@ -78,20 +78,6 @@ def _draw_word(self):
|
|||||||
|
|
||||||
return word
|
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):
|
class WordsConfig(AppConfig):
|
||||||
name = 'words'
|
name = 'words'
|
||||||
|
|
||||||
@ -99,5 +85,3 @@ class WordsConfig(AppConfig):
|
|||||||
User.current_word = _current_word
|
User.current_word = _current_word
|
||||||
User.draw_word = _draw_word
|
User.draw_word = _draw_word
|
||||||
User.last_draw = _last_draw
|
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
|
@python_2_unicode_compatible
|
||||||
class Word(models.Model):
|
class Word(models.Model):
|
||||||
language = models.CharField(max_length=5)
|
def translation(self, language):
|
||||||
word = models.CharField(max_length=100, null=False, blank=False)
|
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_by = models.ForeignKey(User)
|
||||||
added_at = models.DateTimeField(default=timezone.now)
|
added_at = models.DateTimeField(default=timezone.now)
|
||||||
|
|
||||||
def __str__(self):
|
def clean(self):
|
||||||
return self.word
|
from django.core.exceptions import ValidationError
|
||||||
|
|
||||||
def __repr__(self):
|
if self.translation is None or self.translation == '':
|
||||||
return '<Word: {} ({})>'.format(self.word, self.language)
|
raise ValidationError('translation must not be empty',
|
||||||
|
code='translation-empty')
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return self.translation
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
unique_together = (('language', 'word'),)
|
unique_together = (
|
||||||
|
('word', 'language'),
|
||||||
|
('language', 'translation'),
|
||||||
|
)
|
||||||
|
|
||||||
class Draw(models.Model):
|
class Draw(models.Model):
|
||||||
user = models.ForeignKey(User)
|
user = models.ForeignKey(User)
|
||||||
|
@ -7,25 +7,62 @@ from django.utils.dateparse import parse_duration
|
|||||||
from django.utils.translation import activate
|
from django.utils.translation import activate
|
||||||
from django.test import TestCase, override_settings
|
from django.test import TestCase, override_settings
|
||||||
|
|
||||||
from .models import Word, Draw, Work
|
from .models import Word, WordTranslation, Draw, Work
|
||||||
|
|
||||||
class WordTest(TestCase):
|
class WordTest(TestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
user = User.objects.create_user(username='test', password='test')
|
user = User.objects.create_user(username='test', password='test')
|
||||||
self.word1 = Word.objects.create(language='en-us',
|
self.word1 = Word.objects.create()
|
||||||
word='color',
|
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)
|
added_by=user)
|
||||||
|
|
||||||
def test_word_str(self):
|
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):
|
class DrawTest(TestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.user = User.objects.create_user(username='test', password='test')
|
self.user = User.objects.create_user(username='test', password='test')
|
||||||
self.word = Word.objects.create(added_by=self.user,
|
self.word = Word.objects.create()
|
||||||
language='en-us',
|
|
||||||
word='color')
|
|
||||||
|
|
||||||
def test_current_word(self):
|
def test_current_word(self):
|
||||||
self.assertIsNone(self.user.current_word())
|
self.assertIsNone(self.user.current_word())
|
||||||
@ -39,6 +76,7 @@ class DrawTest(TestCase):
|
|||||||
draw.save()
|
draw.save()
|
||||||
self.assertEquals(self.word, self.user.current_word())
|
self.assertEquals(self.word, self.user.current_word())
|
||||||
|
|
||||||
|
@override_settings(DRAW_TIME='1 00:00:00')
|
||||||
def test_draw_word(self):
|
def test_draw_word(self):
|
||||||
# User has no words yet
|
# User has no words yet
|
||||||
self.assertEquals(self.word, self.user.draw_word())
|
self.assertEquals(self.word, self.user.draw_word())
|
||||||
@ -55,7 +93,7 @@ class DrawTest(TestCase):
|
|||||||
Work.objects.create(draw=draw)
|
Work.objects.create(draw=draw)
|
||||||
|
|
||||||
# Create a second word for further testing
|
# 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
|
# The next word should be different from the previous one
|
||||||
self.assertEquals(word2, self.user.draw_word())
|
self.assertEquals(word2, self.user.draw_word())
|
||||||
@ -79,9 +117,7 @@ class DrawTest(TestCase):
|
|||||||
draw.save()
|
draw.save()
|
||||||
work.delete()
|
work.delete()
|
||||||
# Also create a new word
|
# Also create a new word
|
||||||
word3 = Word.objects.create(added_by=self.user,
|
word3 = Word.objects.create()
|
||||||
language='en-gb',
|
|
||||||
word='colour')
|
|
||||||
|
|
||||||
# A next draw should return the same word in this case
|
# A next draw should return the same word in this case
|
||||||
self.assertEquals(word2, self.user.draw_word())
|
self.assertEquals(word2, self.user.draw_word())
|
||||||
@ -115,7 +151,7 @@ class DrawTest(TestCase):
|
|||||||
accepted=True,
|
accepted=True,
|
||||||
timestamp=timezone.now() - timedelta(days=1))
|
timestamp=timezone.now() - timedelta(days=1))
|
||||||
Work.objects.create(draw=draw)
|
Work.objects.create(draw=draw)
|
||||||
word = Word.objects.create(added_by=self.user)
|
word = Word.objects.create()
|
||||||
draw = Draw.objects.create(user=self.user,
|
draw = Draw.objects.create(user=self.user,
|
||||||
word=word,
|
word=word,
|
||||||
accepted=True)
|
accepted=True)
|
||||||
@ -128,15 +164,16 @@ class DrawTest(TestCase):
|
|||||||
word=self.word,
|
word=self.word,
|
||||||
accepted=True)
|
accepted=True)
|
||||||
Work.objects.create(draw=draw)
|
Work.objects.create(draw=draw)
|
||||||
Word.objects.create(added_by=self.user)
|
Word.objects.create()
|
||||||
|
|
||||||
self.assertEquals(self.word, self.user.draw_word())
|
self.assertEquals(self.word, self.user.draw_word())
|
||||||
|
|
||||||
|
@override_settings(DRAW_TIME='1 00:00:00')
|
||||||
def test_draw_successful(self):
|
def test_draw_successful(self):
|
||||||
# If there is no work, but we are within the time range
|
# If there is no work, but we are within the time range
|
||||||
draw = Draw.objects.create(
|
draw = Draw.objects.create(
|
||||||
user=self.user,
|
user=self.user,
|
||||||
word=Word.objects.create(added_by=self.user),
|
word=Word.objects.create(),
|
||||||
accepted=True,
|
accepted=True,
|
||||||
timestamp=timezone.now())
|
timestamp=timezone.now())
|
||||||
self.assertIsNone(draw.successful())
|
self.assertIsNone(draw.successful())
|
||||||
@ -158,23 +195,3 @@ class DrawTest(TestCase):
|
|||||||
draw.save()
|
draw.save()
|
||||||
self.assertIsNotNone(draw.successful())
|
self.assertIsNotNone(draw.successful())
|
||||||
self.assertFalse(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…
x
Reference in New Issue
Block a user