Created
February 16, 2013 12:40
-
-
Save mtiller/4966755 to your computer and use it in GitHub Desktop.
How this could be done...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask | |
from flask.ext.mongoengine import MongoEngine | |
from flask.ext.mongorest import MongoRest | |
from flask.ext.mongorest.views import ResourceView | |
from flask.ext.mongorest.resources import Resource | |
from flask.ext.mongorest import operators as ops | |
from flask.ext.mongorest import methods | |
app = Flask(__name__) | |
// Configure your database here | |
db = MongoEngine(app) | |
api = MongoRest(app) | |
class Author(db.Document): | |
name = db.StringField(required=True) | |
class AuthorResource(Resource): | |
document = Author | |
uri_path = '/authors/' | |
class Book(db.Document): | |
title = db.StringField(required=True) | |
author = db.ReferenceField(Author, dbref=False) | |
class BookResource(Resource): | |
document = Book | |
related_resources = {'author': AuthorResource} | |
uri_path = '/books/' | |
@api.register(name='authors', url=AuthorResource.uri_path) | |
class AuthorView(ResourceView): | |
resource = AuthorResource | |
methods = [methods.Create, methods.Update, methods.Fetch, methods.List] | |
@api.register(name='books', url=BookResource.uri_path) | |
class AuthorView(ResourceView): | |
resource = BookResource | |
methods = [methods.Create, methods.Update, methods.Fetch, methods.List] | |
if __name__=='__main__': | |
app.debug = True | |
app.run(debug=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment