def post(self): args = create_parse.parse_args() entity = Category(name=args.get('name'),description=args.get('description')) try: db.session.add(entity) db.session.commit() return Resp(data=entity.to_json()) except Exception as ex: db.session.rollback() return Resp(code=400,msg='insert failure')
def delete(self, news_id): news = News.query.get(news_id) abort_if_obj_null(news) try: db.session.delete(news) db.session.commit() return Resp() except Exception as ex: db.session.rollback() print(ex.args.__str__()) return Resp(code=400, msg='delete error')
def delete(self,cid): entity = Category.query.get(cid) abort_if_obj_null(entity) try: db.session.delete(entity) db.session.commit() return Resp() except Exception as ex: db.session.rollback() return Resp(code=400,msg='delete error')
def get(self,cid): entity = Category.query.get(cid) abort_if_obj_null(entity) return Resp(data=entity.to_json())
def post(self): args = create_parse.parse_args() news = News( category_id=args.get('cid'), title=args.get('title'), body=args.get('body'), front_image=args.get('front_image'), ) try: db.session.add(news) db.session.commit() return Resp(data=news.to_json()) except Exception as ex: db.session.rollback() return Resp(code=400, msg='create news error')
def put(self, news_id): args = create_parse.parse_args() news = News.query.get(news_id) abort_if_obj_null(news) args['body'] = args.pop("content") args['category_id'] = args.pop("cid") for prop in args: if args.get(prop): setattr(news, prop, args.get(prop)) db.session.commit() return Resp(data=news.to_json())
def get(self): args = news_parse.parse_args() temp_query = News.query.filter( News.title.like("%" + args.get('keyword') + "%")) if args.get('cid'): temp_query = temp_query.filter(News.category_id == args.get('cid')) pagination = temp_query.paginate(args.get('page'), args.get('pagesize'), error_out=False) data = { 'totalpage': pagination.pages, 'items': [item.to_json() for item in pagination.items], 'page': pagination.page, 'pagesize': pagination.per_page } return Resp(data=data)
def get(self, news_id): news = News.query.get(news_id) abort_if_obj_null(news) return Resp(data=news.to_json())
def get(self): entices = Category.query.all() data = [e.to_json() for e in entices] return Resp(data=data)