{"id":13421541,"url":"https://github.com/closeio/flask-mongorest","last_synced_at":"2025-03-15T10:31:13.676Z","repository":{"id":3578564,"uuid":"4641343","full_name":"closeio/flask-mongorest","owner":"closeio","description":"Restful API framework wrapped around MongoEngine","archived":true,"fork":false,"pushed_at":"2024-01-23T12:59:32.000Z","size":375,"stargazers_count":521,"open_issues_count":24,"forks_count":88,"subscribers_count":48,"default_branch":"master","last_synced_at":"2025-03-02T13:38:09.360Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/closeio.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":"AUTHORS","dei":null,"publiccode":null,"codemeta":null}},"created_at":"2012-06-12T18:40:14.000Z","updated_at":"2024-11-22T20:08:52.000Z","dependencies_parsed_at":"2024-06-20T15:44:00.250Z","dependency_job_id":"26810a48-85fd-4ca8-98ad-d75bb2ed2ea9","html_url":"https://github.com/closeio/flask-mongorest","commit_stats":{"total_commits":264,"total_committers":18,"mean_commits":"14.666666666666666","dds":0.6553030303030303,"last_synced_commit":"6a95fa2d342eaa8b068950acd4ea909c82814dba"},"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/closeio%2Fflask-mongorest","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/closeio%2Fflask-mongorest/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/closeio%2Fflask-mongorest/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/closeio%2Fflask-mongorest/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/closeio","download_url":"https://codeload.github.com/closeio/flask-mongorest/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243718885,"owners_count":20336590,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-07-30T23:00:24.566Z","updated_at":"2025-03-15T10:31:13.161Z","avatar_url":"https://github.com/closeio.png","language":"Python","readme":"*This project isn't actively maintained. We do not recommend it for production use.*\n\nFlask-MongoRest [![Build Status](https://circleci.com/gh/closeio/flask-mongorest.png?branch=master\u0026style=shield)](https://circleci.com/gh/closeio/flask-mongorest)\n===============\nA Restful API framework wrapped around MongoEngine.\n\nSetup\n=====\n\n``` python\nfrom flask import Flask\nfrom flask_mongoengine import MongoEngine\nfrom flask_mongorest import MongoRest\nfrom flask_mongorest.views import ResourceView\nfrom flask_mongorest.resources import Resource\nfrom flask_mongorest import operators as ops\nfrom flask_mongorest import methods\n\n\napp = Flask(__name__)\n\napp.config.update(\n    MONGODB_HOST = 'localhost',\n    MONGODB_PORT = '27017',\n    MONGODB_DB = 'mongorest_example_app',\n)\n\ndb = MongoEngine(app)\napi = MongoRest(app)\n\nclass User(db.Document):\n    email = db.EmailField(unique=True, required=True)\n\nclass Content(db.EmbeddedDocument):\n    text = db.StringField()\n\nclass ContentResource(Resource):\n    document = Content\n\nclass Post(db.Document):\n    title = db.StringField(max_length=120, required=True)\n    author = db.ReferenceField(User)\n    content = db.EmbeddedDocumentField(Content)\n\nclass PostResource(Resource):\n    document = Post\n    related_resources = {\n        'content': ContentResource,\n    }\n    filters = {\n        'title': [ops.Exact, ops.Startswith],\n        'author_id': [ops.Exact],\n    }\n    rename_fields = {\n        'author': 'author_id',\n    }\n\n@api.register(name='posts', url='/posts/')\nclass PostView(ResourceView):\n    resource = PostResource\n    methods = [methods.Create, methods.Update, methods.Fetch, methods.List]\n```\n\nWith this app, following cURL commands could be used:\n```\nCreate a Post:\ncurl -H \"Content-Type: application/json\" -X POST -d \\\n'{\"title\": \"First post!\", \"author_id\": \"author_id_from_a_previous_api_call\", \"content\": {\"text\": \"this is our test post content\"}}' http://0.0.0.0:5000/posts/\n{\n  \"id\": \"1\",\n  \"title\": \"First post!\",\n  \"author_id\": \"author_id_from_a_previous_api_call\",\n  \"content\": {\n    \"text\": \"this is our test post content\"\n  }\n} \n```\nGet a Post:\n```\ncurl http://0.0.0.0:5000/posts/1/\n{\n  \"id\": \"1\",\n  \"title\": \"First post!\",\n  \"author_id\": \"author_id_from_a_previous_api_call\",\n  \"content\": {\n    \"text\": \"this is our test post content\"\n  }\n} \n```\nList all Posts or filter by the title:\n```\ncurl http://0.0.0.0:5000/posts/ or curl http://0.0.0.0:5000/posts/?title__startswith=First%20post\n{\n  \"data\": [\n    {\n      \"id\": \"1\",\n      \"title\": \"First post!\",\n      \"author_id\": \"author_id_from_a_previous_api_call\",\n      \"content\": {\n        \"text\": \"this is our test post content\"\n      }\n    },\n    ... other posts\n  ]\n}\n```\nDelete a Post:\n```\ncurl -X DELETE http://0.0.0.0:5000/posts/1/\n# Fails since PostView.methods does not allow Delete\n```\n\nRequest Params\n==============\n\n**_skip** and **_limit** =\u003e utilize the built-in functions of mongodb.\n\n**_fields** =\u003e limit the response's fields to those named here (comma separated).\n\n**_order_by** =\u003e order results if this string is present in the Resource.allowed_ordering list.  \n\n\nResource Configuration\n======================\n\n**rename_fields** =\u003e dict of renaming rules.  Useful for mapping _id fields such as \"organization\": \"organization_id\"\n\n**filters** =\u003e filter results of a List request using the allowed filters which are used like `/user/?id__gt=2` or `/user/?email__exact=a@b.com`\n\n**related_resources** =\u003e nested resource serialization for reference/embedded fields of a document\n\n**child_document_resources** =\u003e Suppose you have a Person base class which has Male and Female subclasses.  These subclasses and their respective resources share the same MongoDB collection, but have different fields and serialization characteristics.  This dictionary allows you to map class instances to their respective resources to be used during serialization.\n\nAuthentication\n==============\nThe AuthenticationBase class provides the ability for application's to implement their own API auth.  Two common patterns are shown below along with a BaseResourceView which can be used as the parent View of all of your app's resources.\n``` python\nclass SessionAuthentication(AuthenticationBase):\n    def authorized(self):\n        return current_user.is_authenticated()\n\nclass ApiKeyAuthentication(AuthenticationBase):\n    \"\"\"\n    @TODO ApiKey document and key generation left to the specific implementation\n    \"\"\"\n    def authorized(self):\n        if 'AUTHORIZATION' in request.headers:\n            authorization = request.headers['AUTHORIZATION'].split()\n            if len(authorization) == 2 and authorization[0].lower() == 'basic':\n                try:\n                    authorization_parts = base64.b64decode(authorization[1]).partition(':')\n                    key = smart_unicode(authorization_parts[0])\n                    api_key = ApiKey.objects.get(key__exact=key)\n                    if api_key.user:\n                        login_user(api_key.user)\n                        setattr(current_user, 'api_key', api_key)\n                    return True\n                except (TypeError, UnicodeDecodeError, ApiKey.DoesNotExist):\n                    pass\n        return False\n\nclass BaseResourceView(ResourceView):\n    authentication_methods = [SessionAuthentication, ApiKeyAuthentication]\n```\n\nRunning the test suite\n======================\nThis package uses nosetests for automated testing. Just run `python setup.py nosetests` to run the tests. No setup or any other prep needed.\n\nContributing\n============\nPull requests are greatly appreciated!\n","funding_links":[],"categories":["Python","Web Frameworks","Third-Party Extensions","Framework","介绍"],"sub_categories":["APIs"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcloseio%2Fflask-mongorest","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcloseio%2Fflask-mongorest","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcloseio%2Fflask-mongorest/lists"}