def store_article_category(self): ''' @descripttion: 新增文章分类 @param {type} @return: ''' # 参数验证 store_article_category_form = StoreArticleCategoryForm.from_json( self.request.json) if not store_article_category_form.validate(): raise APIException(err_msg=store_article_category_form.errors, err_key='validate_err') name = self.request.json['name'] # 判断分类名称是否已存在 if ArticleCategoryModel.query.filter_by(name=name).first(): raise APIException(err_key='site_article_category_exist') # 新增分类入库 article_category_obj = ArticleCategoryModel(name=name) db.session.add(article_category_obj) db.session.commit() return self.return_success({ 'id': article_category_obj.id, 'name': name })
def decode_access_token(access_token): try: payload = jwt.decode(access_token, SECRET_KEY) if 'data' in payload and 'id' in payload['data']: return payload else: raise APIException(err_key='invalid_token') except jwt.ExpiredSignatureError: # Token 过期 raise APIException(err_key='expired_token') except jwt.InvalidTokenError: raise APIException(err_key='invalid_token')
def partical_update(self, id): ''' @description: 更新文章部分字段 @param : @return: 更新后完整的文章资源对象 ''' # body 验证 request_json = self.request.json if not request_json: raise APIException() # 判断文章ID是否存在 article_obj = ArticleModel.query.filter_by(id=id).first() if not article_obj: raise APIException(err_key='article_not_found', http_status_code=http_status.HTTP_404_NOT_FOUND) if 'title' in request_json: article_obj.title = request_json['title'] if 'summary' in request_json: article_obj.summary = request_json['summary'] if 'content_type' in request_json: article_obj.content_type = request_json['content_type'] if 'content' in request_json: article_obj.content = request_json['content'] if 'content_markdown' in request_json: article_obj.content_markdown = request_json['content_markdown'] if 'word_count' in request_json: article_obj.word_count = request_json['word_count'] if 'category_id' in request_json: article_obj.category_id = request_json['category_id'] if 'tags' in request_json: article_obj.tags = request_json['tags'] if 'status' in request_json: article_obj.status = request_json['status'] db.session.commit() return self.return_success({ 'title' : article_obj.title, 'summary' : article_obj.summary, 'content_type' : article_obj.content_type, 'content' : article_obj.content, 'content_markdown': article_obj.content_markdown, 'word_count' : article_obj.word_count, 'category_id' : article_obj.category_id, 'tags' : article_obj.tags, 'status' : article_obj.status, 'created_at' : self.datetime_to_timestamp(article_obj.created_at), 'updated_at' : self.datetime_to_timestamp(article_obj.updated_at) })
def login(self): ''' @descripttion: /admin-api/users/token 用户登录方法 @param body email 用户邮箱 @param body password 用户密码 @return: ''' # 请求参数验证 login_form = LoginForm.from_json(self.request.json) if not login_form.validate(): raise APIException(err_key='validate_err', err_msg=login_form.errors) email = self.request.json['email'] password = self.request.json['password'] # 判断用户是否存在 account_obj = AccountModel.query.filter_by(email=email).first() if not account_obj: # 用户不存在 raise APIException(err_key='account_not_found') account_id = account_obj.id account_password = account_obj.password # 密码校验 if BcryptPassword.check_password(password, account_password): # 登录成功 updated_at = int(time.time()) access_token = JWTAuth.encode_access_token(account_id, updated_at) response_data = { # TODO:refreshToken 功能 'refresh_token': '', 'token_type': 'bearer', 'access_token': bytes.decode(access_token), 'expires_in': 7200 } return self.return_success(response_data) else: # 登录失败 raise APIException(err_key='login_fail')
def store(self): ''' @descripttion: 发布文章 @param {type} @return: ''' # 参数验证 store_article_form = StoreArticleForm.from_json(self.request.json) if not store_article_form.validate(): raise APIException(err_msg=store_article_form.errors, err_key='validate_err') account_id = g.account_id title = self.request.json['title'] summary = self.request.json['summary'] content_type = self.request.json['content_type'] content = self.request.json['content'] content_markdown = self.request.json['content_markdown'] # TODO: word_count 计算 word_count = 0 category_id = self.request.json['category_id'] tags = self.request.json['tags'] status = self.request.json['status'] if 'status' in self.request.json else ArticleModel.status_draft # 新增文章 article_obj = ArticleModel( account_id=account_id, title=title, summary=summary, content_type=content_type, content=content, content_markdown=content_markdown, word_count=word_count, category_id=category_id, tags=tags ) db.session.add(article_obj) db.session.commit() return self.return_success({ 'id' : article_obj.id, 'account_id' : article_obj.account_id, 'title' : article_obj.title, 'summary' : article_obj.summary, 'content_type' : article_obj.content_type, 'content' : article_obj.content, 'content_markdown': article_obj.content_markdown, 'word_count' : article_obj.word_count, 'read' : article_obj.read, 'category_id' : article_obj.category_id, 'tags' : article_obj.tags, 'status' : article_obj.status, 'created_at' : self.datetime_to_timestamp(article_obj.created_at), 'updated_at' : self.datetime_to_timestamp(article_obj.updated_at) })
def delete(self, id): ''' @descripttion: 删除账号 @param path int id 用户ID @return: 空文档 ''' account_obj = AccountModel.query.filter_by(id=id).first() if not account_obj: raise APIException(err_key='account_not_found') account_obj.deleted_at = datetime.datetime.now() db.session.commit() return self.return_success()
def delete(self, id): ''' @descripttion: 删除文章 @param path int 文章ID @return: ''' article_obj = ArticleModel.query.filter_by(id=id).first() if not article_obj: # 文章不存在 raise APIException(err_key='article_not_found', http_status_code=http_status.HTTP_404_NOT_FOUND) article_obj.deleted_at = datetime.datetime.now() db.session.commit() return self.return_success()
def store(self): ''' @description: 新增账号 @param : @return: 新增账号信息 ''' store_account_form = StoreAccountForm.from_json(self.request.json) if not store_account_form.validate(): raise APIException(err_msg=store_account_form.errors, err_key='validate_err') name = self.request.json['name'] email = self.request.json['email'] password = BcryptPassword.encode_password( self.request.json['password']) # TODO:校验合法性 role_id = self.request.json['role_id'] status = self.request.json['status'] # 判断 email 是否已存在 if AccountModel.query.filter_by(email=email).first(): raise APIException(err_key='email_already_exist') # 新增数据库入 account_obj = AccountModel(name=name, email=email, password=password, role_id=role_id, status=status) db.session.add(account_obj) db.session.commit() return self.return_success({ 'id': account_obj.id, 'name': name, 'email': email, 'role_id': role_id, 'status': status })
def partical_update(self, id): ''' @descripttion: 更新账号部分字段 @param {type} @return: 修改后的账号详细信息 ''' request_json = self.request.json if not request_json: raise APIException() # TODO: 参数校验 # 判断账号是否存在 account_obj = AccountModel.query.filter_by(id=id).first() if not account_obj: raise APIException(err_key='account_not_found') if 'name' in request_json: account_obj.name = request_json['name'] if 'email' in request_json: account_obj.email = request_json['email'] if 'role_id' in request_json: account_obj.role_id = request_json['role_id'] if 'status' in request_json: account_obj.status = request_json['status'] if 'password' in request_json: account_obj.password = BcryptPassword.encode_password( request_json['password']) db.session.commit() return self.return_success({ 'id': account_obj.id, 'name': account_obj.name, 'email': account_obj.email, 'role_id': account_obj.role_id, 'status': account_obj.status })
def check_auth_token(): special_route = ['/admin-api/users/token'] if request.method != 'OPTIONS' and request.path not in special_route: access_token = request.headers.get('Authorization') payload = JWTAuth.decode_access_token(access_token) account_id = payload['data']['id'] # 判断用户是否存在 account_obj = AccountModel.query.filter_by(id=account_id).first() if account_obj is None: raise APIException(err_key='invalid_token') g.account_id = account_id g.account_obj = account_obj
def update(self, id): ''' @description: 更新文章 @param : path int id 文章ID @return: 更新后的文章资源数据 ''' # TODO: 参数验证 article_obj = ArticleModel.query.filter_by(id=id).first() if not article_obj: raise APIException(err_key='article_not_found', http_status_code=http_status.HTTP_404_NOT_FOUND) title = self.request.json['title'] summary = self.request.json['summary'] content_type = self.request.json['content_type'] content = self.request.json['content'] content_markdown = self.request.json['content_markdown'] # TODO: word_count 计算 word_count = 0 category_id = self.request.json['category_id'] tags = self.request.json['tags'] status = self.request.json['status'] if 'status' in self.request.json else ArticleModel.status_draft # TODO: 是否增加更新人 updated_user article_obj.title = title article_obj.summary = summary article_obj.content_type = content_type article_obj.content = content article_obj.content_markdown = content_markdown article_obj.word_count = word_count article_obj.category_id = category_id article_obj.tags = tags article_obj.status = status db.session.commit() return self.return_success({ 'title' : article_obj.title, 'summary' : article_obj.summary, 'content_type' : article_obj.content_type, 'content' : article_obj.content, 'content_markdown': article_obj.content_markdown, 'word_count' : article_obj.word_count, 'category_id' : article_obj.category_id, 'tags' : article_obj.tags, 'status' : article_obj.status, 'created_at' : self.datetime_to_timestamp(article_obj.created_at), 'updated_at' : self.datetime_to_timestamp(article_obj.updated_at) })
def index(self): ''' @descripttion: 获取文章列表 @param query int page 页码 @param query int limit 每页数量 @param query string title 文章标题 @param query int category_id 文章分类ID @param query string tags 标签(待定) @return: ''' # 请求参数验证 request_articles = RequestArticles(self.request.args) if not request_articles.validate(): raise APIException(err_key='validate_err', err_msg=request_articles.errors) page = int(self.request.args.get('page', 1)) limit = int(self.request.args.get('limit', 20)) title = self.request.args.get('title', '') category_id = int(self.request.args.get('category_id', 0)) # 构造查询 condition = list() if title: condition.append(ArticleModel.title.like("%" + title + "%")) if category_id: condition.append(ArticleModel.category_id == category_id) article_pages = ArticleModel.query.filter(*condition).paginate(page=page, per_page=limit) articles = list() for article_obj in article_pages.items: articles.append({ 'id' : article_obj.id, 'title' : article_obj.title, 'summary' : article_obj.summary, 'created_at' : self.datetime_to_timestamp(article_obj.created_at), 'updated_at' : self.datetime_to_timestamp(article_obj.updated_at), 'status' : article_obj.status, 'category_id': article_obj.category_id, 'tags' : article_obj.tags }) return self.return_success({ 'pages': article_pages.pages, 'total': article_pages.total, 'articles': articles })
def update_article_category(self, id): ''' @descripttion: 更新文章类型名称 @param path int id 文章类型ID @return: ''' # 参数验证 store_article_category_form = StoreArticleCategoryForm.from_json( self.request.json) if not store_article_category_form.validate(): raise APIException(err_msg=store_article_category_form.errors, err_key='validate_err') name = self.request.json['name'] article_category_obj = ArticleCategoryModel.query.filter_by( id=id).update({'name': name}) db.session.commit() return self.return_success({ 'id': id, 'name': name, })
def show(self, id): ''' @descripttion: 获取文章详情 @param path int artcile_id 文章ID @return: ''' article_obj = ArticleModel.query.filter_by(id=id).first() if not article_obj: raise APIException(err_key='article_not_found', http_status_code=http_status.HTTP_404_NOT_FOUND) return self.return_success({ 'id' : article_obj.id, 'title' : article_obj.title, 'content_type' : article_obj.content_type, 'status' : article_obj.status, 'word_count' : article_obj.word_count, 'content' : article_obj.content, 'content_markdown': article_obj.content_markdown, 'summary' : article_obj.summary, 'tags' : article_obj.tags, 'read' : article_obj.read, 'created_at' : self.datetime_to_timestamp(article_obj.created_at), 'updated_at' : self.datetime_to_timestamp(article_obj.updated_at) })