示例#1
0
def question_page(request, question_id):
    try:
        question = Question.objects.filter(valid=True).get(pk=question_id)
    except ObjectDoesNotExist:
        messages.add_message(request,
                             info_messages['question does not exist'][0],
                             info_messages['question does not exist'][1])
        return HttpResponseRedirect(reverse('user_session:login'))

    question_status_obj = QuestionStatus.objects.filter(
        team_id=request.user).filter(question_id=question)
    is_answered = question_if_answered(request, question_id, QuestionStatus,
                                       question)
    if is_answered is not None:
        return is_answered

    #Question not opened
    if len(question_status_obj) == 0:
        qs = QuestionStatus(team_id=request.user,
                            question_id=question,
                            question_status='OP')
        qs.save()

    if question.has_context:
        return HttpResponseRedirect(question_urls[question_id])

    return render(request, QUESTIONS_DIR + question.source_file)
示例#2
0
def submit_answer(request, question_id):
    try:
        question = Question.objects.get(pk=question_id)
        if request.method == "POST" and "answer" in request.POST:
            answer = request.POST["answer"]
            if question.answer == answer:
                try:
                    question = Question.objects.get(pk=question_id)
                except ObjectDoesNotExist:
                    return HttpResponse("invalid")
                question_status_obj = (
                    QuestionStatus.objects.filter(team_id=request.user)
                    .filter(question_id=question)
                    .filter(question_status="AW")
                )
                if len(question_status_obj) != 0:
                    return HttpResponse("as")

                qs = QuestionStatus(team_id=request.user, question_id=question, question_status="AW")
                qs.save()
                team = TeamDetail.objects.filter(team=qs.team_id)[0]
                team.points += qs.question_id.points
                team.save()
                return HttpResponse("wow")
            else:
                return HttpResponse("tryagain")
            return HttpResponse("error")

    except ObjectDoesNotExist:
        pass
    return HttpResponse("error")
示例#3
0
def routeCommand(commandString, username, channel):
    if commandString.strip() == "":
        return flask.render_template("help.txt") + "\n"
    commandPieces = commandString.split()
    command = commandPieces[0]
    commandArgs = commandPieces[1:]
    if command == "list":
        status = QuestionStatus.pending
        if len(commandArgs) > 0:
            statusName = commandArgs[0]
            status = QuestionStatus.statusForName(statusName)
        return listForUser(username, status)
    elif command == "publish":
        if len(commandArgs) == 2:
            channel = commandArgs[1]
        questionNumber = commandArgs[0]
        return publish(username, trimNumber(commandArgs[0]), channel)
    elif command == "republish":
        if len(commandArgs) == 2:
            channel = commandArgs[1]
        questionNumber = commandArgs[0]
        return publish(username, trimNumber(commandArgs[0]), channel, republish=True)
    elif command == "delete":
        return delete(username, trimNumber(commandArgs[0]))
    elif command == "undelete":
        return undelete(username, trimNumber(commandArgs[0]))
    elif command == "help":
        return flask.render_template("help.txt") + "\n"
    elif len(commandArgs) > 0:
        userToAsk = command
        return askUser(userToAsk[1:] if userToAsk[0] == '@' else userToAsk, re.sub("^{}\s*".format(userToAsk), "", commandString))
示例#4
0
def routeCommand(commandString, username, channel):
    if commandString.strip() == "":
        return flask.render_template("help.txt") + "\n"
    commandPieces = commandString.split()
    command = commandPieces[0]
    commandArgs = commandPieces[1:]
    if command == "list":
        status = QuestionStatus.pending
        if len(commandArgs) > 0:
            statusName = commandArgs[0]
            status = QuestionStatus.statusForName(statusName)
        return listForUser(username, status)
    elif command == "publish":
        if len(commandArgs) == 2:
            channel = commandArgs[1]
        questionNumber = commandArgs[0]
        return publish(username, trimNumber(commandArgs[0]), channel)
    elif command == "republish":
        if len(commandArgs) == 2:
            channel = commandArgs[1]
        questionNumber = commandArgs[0]
        return publish(username,
                       trimNumber(commandArgs[0]),
                       channel,
                       republish=True)
    elif command == "delete":
        return delete(username, trimNumber(commandArgs[0]))
    elif command == "undelete":
        return undelete(username, trimNumber(commandArgs[0]))
    elif command == "help":
        return flask.render_template("help.txt") + "\n"
    elif len(commandArgs) > 0:
        userToAsk = command
        return askUser(userToAsk[1:] if userToAsk[0] == '@' else userToAsk,
                       re.sub("^{}\s*".format(userToAsk), "", commandString))
示例#5
0
def submit_answer(request, question_id):
    if request.method == 'POST' and 'answer' in request.POST:
        try:
            question = Question.objects.filter(valid=True).get(pk=question_id)
        except ObjectDoesNotExist:
            return HttpResponse(info_messages['invalid_question'][1])
        question_status_obj = QuestionStatus.objects.filter(
            team_id=request.user).filter(question_id=question)
        #Question Already Answered
        if len(question_status_obj.filter(question_status='AW')) != 0:
            return HttpResponse(info_messages['answered'][1])

        _log = Log.objects.filter(team_id=request.user).filter(
            question_id=question)
        if len(_log) == 0:
            log = Log(
                team_id=request.user,
                question_id=question,
            )
        else:
            log = _log[0]
        log.submission_time = timezone.now()

        #Question Already Opened
        if len(question_status_obj) != 0:
            qs = question_status_obj[0]
        else:
            qs = QuestionStatus(team_id=request.user,
                                question_id=question,
                                question_status='OP')

        answer = request.POST['answer']
        if question.answer == answer:
            log.solved = True
            qs.question_status = 'AW'
            qs.submission_time = timezone.now()
            qs.save()
            team = TeamDetail.objects.filter(team=qs.team_id)[0]
            team.points += qs.question_id.points
            team.save()
            log.save()
            return HttpResponse(info_messages['correct answer'][1])
        else:
            log.count_fail += 1
            qs.submission_time = timezone.now()
            qs.save()
            log.save()
            return HttpResponse(info_messages['incorrect answer'][1])
    return HttpResponse(info_messages['invalid_request'][1])
示例#6
0
def question_page(request, question_id):
    try:
        question = Question.objects.filter(valid=True).get(pk=question_id)
    except ObjectDoesNotExist:
        messages.add_message(
            request, info_messages["question does not exist"][0], info_messages["question does not exist"][1]
        )
        return HttpResponseRedirect(reverse("user_session:login"))

    question_status_obj = QuestionStatus.objects.filter(team_id=request.user).filter(question_id=question)
    is_answered = question_if_answered(request, question_id, QuestionStatus, question)
    if is_answered is not None:
        return is_answered

        # Question not opened
    if len(question_status_obj) == 0:
        qs = QuestionStatus(team_id=request.user, question_id=question, question_status="OP")
        qs.save()

    if question.has_context:
        return HttpResponseRedirect(question_urls[question_id])

    return render(request, QUESTIONS_DIR + question.source_file)
示例#7
0
def submit_answer(request, question_id):
    if request.method == "POST" and "answer" in request.POST:
        try:
            question = Question.objects.filter(valid=True).get(pk=question_id)
        except ObjectDoesNotExist:
            return HttpResponse(info_messages["invalid_question"][1])
        question_status_obj = QuestionStatus.objects.filter(team_id=request.user).filter(question_id=question)
        # Question Already Answered
        if len(question_status_obj.filter(question_status="AW")) != 0:
            return HttpResponse(info_messages["answered"][1])

        _log = Log.objects.filter(team_id=request.user).filter(question_id=question)
        if len(_log) == 0:
            log = Log(team_id=request.user, question_id=question)
        else:
            log = _log[0]
        log.submission_time = timezone.now()

        # Question Already Opened
        if len(question_status_obj) != 0:
            qs = question_status_obj[0]
        else:
            qs = QuestionStatus(team_id=request.user, question_id=question, question_status="OP")

        answer = request.POST["answer"]
        if question.answer == answer:
            log.solved = True
            qs.question_status = "AW"
            qs.submission_time = timezone.now()
            qs.save()
            team = TeamDetail.objects.filter(team=qs.team_id)[0]
            team.points += qs.question_id.points
            team.save()
            log.save()
            return HttpResponse(info_messages["correct answer"][1])
        else:
            log.count_fail += 1
            qs.submission_time = timezone.now()
            qs.save()
            log.save()
            return HttpResponse(info_messages["incorrect answer"][1])
    return HttpResponse(info_messages["invalid_request"][1])