Helper utils for mongoDB in flask use flask-rest
Project description
Flask-REST-MongoDB
一个用于快速构建RESTful API的辅助库,该库依赖于:
- Flask
- Falsk-restful
- MongoEngine
1. 核心工具类
实现了基本的GET\POST\PUT\PATCH\DELETE方法
flask_rest_mongo.mix.MixList # 实现get接口,获取批量数据
flask_rest_mongo.mix.MixCreate # 实现post接口,新建document
flask_rest_mongo.mix.MixRetrial # 实现get接口,获取单个document
flask_rest_mongo.mix.MixUpdate # 实现put与patch接口,更新document
flask_rest_mongo.mix.MixDelete # 实现delete接口,删除document
除此之外提供了各种组合
flask_rest_mongo.mix.MixListCreate
flask_rest_mongo.mix.MixRetrialUpdate
flask_rest_mongo.mix.MixRetrialDelete
flask_rest_mongo.mix.MixUpdateDelete
flask_rest_mongo.mix.MixRetrialUpdateDelete
2. 示例
from flask import Flask
from flask_mongoengine import MongoEngine
from mongoengine import fields
from flask_restful import Resource, Api
from flask_rest_mongo.mix import *
import mongoengine
class Config(object):
ENV = 'development'
SERVER_NAME = '127.0.0.1:7788'
DEBUG = True
MONGODB_SETTINGS = {
'db': 'flaskTest',
'host': '127.0.0.1',
'port': 27017
}
if __name__ == "__main__":
app = Flask(__name__)
db = MongoEngine()
api = Api()
app.config.from_object(Config())
class UserTest(db.DynamicDocument):
username = fields.StringField(primary_key=True)
email = fields.EmailField()
password = fields.StringField()
def to_json(self):
return {
'username':self.username,
'email':self.email
}
class DataTest(db.DynamicDocument):
data = fields.DictField()
list_data = fields.ListField()
def to_json(self, *args, **kwargs):
return {
'id':str(self.id),
'data':self.data,
'list':self.list_data
}
class TestDocument(db.DynamicDocument):
user = fields.ReferenceField(UserTest, reverse_delete_rule=mongoengine.CASCADE, required=True)
data_list = fields.ListField(fields.ReferenceField(DataTest))
count = fields.IntField()
factor = fields.FloatField()
class UserListCreate(Resource, MixListCreate):
document = UserTest
exclude = ['password']
page = True
api.add_resource(UserListCreate, '/user/')
class UserDetail(Resource, MixRetrialUpdateDelete):
document = UserTest
exclude = ['password']
api.add_resource(UserDetail, '/user/detail/')
class DataListCreate(Resource, MixListCreate):
document = DataTest
page = True
api.add_resource(DataListCreate, '/data/')
class DataDetail(Resource, MixRetrialUpdateDelete):
document = DataTest
api.add_resource(DataDetail, '/data/detail/')
class TestDocumentListCreate(Resource, MixListCreate):
document = TestDocument
page = True
api.add_resource(TestDocumentListCreate, '/test/')
class TestDocumentDetail(Resource, MixRetrialUpdateDelete):
document = TestDocument
api.add_resource(TestDocumentDetail, '/test/detail/')
db.init_app(app)
api.init_app(app)
app.run()
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
flask-rest-mongo-0.1.2.tar.gz
(6.8 kB
view hashes)
Built Distributions
flask_rest_mongo-0.1.2-py3.8.egg
(12.8 kB
view hashes)
Close
Hashes for flask_rest_mongo-0.1.2-py3.8.egg
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6ba0f6f3e0babfa5b8b019de48612e197ed412ad22e60b457deeb6ad9fbff9e5 |
|
MD5 | 940011012554dbce007f92cb90316d4b |
|
BLAKE2b-256 | 34e234de0692ca363734b19c8ed262cac9f8b4248090ff0f33962e48138d6b3e |
Close
Hashes for flask_rest_mongo-0.1.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f2deb2ae38fe454e8a4711e67a94cc19054acc77315cc249d8017163286611de |
|
MD5 | c2fba375d44507736ed0337569ee106d |
|
BLAKE2b-256 | 005915322517354dd778230487089718c4e80da7be2f6f46cadfe6b4b0502cb4 |