def on_get(self, req, res, confirmation_id): LOG.info(f'Enter /v2/didtx/confirmation_id/{confirmation_id}') try: rows = Didtx.objects(id=confirmation_id) if rows: row = [each.as_dict() for each in rows][0] self.on_success(res, row) else: LOG.info(f"Error /v2/didtx/id/{confirmation_id}") raise NotFoundError() except Exception as e: LOG.info(f"Error /v2/didtx/id/{confirmation_id}: {str(e)}") raise NotFoundError()
def get_results_user(): user_id = get_user_cookie() if not user_id: raise ForbiddenError() user_questions = get_questions_for_user(user_id) if not user_questions: raise NotFoundError() aggd = [] for q in user_questions: stats = q["stats"] aggd.append(stats) num_pauses = 0 wpm = 0 for each in aggd: num_pauses += int(each["number_of_pauses"]) wpm += int(each["words_per_min"]) return { "results": { "words_per_minute": { "total": wpm, "avg": wpm / len(aggd) }, "number_of_pauses": { "total": num_pauses, "avg": num_pauses / len(aggd), }, }, }
def get_task_status(task_id): get_user_cookie(True) task = process_audio_stats.AsyncResult(task_id) if not task: raise NotFoundError() response = {"state": task.state, "result": {}} if task.state == "PENDING": response["current"] = 0 response["total"] = 100 response["status"] = "pending" elif task.state != "FAILURE": response["current"] = task.info.get("current", 0) response["total"] = task.info.get("total", 100) response["status"] = task.info.get("status", "") if "result" in task.info: response["result"] = task.info["result"] else: response["current"] = 100 response["total"] = 100 response["status"] = str(task.info) return response, 200
def item(self, id): if not self._datamodel_: raise CodeError("重写该方法或定义 __database__ 属性") item = self._datamodel_.query.get(id) if item is None: raise NotFoundError("{}:{}".format(self._datamodel_.__tablename__, id)) self.set_response(data=self.to_dict(item))
def on_get(self, req, res, did): LOG.info(f'Enter /v2/didtx/recent/did/{did}') rows = Didtx.objects(did=did.replace("did:elastos:", "").split("#") [0]).order_by('-modified')[:5] if rows: obj = [each.as_dict() for each in rows] self.on_success(res, obj) else: LOG.info(f"Error /v2/didtx/recent/did/{did}") raise NotFoundError()
def get_user_questions(): """ GET /api/questions Fetch questions for the current user if cookie is found, else return a 404 """ user_id = get_user_cookie(True) questions = get_questions_for_user(user_id) if not questions: raise NotFoundError() return {"questions": questions}, 200
def get_results_question(question_id): user_id = get_user_cookie(True) question = get_question_by_id(question_id) if not question: raise NotFoundError() if question["user_id"] != user_id: app.logger.warning( "Revoked unauthorized access to question[%s] by user[%s]", question_id, user_id, ) raise ForbiddenError() return {"result": question["stats"]}, 200
def get_question(question_id): """ GET /api/questions/<question_id> Fetch information about a specific question matching question_id, else return a 404 """ user_id = get_user_cookie(True) question = get_question_by_id(question_id) if not question: raise NotFoundError() if question["user_id"] != user_id: app.logger.warning( "Revoked unauthorized access to question[%s] by user[%s]", question_id, user_id, ) raise ForbiddenError() return {"question": question}, 200
def on_delete(self, req, res): raise NotFoundError(method="DELETE", url=req.path)
def on_put(self, req, res): raise NotFoundError(method="PUT", url=req.path)
def on_get(self, req, res): if req.path == "/": res.status = falcon.HTTP_200 res.body = self.to_json(self.HELLO_WORLD) else: raise NotFoundError(method="GET", url=req.path)
def get_user_by_name(cls, username: str): user = cls.query.filter(cls.username == username).first() if user is None: raise NotFoundError("{} username:{}".format( cls.__tablename__, username)) return user
def not_found_error(error): return NotFoundError().message
def get_or_404(cls, id: str): item = cls.query.get(id) if item is None: raise NotFoundError("{}:{}".format(cls.__tablename__, id)) return item