From 5f26adffda81efd315e69e6a2acb81195567e0ba Mon Sep 17 00:00:00 2001 From: Per-Erik Persson Date: Wed, 28 Sep 2011 12:31:08 +0200 Subject: [PATCH 1/2] Enable questions to reference the anser of a previous answered question. The magic string subst_with_ans_1 will insert the answer from question one into your current question. --- example/settings.py | 2 ++ questionnaire/models.py | 4 +++- questionnaire/views.py | 16 +++++++++++++--- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/example/settings.py b/example/settings.py index cf9574c..b3767f1 100644 --- a/example/settings.py +++ b/example/settings.py @@ -92,5 +92,7 @@ LANGUAGES = ( ('de', 'Deutsch'), ) +REPLACEMENTSTRING = u'subst_with_ans_' + try: from local_settings import * except: pass diff --git a/questionnaire/models.py b/questionnaire/models.py index fa65876..727f307 100644 --- a/questionnaire/models.py +++ b/questionnaire/models.py @@ -164,7 +164,9 @@ class RunInfo(models.Model): if key in cookies: del cookies[key] else: - cookies[key] = str(value) + if type(value) == 'int': + value=str(value) + cookies[key] = value cstr = json.dumps(cookies) if len(cstr) > 512: # XXX - hard coded to match cookie length above raise Exception("Cannot set cookie. No more space in cookie jar!") diff --git a/questionnaire/views.py b/questionnaire/views.py index fe6165b..f2ab83e 100644 --- a/questionnaire/views.py +++ b/questionnaire/views.py @@ -314,6 +314,10 @@ def show_questionnaire(request, runinfo, errors={}): jstriggers = [] qvalues = {} alignment=4 + # initialize qvalues + for k,v in runinfo.get_cookiedict().items(): + qvalues[k] = v + for question in questions: Type = question.get_type() _qnum, _qalpha = split_numal(question.number) @@ -328,6 +332,15 @@ def show_questionnaire(request, runinfo, errors={}): 'qalpha_class' : _qalpha and (ord(_qalpha[-1]) % 2 \ and ' alodd' or ' aleven') or '', } +#If the question has a magic string surrounded by spaces that refers to an answer to a previous question, fetch the answer and replace the magic string +#To be able to fetch the cookie with the answer it has to be stored using additional checks +#At the moment it only works for english. + replacementtext=settings.REPLACEMENTSTRING + questionnumberpos=question.text.find(replacementtext) + if questionnumberpos <> -1: + questionnumber=question.text_en[questionnumberpos:].split(' ')[0].replace(replacementtext,'') + if questionnumber in qvalues.keys(): + question.text_en=question.text_en.replace(replacementtext+questionnumber,qvalues[questionnumber]) if not question.newline(): alignment = max(alignment, calc_alignment(question.text)) @@ -362,9 +375,6 @@ def show_questionnaire(request, runinfo, errors={}): if runinfo.questionset.sortid != 0: progress = get_progress(runinfo.questionset.sortid / float(total)) - # initialize qvalues - for k,v in runinfo.get_cookiedict().items(): - qvalues[k] = v if request.POST: for k,v in request.POST.items(): if k.startswith("question_"): From 17021b402d227af1efcf4aeec3b639b2a96940d7 Mon Sep 17 00:00:00 2001 From: Per-Erik Persson Date: Wed, 28 Sep 2011 15:45:46 +0200 Subject: [PATCH 2/2] convert integers to str to allow them to be exported --- questionnaire/views.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/questionnaire/views.py b/questionnaire/views.py index 8f9983c..57b8097 100644 --- a/questionnaire/views.py +++ b/questionnaire/views.py @@ -567,6 +567,8 @@ def answer_export(questionnaire, answers=None): subject = answer.subject row = [""] * len(headings) ans = answer.split_answer() + if type(ans) == int: + ans = str(ans) for choice in ans: col = None if type(choice) == list: