예제 #1
0
def find_answers(question):
    if(question.media):
        t = (question.question, question.media)
        c.execute('SELECT answers.answer FROM questions, answers WHERE \
            questions.question = ? AND questions.id = answers.question AND \
            questions.media = ? ORDER BY answers.id ASC', t)
    else:
        t = (question.question, )
        c.execute('SELECT answers.answer FROM questions, answers WHERE \
            questions.question = ? AND questions.id = answers.question \
            ORDER BY answers.id ASC', t)
    answers = []
    row = c.fetchone()
    aid = 1
    while(row):
        if(question.type == Question.Type.multiple_choice):
            for answer in question.answers:
                if(answer.answer == row[0]):
                    answers.append(answer)
        elif(question.type == Question.Type.text):
            answer = Answer(aid)
            answer.answer = row[0]
            answers.append(answer)
            aid += 1
        row = c.fetchone()
    return answers
예제 #2
0
def _checkbox_passthrough(func):
    if isinstance(func, types.FunctionType):
        return Checkbox(_text_from_func(func),
                        corrects=Answer(repr(func()), is_code=True),
                        wrongs=[],
                        is_code=True)
    return func
예제 #3
0
    def update_database(self, packet):
        # packet is already validated
        if packet["TYPE"] == "QUESTION":
            # TODO username instead of ip
            user = User(packet["ACTOR"], packet["ACTOR"])
            new_question = Question(user, packet["TITLE"], packet["CONTENT"])

            if not (packet["TITLE"] in self.questions):
                self.questions[packet["TITLE"]] = new_question

        elif packet["TYPE"] == "ANSWER":
            if packet["QUESTION_TITLE"] in self.questions:
                user = User(packet["ACTOR"], packet["ACTOR"])
                answer_object = Answer(user, packet["CONTENT"])
                self.questions[packet["QUESTION_TITLE"]].answer(answer_object)

        elif packet["TYPE"] == "VOTE":
            assert packet["VOTE"] == "+" or packet["VOTE"] == "-"
            question_title = packet["QUESTION_TITLE"]
            if question_title in self.questions:
                user = User(packet["ACTOR"], packet["ACTOR"])
                if packet["VOTE"] == "+":
                    self.questions[question_title].upvote(user)
                elif packet["VOTE"] == "-":
                    self.questions[question_title].downvote(user)
예제 #4
0
def _fill_in_the_blank_passthrough(func, use_answer=True):
    if isinstance(func, types.FunctionType):
        if use_answer:
            return FillInTheBlank(_text_from_func(func),
                                  correct=Answer(repr(func()), is_code=True),
                                  is_code=True)
        return FillInTheBlank(_text_from_func(func),
                              correct=None,
                              is_code=True)
    return func
예제 #5
0
 def dec(func):
     func = _multiple_choice_passthrough(func)
     func.answers.append(Answer(answer, correct=False, is_code=is_code))
     func = _checkbox_if_multiple_answers_passthrough(func)
     func = _multiple_choice_if_wrongs_passthrough(func)
     return func
예제 #6
0
def yeahok(func):
    """Multiple choice question with just one answer: the right one"""
    return MultipleChoice(_text_from_func(func),
                          Answer(repr(func()), is_code=True),
                          is_code=True)
예제 #7
0
def false(func):
    """The function below is a True/False question, and the answer is False"""
    return MultipleChoice(_text_from_func(func),
                          Answer('False'),
                          Answer('True'),
                          is_code=True)
예제 #8
0
def no(func):
    """The function below is a yes/no question, and the answer is no"""
    return MultipleChoice(_text_from_func(func),
                          Answer('no'),
                          Answer('yes'),
                          is_code=True)
예제 #9
0
def question(func):
    """Make the function below a question, with what it returns as the answer"""
    return FillInTheBlank(_text_from_func(func),
                          correct=Answer(repr(func()), is_code=True),
                          is_code=True)
예제 #10
0
def _multiple_choice_passthrough(func):
    if isinstance(func, types.FunctionType):
        return MultipleChoice(_text_from_func(func),
                              correct=Answer(repr(func()), is_code=True),
                              is_code=True)
    return func
예제 #11
0
def display_answer(func):
    """Uses what is printed to stdout when the function is run as the correct answer"""
    ans = Answer(_stdout_output(func), correct=True, is_code=True)
    func = _fill_in_the_blank_passthrough(func, use_answer=False)
    func.answers.append(ans)
    return func
예제 #12
0
def get_answer(question_id, choice_id=0):
    answer_response = requests.post(
        ANSWER_URL.format(id=question_id), json={"choices": [choice_id]}
    )

    return Answer(answer_response.content, choice_id)