diff options
-rw-r--r-- | exam/models.py | 2 | ||||
-rw-r--r-- | exam/views.py | 32 | ||||
-rw-r--r-- | templates/exam/question.html | 6 |
3 files changed, 22 insertions, 18 deletions
diff --git a/exam/models.py b/exam/models.py index 247b362..3a8121b 100644 --- a/exam/models.py +++ b/exam/models.py @@ -13,7 +13,7 @@ class Question(models.Model): # An optional one-line summary of the question. summary = models.CharField(max_length=256) # The question text. - question = models.TextField() + description = models.TextField() # Number of points for the question. points = models.IntegerField() diff --git a/exam/views.py b/exam/views.py index 8cc239d..69d7838 100644 --- a/exam/views.py +++ b/exam/views.py @@ -29,13 +29,6 @@ def index(request): user = request.user if user.is_authenticated(): return redirect("/exam/start/") - else: - try: - ip = request.META['REMOTE_ADDR'] - Quiz.objects.get(user_ip=ip) - return redirect("/exam/complete") - except Quiz.DoesNotExist: - pass if request.method == "POST": form = UserRegisterForm(request.POST) @@ -81,7 +74,7 @@ def start(request): try: old_quiz = Quiz.objects.get(user=user) q = old_quiz.current_question() - return show_question(request, q) + return redirect('/exam/%s'%q) except Quiz.DoesNotExist: ip = request.META['REMOTE_ADDR'] key = gen_key(10) @@ -109,8 +102,11 @@ def question(request, q_id): context_instance=ci) def test_answer(func_code, test_code): - exec func_code - exec test_code + obj = compile(func_code, '<string>', mode='exec') + g = {} + exec obj in g + t = compile(test_code, '<string>', mode='exec') + exec t in g def check(request, q_id): user = request.user @@ -125,20 +121,28 @@ def check(request, q_id): # Otherwise we were asked to check. retry = True + tb = None try: test_answer(answer, question.test) - except: + except AssertionError: type, value, tb = sys.exc_info() info = traceback.extract_tb(tb) fname, lineno, func, text = info[-1] - err = "{0}: {1} In code: {2}".format(type.__name__, str(value), text) + text = str(question.test).splitlines()[lineno-1] + err = "{0} {1} in: {2}".format(type.__name__, str(value), text) + except: + type, value = sys.exc_info()[:2] + err = "Error: {0}".format(repr(value)) else: retry = False err = 'Correct answer' - + finally: + del tb + ci = RequestContext(request) if retry: - context = {'question': question, 'error_message': err} + context = {'question': question, 'error_message': err, + 'last_attempt': answer} return render_to_response('exam/question.html', context, context_instance=ci) else: diff --git a/templates/exam/question.html b/templates/exam/question.html index 05e80a8..896a8aa 100644 --- a/templates/exam/question.html +++ b/templates/exam/question.html @@ -1,6 +1,6 @@ <h2> {{ question.summary }} </h2> -<p>{{ question.question }} </p> +<p>{{ question.description }} </p> {% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %} @@ -8,7 +8,7 @@ {% csrf_token %} <textarea rows="20" cols="100" name="answer"> -# Enter your answer here. +{% if last_attempt %}{{last_attempt}}{% else %}# Enter your answer here.{% endif %} </textarea> <br/> @@ -17,4 +17,4 @@ <input type="submit" name="skip" value="Skip question" /> </form> -<p> You have {{quiz.questions_left}} question(s) left. </p>
\ No newline at end of file +<p> You have {{quiz.questions_left}} question(s) left. </p> |