def disable_user(request: HttpRequest): """disable user [route]: /auth/disable [method]: POST """ user_info: dict = parse_data(request) if not user_info: return failed_api_response(StatusCode.BAD_REQUEST, "Bad request") username = user_info.get("username") password = user_info.get("password") if username is None or password is None: return failed_api_response(StatusCode.INVALID_REQUEST_ARGUMENT, "Bad user information") if not UserModel.objects.filter(username=username).exists(): return failed_api_response(StatusCode.ITEM_NOT_FOUND, "User does not exist") user = UserModel.objects.get(username=username) if not user.check_password(password): return failed_api_response(StatusCode.INVALID_USERNAME_OR_PASSWORD, "User password is wrong") user.is_active = False user.save() return success_api_response({"result": "Ok, user has been diabled."})
def retrieve_ocr_result(request: HttpRequest, project_id: int, result_id: int): """retrieve ocr result [route]: /ocr/project/<int:project_id>/<int:result_id> [method]: GET """ user = request.user project: Project = Project.objects.filter(id=project_id).filter( belong_to=user).first() if not project: return failed_api_response(StatusCode.REFUSE_ACCESS) result: RecognitionResult = project.recognitionresult_set.filter( id=result_id).first() if not result: return failed_api_response(StatusCode.ITEM_NOT_FOUND) res_data = { "id": result.id, "name": result.name, "comment": result.comment, "result": result.result } return success_api_response(res_data)
def send_captcha(request: HttpRequest): """create captcha before registration or reset password [route]: /auth/captcha [method]: POST """ email_info: dict = parse_data(request) if not email_info: return failed_api_response(StatusCode.BAD_REQUEST, "Bad request") email = email_info.get("email") verified_form = VerifiedEmail({"email": email}) if not verified_form.is_valid(): return failed_api_response(StatusCode.INVALID_REQUEST_ARGUMENT, "Bad email address") captcha = random_str(6) cache.set(email, captcha, 5 * 60) send_mail( "Nocode 验证", "验证码:" + captcha + "\n该验证码在五分钟内有效", settings.EMAIL_HOST_USER, [email], ) return success_api_response( {"result": "Ok, confirmation email has been sent"})
def forgot_password(request: HttpRequest): """forgot password and reset [route]: /auth/forgot [method]: POST """ user_info: dict = parse_data(request) if not user_info: return failed_api_response(StatusCode.BAD_REQUEST, "Bad request") username = user_info.get("username") password = user_info.get("password") email = user_info.get("email") captcha = user_info.get("captcha") verified_form = VerifiedUserForm({ "username": username, "password": password, "email": email, "captcha": captcha }) if not verified_form.is_valid(): return failed_api_response(StatusCode.INVALID_REQUEST_ARGUMENT, "Bad information") verified_captcha = cache.get(email) delattr(cache, email) if captcha != verified_captcha: return failed_api_response(StatusCode.INVALID_CAPTCHA, "Captcha not matched") user = UserModel.objects.filter(username=username).first() if not user or user.email != email: return failed_api_response(StatusCode.ITEM_NOT_FOUND, "User not found") user.set_password(password) user.save() return success_api_response({"result": "Ok, password reset"})
def receive_ocr_photo(request: HttpRequest, project_id: int): """receive ocr photo and invoke ocr handler [route]: /ocr/project/<int:project_id> [method]: POST """ user = request.user project: Project = Project.objects.filter(id=project_id).filter( belong_to=user).first() if not project: return failed_api_response(StatusCode.REFUSE_ACCESS) json_text = request.POST.get("json", None) img_file = request.FILES.get("file", None) load = json.loads(json_text) name = load.get("name") comment = load.get("comment") result_json = ocr_handler(img_file) result: RecognitionResult = RecognitionResult(name=name, comment=comment, belong_to=project, result=result_json) result.save() res_data = {"id": result.id, "created_at": result.created_at} return success_api_response(res_data)
def login(request: HttpRequest): """Handle requests which are to obtain jwt token [route]: /auth/ [method]: POST """ data: dict = parse_data(request) if not data or data.get("username") is None or data.get( "password") is None: return failed_api_response(StatusCode.INVALID_REQUEST_ARGUMENT, "Bad login info") user = authenticate(username=data["username"], password=data["password"]) if not user: return failed_api_response(StatusCode.INVALID_USERNAME_OR_PASSWORD, "The username or password is incorrect") return success_api_response( {"access_token": generate_access_token(user.id)})
def remove_ocr_result(request: HttpRequest, project_id: int, result_id: int): """remove ocr result [route]: /ocr/project/<int:project_id>/<int:result_id> [method]: DELETE """ user = request.user project: Project = Project.objects.filter(id=project_id).filter( belong_to=user).first() if not project: return failed_api_response(StatusCode.REFUSE_ACCESS) result: RecognitionResult = project.recognitionresult_set.filter( id=result_id).first() if not result: return failed_api_response(StatusCode.ITEM_NOT_FOUND) result.delete() return success_api_response({"result": "Ok, Recognition result removed"})
def update_project(request: HttpRequest, project_id: int, **kwargs): """update project [route]: /ocr/project/<int:project_id> [method]: PUT """ project = Project.objects.filter(id=project_id).first() if not project: return failed_api_response(StatusCode.ITEM_NOT_FOUND, "Project not found") data: dict = kwargs.get("data") if data.get("name") and len(data.get("name")) == 0: return failed_api_response(StatusCode.INVALID_REQUEST_ARGUMENT, "Project name should not be empty") for key in data.keys(): setattr(project, key, data.get(key)) project.save() return success_api_response({"result": "Ok, Project updated"})
def create_user(request: HttpRequest): """create user [route]: /auth/create [method]: POST """ user_info: dict = parse_data(request) if not user_info: return failed_api_response(StatusCode.BAD_REQUEST, "Bad request") username = user_info.get("username") password = user_info.get("password") email = user_info.get("email") captcha = user_info.get("captcha") verified_form = VerifiedUserForm({ "username": username, "password": password, "email": email, "captcha": captcha }) if not verified_form.is_valid(): return failed_api_response(StatusCode.INVALID_REQUEST_ARGUMENT, "Bad information") verified_captcha = cache.get(email) delattr(cache, email) if captcha != verified_captcha: return failed_api_response(StatusCode.INVALID_CAPTCHA, "Captcha not matched") if UserModel.objects.filter(username=username).exists(): return failed_api_response(StatusCode.ITEM_ALREADY_EXISTS, "Username conflicted") new_user = UserModel.objects.create_user(username=username, password=password, email=email) return success_api_response({"id": new_user.id})
def update_ocr_result(request: HttpRequest, project_id: int, result_id: int, **kwargs): """retrieve ocr result [route]: /ocr/project/<int:project_id>/<int:result_id> [method]: GET """ user = request.user project: Project = Project.objects.filter(id=project_id).filter( belong_to=user).first() if not project: return failed_api_response(StatusCode.REFUSE_ACCESS) result: RecognitionResult = project.recognitionresult_set.filter( id=result_id).first() if not result: return failed_api_response(StatusCode.ITEM_NOT_FOUND) data: dict = kwargs.get("data") for key in data.keys(): setattr(result, key, data.get(key)) result.save() return success_api_response({"result": "Ok, Recognition result updated"})
def remove_project(request: HttpRequest, project_id: int): """remove a project [route]: /ocr/project/<int:project_id> [method]: DELETE """ user = request.user project: Project = Project.objects.filter(id=project_id).filter( belong_to=user).first() if not project: return failed_api_response(StatusCode.REFUSE_ACCESS) project.delete() return success_api_response({"result": "Ok, Project removed"})
def retrieve_project_detail(request: HttpRequest, project_id: int): """retrieve a projects' info [route]: /ocr/project/<int:project_id> [method]: GET """ user = request.user project: Project = Project.objects.filter(id=project_id).filter( belong_to=user).first() if not project: return failed_api_response(StatusCode.REFUSE_ACCESS) results = project.recognitionresult_set.all() res_data = { "name": project.name, "comment": project.comment, "created_at": project.created_at, "result_num": results.count(), "results": list(results.values("id", "name", "comment", "created_at")) } return success_api_response(res_data)
def create_project(request: HttpRequest, **kwargs): """create project [route]: /ocr/project [method]: POST """ info: dict = kwargs.get("data") name = info.get("name") comment = info.get("comment") if len(name) == 0: return failed_api_response(StatusCode.INVALID_REQUEST_ARGUMENT, "Project name is required") project: Project = Project(name=name, comment=comment, belong_to=request.user) project.save() return success_api_response({ "id": project.id, "created_at": project.created_at })
def auth_failed(status: StatusCode, msg: str): return failed_api_response(status, msg)