def get(q_id): query = Question.objects(question_id=q_id) if query: question = query[0] return question.to_dict() else: return None
def delete(q_id): question = Question.objects(question_id=q_id).first() q_id = question.question_id if question: question.delete() log.debug("## Question Deleted : ID {}".format(q_id)) return True, None else: return False, "Question not found"
def index(): # Slice of to pagination # List of filter by get args: # Example: /admin/question/?page=1&name_icontains=apple data = request.args.to_dict() # Type of filter engine_filter = {'content__icontains': str} # Prepare filter criteria = {} for k in data: if k in engine_filter: criteria[k] = engine_filter[k](data[k]) pagination = Paginate('admin.question.index', count=len(Question.objects(**criteria)), per_page=10) page = pagination.get_page() questions = Question.objects(**criteria)[(page-1) * 10:page * 10] return render.template('admin/question/index.html', questions=questions, pagination=pagination)
def collect_questions(): questions = [] older_than = datetime.datetime.utcnow() - datetime.timedelta(days=COOLDOWN_DURATION) for qt_id in range(1, MAX_NUMBER_OF_DIFFICULTY + 1): question = Question.objects( Q(question_type=qt_id) & Q(modified_at__lte=older_than) ).order_by('modified_at').first() if question: questions.append(question) return questions
def update(post_data, q_id): query = Question.objects(question_id=q_id) if query: question = query[0] question.question_text = post_data.get("question_text") question.sample_input = post_data.get("sample_input") question.sample_output = post_data.get("sample_output") question.explanation = post_data.get("explanation") question.question_type = post_data.get("question_type") try: question.update_timestamp() except Exception as e: return response.failure({"status": "Failure occured.", "error": str(e)}) log.debug("## Question Updated: ID {}".format(question.question_id)) return response.success(question.to_dict()) else: return response.failure({"error": "Question not found"})
def list_questions(): query = Question.objects() return_list = [] for question in query: return_list.append(question.to_dict()) return return_list