<s> import os <EOL> from flask import Flask , request <EOL> from flask . ext . mongoengine import MongoEngine <EOL> from flask . ext . mongorest import MongoRest <EOL> from flask . ext . mongorest . views import ResourceView <EOL> from flask . ext . mongorest . resources import Resource <EOL> from flask . ext . mongorest import operators as ops <EOL> from flask . ext . mongorest . methods import * <EOL> from flask . ext . mongorest . authentication import AuthenticationBase <EOL> from example import schemas , documents <EOL> app = Flask ( __name__ ) <EOL> app . url_map . strict_slashes = False <EOL> app . config . update ( <EOL> DEBUG = True , <EOL> TESTING = True , <EOL> MONGODB_SETTINGS = { <EOL> '<STR_LIT>' : '<STR_LIT:localhost>' , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : True , <EOL> } , <EOL> ) <EOL> db = MongoEngine ( app ) <EOL> api = MongoRest ( app ) <EOL> class UserResource ( Resource ) : <EOL> document = documents . User <EOL> schema = schemas . User <EOL> filters = { <EOL> '<STR_LIT>' : [ ops . Exact ] <EOL> } <EOL> @ api . register ( ) <EOL> class UserView ( ResourceView ) : <EOL> resource = UserResource <EOL> methods = [ Create , Update , Fetch , List , Delete ] <EOL> class ContentResource ( Resource ) : <EOL> document = documents . Content <EOL> class PostResource ( Resource ) : <EOL> document = documents . Post <EOL> schema = schemas . Post <EOL> related_resources = { <EOL> '<STR_LIT:content>' : ContentResource , <EOL> '<STR_LIT>' : ContentResource , <EOL> '<STR_LIT>' : UserResource , <EOL> } <EOL> filters = { <EOL> '<STR_LIT:title>' : [ ops . Exact , ops . Startswith , ops . In ( allow_negation = True ) ] , <EOL> '<STR_LIT>' : [ ops . Exact ] , <EOL> '<STR_LIT>' : [ ops . Boolean ] , <EOL> } <EOL> rename_fields = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> def get_objects ( self , ** kwargs ) : <EOL> qs , has_more = super ( PostResource , self ) . get_objects ( ** kwargs ) <EOL> return qs , has_more , { '<STR_LIT>' : '<STR_LIT>' } <EOL> def get_fields ( self ) : <EOL> fields = super ( PostResource , self ) . get_fields ( ) <EOL> if '<STR_LIT>' in request . args : <EOL> fields = set ( fields ) | set ( [ '<STR_LIT>' ] ) <EOL> return fields <EOL> def update_object ( self , obj , data = None , save = True , parent_resources = None ) : <EOL> data = data or self . data <EOL> if data . get ( '<STR_LIT>' ) : <EOL> author = data [ '<STR_LIT>' ] <EOL> if author . email == '<STR_LIT>' : <EOL> obj . tags . append ( '<STR_LIT>' ) <EOL> return super ( PostResource , self ) . update_object ( obj , data , save , parent_resources ) <EOL> @ api . register ( name = '<STR_LIT>' , url = '<STR_LIT>' ) <EOL> class PostView ( ResourceView ) : <EOL> resource = PostResource <EOL> methods = [ Create , Update , BulkUpdate , Fetch , List , Delete ] <EOL> class LimitedPostResource ( Resource ) : <EOL> document = documents . Post <EOL> related_resources = { <EOL> '<STR_LIT:content>' : ContentResource , <EOL> } <EOL> @ api . register ( name = '<STR_LIT>' , url = '<STR_LIT>' ) <EOL> class LimitedPostView ( ResourceView ) : <EOL> resource = LimitedPostResource <EOL> methods = [ Create , Update , Fetch , List ] <EOL> class DummyAuthenication ( AuthenticationBase ) : <EOL> def authorized ( self ) : <EOL> return False <EOL> @ api . register ( name = '<STR_LIT>' , url = '<STR_LIT>' ) <EOL> class DummyAuthView ( ResourceView ) : <EOL> resource = PostResource <EOL> methods = [ Create , Update , Fetch , List , Delete ] <EOL> authentication_methods = [ DummyAuthenication ] <EOL> @ api . register ( name = '<STR_LIT>' , url = '<STR_LIT>' ) <EOL> class RestrictedPostView ( ResourceView ) : <EOL> """<STR_LIT>""" <EOL> resource = PostResource <EOL> methods = [ Create , Update , Fetch , List , Delete ] <EOL> def has_read_permission ( self , request , qs ) : <EOL> return qs . filter ( is_published = True ) <EOL> def has_add_permission ( self , request , obj ) : <EOL> return not obj . is_published <EOL> def has_change_permission ( self , request , obj ) : <EOL> return not obj . is_published <EOL> def has_delete_permission ( self , request , obj ) : <EOL> return not obj . is_published <EOL> class TestDocument ( db . Document ) : <EOL> name = db . StringField ( ) <EOL> other = db . StringField ( ) <EOL> dictfield = db . DictField ( ) <EOL> is_new = db . BooleanField ( ) <EOL> class TestResource ( Resource ) : <EOL> document = TestDocument <EOL> class TestFieldsResource ( Resource ) : <EOL> document = TestDocument <EOL> fields = [ '<STR_LIT:id>' , '<STR_LIT:name>' , '<STR_LIT>' ] <EOL> def upper_name ( self , obj ) : <EOL> return obj . name . upper ( ) <EOL> @ api . register ( name = '<STR_LIT:test>' , url = '<STR_LIT>' ) <EOL> class TestView ( ResourceView ) : <EOL> resource = TestResource <EOL> methods = [ Create , Update , Fetch , List ] <EOL> @ api . register ( name = '<STR_LIT>' , url = '<STR_LIT>' ) <EOL> class TestFieldsResource ( ResourceView ) : <EOL> resource = TestFieldsResource <EOL> methods = [ Create , Update , Fetch , List ] <EOL> class LanguageResource ( Resource ) : <EOL> document = documents . Language <EOL> class PersonResource ( Resource ) : <EOL> document = documents . Person <EOL> schema = schemas . Person <EOL> related_resources = { <EOL> '<STR_LIT>' : LanguageResource , <EOL> } <EOL> save_related_fields = [ '<STR_LIT>' ] <EOL> @ api . register ( name = '<STR_LIT>' , url = '<STR_LIT>' ) <EOL> class PersonView ( ResourceView ) : <EOL> resource = PersonResource <EOL> methods = [ Create , Update , Fetch , List ] <EOL> class Post10Resource ( PostResource ) : <EOL> max_limit = <NUM_LIT:10> <EOL> class Post250Resource ( PostResource ) : <EOL> max_limit = <NUM_LIT> <EOL> @ api . register ( name = '<STR_LIT>' , url = '<STR_LIT>' ) <EOL> class Post10View ( ResourceView ) : <EOL> resource = Post10Resource <EOL> methods = [ Create , Update , BulkUpdate , Fetch , List , Delete ] <EOL> @ api . register ( name = '<STR_LIT>' , url = '<STR_LIT>' ) <EOL> class Post250View ( ResourceView ) : <EOL> resource = Post250Resource <EOL> methods = [ Create , Update , BulkUpdate , Fetch , List , Delete ] <EOL> class A ( db . Document ) : <EOL> txt = db . StringField ( ) <EOL> class B ( db . Document ) : <EOL> ref = db . ReferenceField ( A , dbref = True ) <EOL> txt = db . StringField ( ) <EOL> class C ( db . Document ) : <EOL> ref = db . ReferenceField ( A ) <EOL> txt = db . StringField ( ) <EOL> class AResource ( Resource ) : <EOL> document = A <EOL> class BResource ( Resource ) : <EOL> document = B <EOL> class CResource ( Resource ) : <EOL> document = C <EOL> @ api . register ( url = '<STR_LIT>' ) <EOL> class AView ( ResourceView ) : <EOL> resource = AResource <EOL> methods = [ Create , Update , BulkUpdate , Fetch , List , Delete ] <EOL> @ api . register ( url = '<STR_LIT>' ) <EOL> class AView ( ResourceView ) : <EOL> resource = BResource <EOL> methods = [ Create , Update , BulkUpdate , Fetch , List , Delete ] <EOL> @ api . register ( url = '<STR_LIT>' ) <EOL> class AView ( ResourceView ) : <EOL> resource = CResource <EOL> methods = [ Create , Update , BulkUpdate , Fetch , List , Delete ] <EOL> class MethodTestDoc ( db . Document ) : <EOL> txt = db . StringField ( ) <EOL> class MethodTestResource ( Resource ) : <EOL> document = MethodTestDoc <EOL> @ api . register ( url = '<STR_LIT>' ) <EOL> class CreateOnlyView ( ResourceView ) : <EOL> resource = MethodTestResource <EOL> methods = [ Create ] <EOL> @ api . register ( url = '<STR_LIT>' ) <EOL> class UpdateOnlyView ( ResourceView ) : <EOL> resource = MethodTestResource <EOL> methods = [ Update ] <EOL> @ api . register ( url = '<STR_LIT>' ) <EOL> class BulkUpdateOnlyView ( ResourceView ) : <EOL> resource = MethodTestResource <EOL> methods = [ BulkUpdate ] <EOL> @ api . register ( url = '<STR_LIT>' ) <EOL> class FetchOnlyView ( ResourceView ) : <EOL> resource = MethodTestResource <EOL> methods = [ Fetch ] <EOL> @ api . register ( url = '<STR_LIT>' ) <EOL> class ListOnlyView ( ResourceView ) : <EOL> resource = MethodTestResource <EOL> methods = [ List ] <EOL> @ api . register ( url = '<STR_LIT>' ) <EOL> class DeleteOnlyView ( ResourceView ) : <EOL> resource = MethodTestResource <EOL> methods = [ Delete ] <EOL> class ViewMethodTestDoc ( db . Document ) : <EOL> txt = db . StringField ( ) <EOL> class ViewMethodTestResource ( Resource ) : <EOL> document = ViewMethodTestDoc <EOL> @ api . register ( url = '<STR_LIT>' ) <EOL> class TestViewMethodView ( ResourceView ) : <EOL> resource = ViewMethodTestResource <EOL> methods = [ Create , Update , BulkUpdate , Fetch , List , Delete ] <EOL> def _dispatch_request ( self , * args , ** kwargs ) : <EOL> super ( TestViewMethodView , self ) . _dispatch_request ( * args , ** kwargs ) <EOL> return { '<STR_LIT>' : self . _resource . view_method . __name__ } <EOL> class DateTimeResource ( Resource ) : <EOL> document = documents . DateTime <EOL> schema = schemas . DateTime <EOL> @ api . register ( name = '<STR_LIT>' , url = '<STR_LIT>' ) <EOL> class DateTimeView ( ResourceView ) : <EOL> resource = DateTimeResource <EOL> methods = [ Create , Update , Fetch , List ] <EOL> class DictDoc ( db . Document ) : <EOL> dict = db . DictField ( ) <EOL> class DictDocResource ( Resource ) : <EOL> document = DictDoc <EOL> @ api . register ( url = '<STR_LIT>' ) <EOL> class DictDocView ( ResourceView ) : <EOL> resource = DictDocResource <EOL> methods = [ Fetch , List , Create , Update ] <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> port = int ( os . environ . get ( '<STR_LIT>' , <NUM_LIT> ) ) <EOL> app . run ( host = '<STR_LIT>' , port = port ) </s>
