2015-11-27 13:54:28 +00:00
|
|
|
|
# -*- coding: utf-8
|
2015-11-26 13:18:01 +00:00
|
|
|
|
from django.apps import AppConfig
|
2015-11-26 15:56:55 +00:00
|
|
|
|
from django.conf import settings
|
2015-11-26 13:18:01 +00:00
|
|
|
|
from django.contrib.auth.models import User
|
2015-11-26 15:56:55 +00:00
|
|
|
|
from django.utils import timezone
|
|
|
|
|
from django.utils.dateparse import parse_duration
|
2015-11-26 13:18:01 +00:00
|
|
|
|
|
|
|
|
|
def _current_word(self):
|
|
|
|
|
from .models import Draw
|
|
|
|
|
|
|
|
|
|
# If the user has an undecided draw, return that one
|
|
|
|
|
try:
|
|
|
|
|
word = Draw.objects.get(user=self, accepted=None).word
|
|
|
|
|
|
|
|
|
|
return word
|
|
|
|
|
except Draw.DoesNotExist:
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
# If the user has an accepted draw that is unfinished (ie. no work
|
|
|
|
|
# is uploaded), return that one
|
|
|
|
|
try:
|
|
|
|
|
word = Draw.objects.get(user=self, accepted=True, work=None).word
|
|
|
|
|
|
|
|
|
|
return word
|
|
|
|
|
except Draw.DoesNotExist:
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
|
2015-11-26 15:56:33 +00:00
|
|
|
|
def _last_draw(self):
|
|
|
|
|
from .models import Draw
|
|
|
|
|
|
|
|
|
|
return Draw.objects.filter(user=self).order_by('-timestamp').first()
|
|
|
|
|
|
2015-11-26 13:51:04 +00:00
|
|
|
|
def _draw_word(self):
|
|
|
|
|
if self.current_word() is not None:
|
|
|
|
|
return self.current_word()
|
|
|
|
|
|
|
|
|
|
from .models import Word, Draw
|
|
|
|
|
|
2015-11-26 15:56:55 +00:00
|
|
|
|
last_draw = self.last_draw()
|
|
|
|
|
duration = parse_duration(settings.DRAW_TIME)
|
|
|
|
|
|
2015-11-27 13:42:12 +00:00
|
|
|
|
if last_draw is not None \
|
|
|
|
|
and (last_draw.accepted is None
|
|
|
|
|
or last_draw.accepted == True) \
|
|
|
|
|
and last_draw.timestamp + duration > timezone.now():
|
2015-11-26 15:56:55 +00:00
|
|
|
|
return last_draw.word
|
|
|
|
|
|
2015-11-26 13:51:04 +00:00
|
|
|
|
# Find all words
|
|
|
|
|
# Exclude all words that has an accepted draw for this user
|
|
|
|
|
# Choose a random one
|
|
|
|
|
# If there are no more words, return None
|
2015-11-27 13:42:12 +00:00
|
|
|
|
if last_draw is not None:
|
|
|
|
|
last_word = last_draw.word
|
|
|
|
|
else:
|
|
|
|
|
last_word = None
|
2015-11-26 13:51:04 +00:00
|
|
|
|
|
2015-11-27 13:42:12 +00:00
|
|
|
|
all_words = Word.objects.exclude(draws__accepted=True, draws__user=self)
|
|
|
|
|
all_count = all_words.count()
|
|
|
|
|
|
|
|
|
|
# If there are no more words, return None
|
|
|
|
|
if all_count == 0:
|
2015-11-26 13:51:04 +00:00
|
|
|
|
return None
|
|
|
|
|
|
2015-11-27 13:42:12 +00:00
|
|
|
|
# If there is only one word, return it, regardless if it’s the
|
|
|
|
|
# same as the last one
|
|
|
|
|
if all_count == 1:
|
|
|
|
|
word = all_words.first()
|
|
|
|
|
# Otherwise, choose a word different from the last one
|
|
|
|
|
else:
|
|
|
|
|
word = last_word
|
|
|
|
|
|
|
|
|
|
while last_word == word:
|
|
|
|
|
word = all_words.order_by('?').first()
|
|
|
|
|
|
2015-11-26 13:51:04 +00:00
|
|
|
|
Draw.objects.create(user=self, word=word, accepted=None)
|
|
|
|
|
|
|
|
|
|
return word
|
|
|
|
|
|
2015-11-26 13:18:01 +00:00
|
|
|
|
class WordsConfig(AppConfig):
|
|
|
|
|
name = 'words'
|
|
|
|
|
|
|
|
|
|
def ready(self):
|
|
|
|
|
User.current_word = _current_word
|
2015-11-26 13:51:04 +00:00
|
|
|
|
User.draw_word = _draw_word
|
2015-11-26 15:56:33 +00:00
|
|
|
|
User.last_draw = _last_draw
|