예제 #1
0
def populate_questions():
    for question, details in questions.items():
        question_model = QuestionModel.get_by_question(question)
        if not question_model:
            # New Question - add it
            yes = details['yes']
            no = details['no']
            why = details['why']
            question_model = QuestionModel(question=question,
                                           answer_yes=yes,
                                           answer_no=no,
                                           why=why)
            question_model.save()
예제 #2
0
async def submit(event, ctx, user, *args, **kwargs):
    data = event.body
    uid = str(user['_id'])
    qid = 'q' + str(uuid.uuid4())[:13]
    prev_q = data.get('prev_q', None)
    display_choice = data['display_choice']
    title = data['title']
    if prev_q:
        full_prev_q = QuestionModel.get(prev_q)
        if full_prev_q.uid != uid:
            raise Exception("prev_q does not match!")
    question = data['question']
    if not (20 < len(question) <= 4000):
        raise Exception("Your question is too large!")
    if not (10 < len(title) <= 200):
        raise Exception("Your title is too long!")
    ts = datetime.datetime.now()
    params = {
        'qid': qid,
        'uid': uid,
        'display_name': gen_display_name(user, data.display_choice),
        'is_anon': display_choice == "anon",
        'question': question,
        'title': title,
        'ts': ts
    }
    params.update({'prev_q': prev_q} if prev_q else {})
    q = QuestionModel(**params)
    q.save()
    update_actions = [
        UserQuestionsModel.qs.set((UserQuestionsModel.qs | []).prepend(
            [UserQuestionLogEntry(ts=ts, qid=q.qid)]))
    ]
    UserQuestionsModel(uid="global").update(actions=update_actions)
    UserQuestionsModel(uid=uid).update(actions=update_actions)
    return {'submitted': True, 'qid': qid, 'question': q.to_python()}