Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Change all code to use Profile.language_pref. Closes #15.

Anything that used to be indexed from a language code is now indexed
from a Language.id value (sometimes written as
Profile.language_pref_id).
  • Loading branch information...
commit 01fee78f0cce012963c22a1c0e395239841d9aae 1 parent b2557f9
@malcolmt authored
View
2  minerva/forms.py
@@ -51,6 +51,6 @@ def __init__(self, *args, **kwargs):
language = kwargs.pop("language", None)
super(ProfileForm, self).__init__(*args, **kwargs)
self.fields["language"].choices = ([("", "<No preference>")] +
- list(Language.objects.values_list("code", "descriptive_name")))
+ list(Language.objects.values_list("id", "descriptive_name")))
self.fields['language'].initial = language
View
23 minerva/questions.py
@@ -14,11 +14,11 @@
('meaning', 'word'),
)
-def get_available_words(query, language, level, last_question):
+def get_available_words(query, language_id, level, last_question):
available_words = SessionProgress.objects.filter(**query)
if len(available_words) < 10:
num_required = 5 - len(available_words)
- pks = word_keys(language, level)
+ pks = word_keys(language_id, level)
current_words = set(available_words.values_list("word", flat=True))
# Collisions should be infrequent, so we should be able to get surplus
sampled_words = set(Word.objects.filter(pk__in=random.sample(pks, num_required + 10)))
@@ -36,7 +36,8 @@ def get_available_words(query, language, level, last_question):
def decrement_weight(progress):
if progress.weight <= 0:
- # if progress is zero we don't want to see this one again, so secretly increment
+ # if progress is zero we don't want to see this one again, so secretly
+ # increment.
progress.weight += 5
progress.save()
return
@@ -69,11 +70,13 @@ def process_answer(query_base, data):
progress_on_correct_answer.weight += 10 + (10 * random.random())
progress_on_correct_answer.save()
-def create_question_complex(query_base, language, level, last_question, num_choices=4):
+def create_question_complex(query_base, language_id, level, last_question,
+ num_choices=4):
# get word with lowest weight, fill session with more words if necessary
query = dict(query_base)
- available_words = get_available_words(query, language, level, last_question)
- query['language'] = language
+ available_words = get_available_words(query, language_id, level,
+ last_question)
+ #query['language'] = language_id
next_word = available_words[0]
possible_answers = random.sample(available_words[1:], num_choices - 1) + [next_word]
random.shuffle(possible_answers)
@@ -110,7 +113,7 @@ def create_question(user, language, level, num_choices=4):
for item in sampled_words]
return question_data, answers
-def word_keys(code, level):
+def word_keys(lang_id, 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
@@ -118,11 +121,11 @@ def word_keys(code, level):
Returns a list of pk values.
"""
- full_list = cache.get(code)
+ full_list = cache.get(lang_id)
if not full_list:
- full_list = tuple(Word.objects.filter(lang_code__code=code). \
+ full_list = tuple(Word.objects.filter(lang_code__id=lang_id). \
values_list("level", "pk").order_by("level"))
- cache.set(code, full_list, settings.LANG_CACHE_TIMEOUT)
+ cache.set(lang_id, 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]]
View
30 minerva/views.py
@@ -7,9 +7,10 @@
def validate_answer(request, query_base):
"""
- For now just update the correct answer with the data
- FIXME - how do I get a question form to validate across fields and against the db
+ For now, just update the correct answer with the data.
"""
+ # FIXME - how do I get a question form to validate across fields and
+ # against the db?
query = dict(query_base)
form = QuestionForm(request.POST)
if not form.is_valid():
@@ -43,10 +44,10 @@ def question(request):
query= {}
if request.user.is_authenticated():
query['student'] = request.user
- language = str(Profile.objects.get(user=request.user).language)
+ language_id = str(Profile.objects.get(user=request.user).language_pref)
else:
query['anon_student'] = request.session.session_key
- language = request.session.get('language', 'zho')
+ language_id = request.session.get('language_id', '1')
if request.method == 'POST':
result = validate_answer(request, query)
@@ -56,7 +57,8 @@ def question(request):
# - a way to select a language.
# - a way to select difficulty level.
# - ...
- problem, answers = create_question_complex(query, language, 1, context.get('prev_id', None))
+ problem, answers = create_question_complex(query, language_id, 1,
+ context.get('prev_id', None))
form = QuestionForm(question=problem, answers = answers)
context['question'] = problem[1]
context['form'] = form
@@ -73,16 +75,16 @@ def status(request):
if request.method == 'POST':
user_profile_form = ProfileForm(request.POST)
if user_profile_form.is_valid():
- language = user_profile_form.cleaned_data['language']
+ language_id = user_profile_form.cleaned_data['language']
changed = False
if request.user.is_authenticated():
profile = Profile.objects.get(user=request.user)
- changed = profile.language != language
- profile.language = language
+ changed = profile.language_pref_id != language_id
+ profile.language_pref_id = language_id
profile.save()
else:
- changed = request.session.get('language', '') != language
- request.session['language'] = language
+ changed = request.session.get('language_id') != language_id
+ request.session['language_id'] = language_id
# clear the session progress, if our language has changed
if changed:
SessionProgress.objects.filter(**query).delete()
@@ -90,12 +92,12 @@ def status(request):
# FIXME - move the setting of the language to the cont
if request.user.is_authenticated():
profile = Profile.objects.get(user=request.user)
- language = profile.language
+ language_id = profile.language_pref_id
else:
- language = request.session.get('language', '')
- user_profile_form = ProfileForm(language = language)
+ language_id = request.session.get('language_id', '')
+ user_profile_form = ProfileForm(language=language_id)
context = {}
- progress = Progress.objects.filter(**query).order_by('correct').reverse()
+ progress = Progress.objects.filter(**query).order_by('-correct')
context['progress'] = progress
context['user_profile_form'] = user_profile_form
return render_to_response('minerva/statistics.html', context,
View
10 user_management/views.py
@@ -9,20 +9,20 @@
def create_user(request):
if request.method == 'POST':
user_create_form = UserCreationForm(request.POST)
- user_profile_form = ProfileForm(request.POST)
+ profile_form = ProfileForm(request.POST)
if user_create_form.is_valid():
user = user_create_form.save()
- if user_profile_form.is_valid():
+ if profile_form.is_valid():
profile = Profile.objects.get(user=user)
- profile.language = user_profile_form.cleaned_data['language']
+ profile.language_pref_id = profile_form.cleaned_data['language']
profile.save()
return HttpResponseRedirect('/login/')
else:
user_create_form = UserCreationForm()
- user_profile_form = ProfileForm()
+ profile_form = ProfileForm()
context = {
'user_create_form': user_create_form,
- 'user_profile_form': user_profile_form
+ 'user_profile_form': profile_form,
}
return render_to_response('user_management/create.html', context,
RequestContext(request))
Please sign in to comment.
Something went wrong with that request. Please try again.