class getGirlsTags(Resource): ''' 返回图片标签 ''' @marshal_with(true_serializer(response_tags)) def get(self): ''' 整个标签集合 :return: ''' tags = girls_tag.query.all() list = [] for tag in tags: list.append({ 'id': tag.id, 'title': tag.title, 'tag': tag.tag, 'cover_img': tag.cover_img, 'local_cover_img': "/static/girlsImg/%s" % tag.local_cover_img, }) return trueReturn({'list': list}, 'success')
class getGirlsImg(Resource): ''' 返回图片集合 albumId(必填): int // 相册id ''' @marshal_with(true_serializer(response_imgs)) def get(self): ''' 返回对应相册的图集 :return: ''' try: parser = reqparse.RequestParser() parser.add_argument('albumId', type=int, location="headers") args = parser.parse_args() album_id = args['albumId'] imgs = girls_img.query.filter_by(girls_album_id=album_id) count = girls_img.query.filter_by(girls_album_id=album_id).count() return trueReturn({ 'list': imgs, 'info': { 'count': count } }, 'success') except Exception as e: print(e) return falseReturn('', e)
class getAllImgs(Resource): ''' 返回全部的图片 ''' @marshal_with(true_serializer(response_allImgs)) def get(self): ''' 返回整个分页图片 :return: ''' try: # 注册入参 parser = reqparse.RequestParser() parser.add_argument('pageIndex', type=int, location=['headers', 'args']) parser.add_argument('pageSize', type=int, location=['headers', 'args']) # 获取入参 args = parser.parse_args() pageIndex = args['pageIndex'] or 1 pageSize = args['pageSize'] or 12 search_img = girls_img.query.order_by(girls_img.id.asc()) imgs = search_img.paginate(pageIndex, per_page=pageSize, error_out=False) count = imgs.total totalPage = math.floor(count / pageSize) list = [] for item in imgs.items: list.append({ 'id': item.id, 'girls_album_id': item.girls_album_id, 'img_url': item.img_url, 'local_img_url': "/static/girlsImg/%s" % item.local_img_url, }) return trueReturn( { 'list': list, 'pageInfo': { 'pageIndex': pageIndex, 'pageSize': pageSize, 'count': count, 'totalPage': totalPage, } }, 'success') except Exception as e: raise Exception(e) print(e) return falseReturn('', e)
class CreateCategory(Resource): ''' 创建分类,目前这个版本分类做固定分类,数据库存固定字段。 ''' @marshal_with(true_serializer(response_category_field)) def post(self): parser = reqparse.RequestParser() parser.add_argument('name', type=str) category = parser.parse_args() c = dict(category) ctg = Category(**c) Category.add(ctg) return trueReturn(ctg, 'success')
class getComment(Resource): ''' 获取评论 ''' @marshal_with(true_serializer(response_getcomment_field)) def post(self): parser = reqparse.RequestParser() parser.add_argument('article_id', type=int) article_id = parser.parse_args()['article_id'] filter = {Comment.article_id == article_id, Comment.parent_id == None} comment = Comment.query.filter(*filter).all() print(comment) return trueReturn(comment, 'success')
class CreateArticle(Resource): ''' 创建文章 ''' @marshal_with(true_serializer(response_article_fields)) def post(self): parser = reqparse.RequestParser() parser.add_argument('title', type=str) parser.add_argument('content', type=str) parser.add_argument('user_id', type=int) parser.add_argument('category_id', type=int) article = parser.parse_args() atc = Articles(**dict(article)) Articles.add(atc) return trueReturn(atc, 'success')
class getGirlsAlbum(Resource): @marshal_with(true_serializer(response_album)) def get(self): ''' 获取相册集合 tagID(可选):int//标签id keywords(可选):str//查询关键词 pageIndex(可选):int//当前页数(默认为1) pageSize(可选):int//每页条数(默认为10) :return: ''' #注册入参 parser = reqparse.RequestParser() parser.add_argument('tagID', type=int, location='headers') parser.add_argument('keywords', type=str, location='headers') parser.add_argument('pageIndex', type=int, location='headers') parser.add_argument('pageSize', type=int, location='headers') #获取入参 args = parser.parse_args() pageIndex = args['pageIndex'] or 1 pageSize = args['pageSize'] or 10 tagID = args['tagID'] keywords = args['keywords'] #查询参数可选 filter = [] if tagID: filter.append(girls_album.girls_tag_id == tagID) if keywords: filter.append(girls_album.title.ilike("%" + keywords + "%")) # album = girls_album.query.filter_by(girls_tag_id=696).first() search_album = girls_album.query.filter(*filter).order_by( girls_album.id.desc()) album = search_album.paginate(pageIndex, per_page=pageSize, error_out=False) count = search_album.count() totalPage = math.floor(count / pageSize) return trueReturn( { 'list': album.items, 'pageInfo': { 'pageIndex': pageIndex, 'pageSize': pageSize, 'count': count, 'totalPage': totalPage, } }, 'success')
class CreateComment(Resource): ''' 编写评论 ''' @marshal_with(true_serializer(response_comment_field)) def post(self): parser = reqparse.RequestParser() parser.add_argument('user_id', type=int) parser.add_argument('content', type=str) parser.add_argument('article_id', type=int) parser.add_argument('parent_id', type=int) parser.add_argument('reply_uid', type=int) comment = parser.parse_args() c = dict(comment) com = Comment(**c) back = Comment.add(com) if back is not None: return falseReturn(back, 'error') return trueReturn(com, 'success')