Esempio n. 1
0
    'password': fields.String,
    'udatetime': fields.DateTime
}


# 定义类视图
class UserResource(Resource):
    # get 请求的处理
    @marshal_with(user_fields)
    def get(self):
        users = User.query.all()
        # userList = []
        # for user in users:
        #     userList.append(user.__dict__)
        return users

    # post
    def post(self):
        return {'msg': '------>post'}

    # put
    def put(self):
        return {'msg': '------>put'}

    # delete
    def delete(self):
        return {'msg': '------>delete'}


api.add_resource(UserResource, '/user')
Esempio n. 2
0
            icon.save(upload_path)
            user.icon = config.UPLOAD_ICON_DIR + '\\' + icon.filename
        user.username = username
        user.password = password
        user.phone = phone
        db.session.add(user)
        db.session.commit()
        return user

    def put(self):
        pass


class UserResourceSimple(Resource):
    @marshal_with(user_fields)
    def get(self, uid):
        user = User.query.get(uid)
        return user

    def post(self, uid):

        pass

    def put(self, uid):
        print('endpoint使用:', url_for('user_all'))
        return {'code': 200, 'msg': "ok"}


api.add_resource(UserResource, '/user', endpoint="user_all")
api.add_resource(UserResourceSimple, '/user/<string:uid>')
from exts import api
from flask_restful import Resource, fields, marshal_with
from flask import Blueprint
from apps.models.news_models import NewsType


news_bp = Blueprint('news_api', __name__, url_prefix='api')

news_type_fields = {
    'id': fields.Integer,
    'name': fields.String(attribute='type_name')
}

class NewsTypeResource(Resource):

    @marshal_with(news_type_fields)
    def get(self):
        types = NewsType.query.all()
        return types


api.add_resource(NewsTypeResource, '/newstype')
Esempio n. 4
0
    @marshal_with(user_friend_fields)
    def get(self, id):
        friends = Friend.query.filter(Friend.uid == id).all()
        user = User.query.get(id)

        friend_list = []
        for friend in friends:
            u = User.query.get(friend.fid)
            friend_list.append(u)

        data = {
            'username': user.username,
            'nums': len(friends),
            'friends':
            friend_list  # 套叠结构 [user,user,user] not JSON serializable 需要结合marshal_with(user_friend_fields)使用
            # 'friends': marshal(friend_list,user_fields) # 用user_fields格式化friend_list后返回
        }
        return data


# 将类加入到api中 会出现路由[<Rule '/user' (POST, HEAD, DELETE, OPTIONS, GET, PUT) -> userresource>,...]
# 访问 http://127.0.0.1:5000/user
api.add_resource(UserResource, '/user', endpoint='all_user')
# 添加路径传参类
# 访问 http://127.0.0.1:5000/user/1
api.add_resource(UserSimpleResource, '/user/<int:id>', endpoint='single_user')

api.add_resource(UserFriendResource,
                 '/friend/<int:id>',
                 endpoint='user_friend')
# python app.py runserver启动
Esempio n. 5
0

class UserSingleResource(Resource):
    @marshal_with(user_fields)
    def get(self, id):
        user = User.query.get(id)
        return user


class UserFriendResource(Resource):
    def get(self, id):
        friends = Friend.query.filter(Friend.uid == id).all()
        user = User.query.get(id)

        friendList = [User.query.get(friend.fid) for friend in friends]

        data = {
            'username': user.username,
            'number': len(friendList),
            'friends': marshal(friendList, user_fields)
        }

        return data

    def delete(self, id, fid):
        pass


api.add_resource(UserResource, '/user', endpoint='users')
api.add_resource(UserSingleResource, '/user/<int:id>', endpoint='userDetail')
api.add_resource(UserFriendResource, '/friend/<int:id>', endpoint='freind')
Esempio n. 6
0
    def delete(self, uid):
        return {'msg': '-----delete'}

    def put(self, uid):
        return {'msg': '-----put'}


class UserFriendSResource(Resource):
    @marshal_with(user_friends_fields)
    def get(self, uid):
        user = User.query.get(uid)
        friends = User.query.all()

        friend_list = []
        for friend in friends:
            friend_list.append(friend)

        data = {
            'username': user.username,
            'nums': len(friend_list),
            # 'friends': marshal(friend_list, user_fields)
            'friends': friend_list
        }

        return data


api.add_resource(UserResource, '/user', endpoint='all_user')
api.add_resource(UserSimpleResource, '/users/<int:uid>')
api.add_resource(UserFriendSResource, '/friends/<int:uid>')
Esempio n. 7
0
    @marshal_with(user_list)
    def post(self, id):
        '''返回全部用户'''
        user = User.query.get(id)

        users = User.query.all()
        user_list = [i for i in users]

        # 使用marshal返回套叠数据
        # data = {
        #     'name': user.username,
        #     'len': len(users),
        #     'user_list': marshal(data=user_list, fields=user_fields),  # 使用marshal序列化全部用户
        # }

        # 使用@marshal_with装饰器返回套叠数据
        data = {
            'name': user.username,
            'len': len(users),
            'user_list': user_list,
        }
        return data


# (类视图,路径),必须添加,不然访问不到
api.add_resource(UsersResource, '/user',
                 endpoint='user')  # endpoint:别名,反向解析url_for()
# 路径传参,用户id,因为上面使用了fields.Url,所以要跟数据库字段一致
api.add_resource(UserResource, '/user/<int:id>', endpoint='single_user')