Exemple #1
0
    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')
Exemple #2
0
    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')
Exemple #3
0
    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')
Exemple #4
0
    def get(self,cid):

        entity = Category.query.get(cid)

        abort_if_obj_null(entity)

        return Resp(data=entity.to_json())
Exemple #5
0
    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')
Exemple #6
0
    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())
Exemple #7
0
 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)
Exemple #8
0
    def get(self, news_id):

        news = News.query.get(news_id)
        abort_if_obj_null(news)

        return Resp(data=news.to_json())
Exemple #9
0
    def get(self):

        entices =  Category.query.all()
        data = [e.to_json() for e in entices]
        return Resp(data=data)