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()}
async def get_question(event, ctx): return { 'question': QuestionModel.get(event.pathParameters.qid).strip_private() }