def get_passage_id(name): """ Passage orm object :param name: name of the passage :type name: str :return: None or id of the object :rtype: Union[None,int] """ passage_objs = Passage.query.filter(Passage.name == name).all() if len(passage_objs) > 1: raise AppError(*ERROR_MULTIPLE_OBJECT_FOUND) if len(passage_objs) == 0: raise AppError(*ERROR_NO_OBJECT_FOUND) return passage_objs[0].id
def get_question_id(**kwargs): """ Passage orm object :param name: name of the passage :type name: str :return: None or id of the object :rtype: Union[None,int] """ question_objs = Question.query.filter_by(**kwargs).all() if len(question_objs) > 1: raise AppError(*ERROR_MULTIPLE_OBJECT_FOUND) if len(question_objs) == 0: raise AppError(*ERROR_NO_OBJECT_FOUND) return question_objs[0].question_id
def create_passage(name): """ :param name: :return: """ exec_rec = Passage.query.filter( Passage.name == name.encode("utf-8")).one_or_none() if exec_rec: raise AppError(*ERROR_DUPLICATE_PASSAGE_TITLE) passage = Passage() passage.name = name with transactional_session() as sess: sess.add(passage)
def create_answer(question_id, option_id): """ :param question_id: :param option_id: :return: """ answer_obj = Answer() answer_obj.question_id = question_id answer_obj.option_id = option_id try: with transactional_session() as sess: sess.add(answer_obj) except: raise AppError(*ERROR_MULTIPLE_OPTION_CREATE)
def create_multiple_option(question_id, option_text): """ :param question_id: :param option_id: :param option_text: :return: """ multi_obj = MultipleOption() multi_obj.question_id = question_id multi_obj.option_text = option_text try: with transactional_session() as sess: sess.add(multi_obj) except: raise AppError(*ERROR_MULTIPLE_OPTION_CREATE)
def create_question(passage_id, question): """ :param passage_id: :param question: :return: """ question_obj = Question() question_obj.passage_id = passage_id question_obj.question = question try: with transactional_session() as sess: sess.add(question_obj) except: raise AppError(*ERROR_QUESTION_CREATE)
def create_paragraph(**kwargs): """ create the Paragraph entity :param kwargs: dictionary containing the fields about the paragraph entity :type kwargs: dict :return: None if adding succeed else dictionary containing {"overwrite":true} :rtype Union[None,dict] """ para = Paragraph() para.passage_id = kwargs["passage_id"] para.paragraph = kwargs["paragraph"].encode("utf-8") try: with transactional_session() as sess: sess.add(para) except: raise AppError(*ERROR_PARAGRAPH_CREATE)