def register_resources(app, resources): for r in resources: add_resource(app, r) root = Resource('root', Root, uri='/', auth_cls=TrivialAuth, kwargs={'resources': resources}) make_root(app, root)
def get_resources(db): resources = [ Resource(name, Collection, serializer=serializer, auth_cls=TrivialAuth, kwargs={ 'db': db, 'table_name': name }) for name in db.collection_names() ] return resources
from pymongo import MongoClient from flask import Flask from rsrc import Resource from rsrc.contrib.root import Root from rsrc.contrib.db.mongo import Collection, serializer from rsrc.contrib.dufilter import DuFilter from rsrc.framework.flask import add_resource, make_root DB = MongoClient().test resources = [ Resource('users', Collection, serializer=serializer, filter_cls=DuFilter, kwargs={ 'db': DB, 'table_name': 'user' }) ] app = Flask(__name__) if __name__ == '__main__': for r in resources: add_resource(app, r) root = Resource('root', Root, uri='/', kwargs={'resources': resources}) make_root(app, root) app.run(debug=True)
if date_joined_lt: conditions.update({'$lt': date_joined_lt}) if conditions: return {'date_joined': conditions} else: return {} resources = [ Resource('users', Table, serializer=serializer, form_cls=UserForm, filter_cls=UserFilter, kwargs={ 'db': DB, 'table_name': 'user' }) ] app = Flask(__name__) if __name__ == '__main__': for r in resources: add_resource(app, r) root = Resource('root', Root, uri='/', kwargs={'resources': resources}) make_root(app, root)
"""Subclass `TokenAuth` to allow POST specially. In general, `TokenAuth` is enough. """ def authenticated(self, method, auth_params): # allow POST in any case # e.g. # 1. everyone can "POST /users/" to register an account # 2. everyone can "POST /tokens/" to login if method == 'POST': return True return super(AllowPOSTAuth, self).authenticated(method, auth_params) resources = [ Resource('tokens', TokenView, auth_cls=AllowPOSTAuth), Resource('users', Collection, serializer=serializer, auth_cls=AllowPOSTAuth, kwargs={ 'db': settings.DB, 'table_name': 'user' }) ] app = Flask(__name__) if __name__ == '__main__': for r in resources: add_resource(app, r)
return Response(status=status.HTTP_204_NO_CONTENT) def patch(self, request, pk): pk = int(pk) try: todos[pk].update(request.data) return Response(status=status.HTTP_204_NO_CONTENT) except KeyError: raise NotFoundError() def delete_list(self, request): todos.clear() return Response(status=status.HTTP_204_NO_CONTENT) def delete_item(self, request, pk): pk = int(pk) try: del todos[pk] return Response(status=status.HTTP_204_NO_CONTENT) except KeyError: raise NotFoundError() resource = Resource('todos', Todo) app = Flask(__name__) if __name__ == '__main__': add_resource(app, resource) app.run(debug=True)
class Card(View): def get_list(self, request, list_id): list_id = int(list_id) query_func = lambda c: c['list_id'] == list_id result_cards = filter(query_func, cards.values()) return Response(result_cards) def get_item(self, request, list_id, pk): list_id = int(list_id) pk = int(pk) query_func = lambda c: c['list_id'] == list_id and c['id'] == pk result_cards = filter(query_func, cards.values()) if result_cards: return Response(result_cards[0]) else: raise NotFoundError() resources = [ Resource('lists', List), Resource('cards', Card, uri='/lists/<list_id>/cards') ] app = Flask(__name__) if __name__ == '__main__': for r in resources: add_resource(app, r) app.run(debug=True)