Skip to content

Commit

Permalink
Merge branch 'master' of github.com:malcolmt/remember_me
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewstevens committed Aug 15, 2010
2 parents b93fdeb + d73eaa5 commit b2bc53e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
27 changes: 23 additions & 4 deletions minerva/questions.py
@@ -1,7 +1,12 @@
import bisect
import random

from django.conf import settings
from django.core.cache import cache

from minerva.models import Word


QUESTION_SCENARIOS = (
# Question, answer
('word', 'meaning'),
Expand All @@ -22,10 +27,7 @@ def create_question(user, language, level, num_choices=4):
# TODO: Right now, this is very simple (doesn't take into account the
# user's performance at all). Eventually, it will be a graded selection
# based on what the user finds difficult, etc.

# FIXME: Cache this, for each language.
pks = Word.objects.filter(lang_code__code=language, level=level). \
values_list("pk", flat=True)
pks = word_keys(language, level)
sampled_words = list(Word.objects.filter(pk__in=random.sample(pks,
num_choices)))
question_attribute, answer_attribute = random.choice(QUESTION_SCENARIOS)
Expand All @@ -35,3 +37,20 @@ def create_question(user, language, level, num_choices=4):
for item in sampled_words]
return question_data, answers

def word_keys(code, level):
"""
Retrieves all the pk values for words for the language "code" and "level".
This interacts sensibly with the cache system to avoid unnecessary database
round trips.
Returns a list of pk values.
"""
full_list = cache.get(code)
if not full_list:
full_list = tuple(Word.objects.filter(lang_code__code=code). \
values_list("level", "pk").order_by("level"))
cache.set(code, full_list, settings.LANG_CACHE_TIMEOUT)
start = bisect.bisect_right(full_list, (level - 1, 0))
end = bisect.bisect_left(full_list, (level + 1, 0))
return [item[1] for item in full_list[start : end]]

3 changes: 3 additions & 0 deletions settings.py
Expand Up @@ -101,5 +101,8 @@
"INTERCEPT_REDIRECTS": False,
}

# Cache period, in seconds, for language data (which hardly ever changes)
LANG_CACHE_TIMEOUT = 10800

utils.load_external_settings("host_settings", globals())

0 comments on commit b2bc53e

Please sign in to comment.