1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
|
from flask_peewee.rest import RestAPI, RestResource, UserAuthentication, AdminAuthentication, RestrictOwnerResource
from app import app
from auth import auth
from models import User, Message, Note, Relationship
user_auth = UserAuthentication(auth)
admin_auth = AdminAuthentication(auth)
# instantiate our api wrapper
api = RestAPI(app, default_auth=user_auth)
class UserResource(RestResource):
exclude = ('password', 'email',)
class MessageResource(RestrictOwnerResource):
owner_field = 'user'
include_resources = {'user': UserResource}
class RelationshipResource(RestrictOwnerResource):
owner_field = 'from_user'
include_resources = {
'from_user': UserResource,
'to_user': UserResource,
}
paginate_by = None
class NoteResource(RestrictOwnerResource):
owner_field = 'user'
include_resources = {
'user': UserResource,
}
def get_query(self):
query = super(NoteResource, self).get_query()
return query.where(Note.status == 1)
# register our models so they are exposed via /api/<model>/
api.register(User, UserResource, auth=admin_auth)
api.register(Relationship, RelationshipResource)
api.register(Message, MessageResource)
api.register(Note, NoteResource)
|