def api_attempts_insert(request, question_id): errors = [] question_id = int(question_id) if request.method != 'POST': return HttpResponse(json.dumps({ "kind" : "Expecting POST request" }), mimetype='application/json') if 'content' in request.POST: content = request.POST['content'] if len(content) == 0: errors.append("Length of content is 0") else: errors.append("No content given") if len(errors) > 0: return HttpResponse(json.dumps({ "kind" : "error", "errors" : errors, }), mimetype='application/json') try: question = models.Question.objects.get(template__id=question_id, user=request.user) except models.Question.DoesNotExist: raise exception.UnauthorizedAttemptException(request.user, question_id) if question is None: return HttpResponse(json.dumps({ "kind" : "question#notfound", }), mimetype='application/json') if question.status != 'released': errors.append('Cannot attempt question with status "%s"' % question.status) if len(errors) > 0: return HttpResponse(json.dumps({ "kind" : "error", "errors" : errors, }), mimetype='application/json') question_m = qm.QuestionManager() correct, msg = question_m.check_question(question, content) msghub.register_message(msg, target=question_id, status=correct) # Record the guess. g = models.Guess(user=request.user, question=question, value=content, correct=correct, time_guessed=datetime.datetime.now()) g.save() if correct and question.status != 'solved': question.status = 'solved' question.save() try: question_m = qm.QuestionManager() # Activate the next question. question_m.activate_next(request.user) except exception.NoQuestionReadyException: # TODO cartland: Should we record this? pass response = attempts_get_json(request, attempt_id=g.id, attempt=g) return HttpResponse(json.dumps(response), mimetype='application/json')
def submit_suggestion(request): sugg_q = str(request.POST['question']) ans = str(request.POST['answer']) # Record the suggestion. s = users.Suggestion(user=request.user, question=sugg_q, answer=ans, time_submitted=datetime.datetime.now()) s.save() msghub.register_message('Thanks for submitting a question!') return redirect('/dash')
def check_q(request): try: qid = int(request.POST['qid']) answer = str(request.POST['answer']) if len(answer.strip()) == 0: return render_to_response('error.tpl', { 'errors' : ['Please provide an answer before submitting.',] }) # Check to make sure the current user is allowed to answer this question. try: question = users.Question.objects.get(id=qid, user=request.user) except users.Question.DoesNotExist: raise exception.UnauthorizedAttemptException(request.user, qid) question_m = qm.QuestionManager() correct, msg = question_m.check_question(question, answer) msghub.register_message(msg, target=qid, status=correct) # Record the guess. g = users.Guess(user=request.user, question=question, value=answer, correct=correct, time_guessed=datetime.datetime.now()) g.save() # If correct, change the status of the question and select a next candidate. # Make sure that the question hasn't been solved already! if correct and question.status != 'solved': question.status = 'solved' question.save() try: question_m = qm.QuestionManager() # Activate the next question. question_m.activate_next(request.user) except exception.NoQuestionReadyException: # TODO cartland: Should we record this? pass return redirect('/question/status/%d' % g.id) # Either the QID or the answer value wasn't set. except KeyError: return render_to_response('error.tpl', { 'errors' : ['There was a problem retrieving information about your guess. Please refresh the question page and try submitting again.',] }) except exception.UnauthorizedAttemptException: return render_to_response('error.tpl', { 'errors' : ['You haven\'t unlocked that question yet.',] })
def submit_hint(request, tid): template = users.QuestionTemplate.objects.get(id=tid) # Save the hint! try: user_q = users.Question.objects.exclude(status='retired').get(template=template, user=request.user) prev_hints = users.QuestionHint.objects.filter(template=template, src=request.user) # Check that the problem has been solved and that the user hasn't provided # any hints for this question already. if user_q.status == 'solved':# and len(prev_hints) is 0: users.QuestionHint(template=template, src=request.user, text=request.POST['hinttext']).save() msghub.register_message('Thanks for providing a hint!', template, True) # Error: the user has already provided a hint. elif len(prev_hints) > 0: msghub.register_error(8, template) # Error: the user hasn't answered the question yet. else: msghub.register_error(7, template) except users.Question.DoesNotExist: msghub.register_error(7, template) return redirect('/dash')
def api_attempts_insert(request, question_id): errors = [] question_id = int(question_id) if request.method != 'POST': return HttpResponse(json.dumps({"kind": "Expecting POST request"}), mimetype='application/json') if 'content' in request.POST: content = request.POST['content'] if len(content) == 0: errors.append("Length of content is 0") else: errors.append("No content given") if len(errors) > 0: return HttpResponse(json.dumps({ "kind": "error", "errors": errors, }), mimetype='application/json') try: question = models.Question.objects.get(template__id=question_id, user=request.user) except models.Question.DoesNotExist: raise exception.UnauthorizedAttemptException(request.user, question_id) if question is None: return HttpResponse(json.dumps({ "kind": "question#notfound", }), mimetype='application/json') if question.status != 'released': errors.append('Cannot attempt question with status "%s"' % question.status) if len(errors) > 0: return HttpResponse(json.dumps({ "kind": "error", "errors": errors, }), mimetype='application/json') question_m = qm.QuestionManager() correct, msg = question_m.check_question(question, content) msghub.register_message(msg, target=question_id, status=correct) # Record the guess. g = models.Guess(user=request.user, question=question, value=content, correct=correct, time_guessed=datetime.datetime.now()) g.save() if correct and question.status != 'solved': question.status = 'solved' question.save() try: question_m = qm.QuestionManager() # Activate the next question. question_m.activate_next(request.user) except exception.NoQuestionReadyException: # TODO cartland: Should we record this? pass response = attempts_get_json(request, attempt_id=g.id, attempt=g) return HttpResponse(json.dumps(response), mimetype='application/json')