Package: tcrudge
TCrudge - simple async CRUDL based on Tornado and Peewee ORM (Peewee
Async)Full documentation (http://tcrudge.readthedocs.io/en/latest/)What is it?Simple configurable framework to create CRUDL (Create, Read, Update,
Delete, List) for models. TCrudge is under heavy development - tons of
bugs are expected. You can use it in production, but API can be broken
at any moment.Why?Tornado is fast. Peewee is great. REST is wonderful.DependenciesTornado (https://github.com/tornadoweb/tornado)Peewee (https://github.com/coleifer/peewee)Peewee-async (https://github.com/05bit/peewee-async)Installationtcrudge is distributed via pypi:https://pypi.python.org/pypi/tcrudge/pip install tcrudgeYou can manually install latest version via GitHub:pip install git+https://github.com/CodeTeam/tcrudge.gitHow to?Describe models using Peewee ORM. Subclasstcrudge.ApiListHandlerandtcrudge.ApiItemHandler. Connect handlers with models using
model_cls handler attribute. Add urls to tornado.Application url
dispatcher.For detailed example see tests (also, tests are available in Docker
container with py.test).You can run tests in docker container only. You’ll need docker and
docker-compose.Go to project root directoryRun docker-compose up, it builts and runs containers.Go to tcrudge container bash: docker exec -ti tcrudge_tcrudge_1
bashRun: DATABASE_URL=postgresql://user:dbpass@pg/test pytestFeatures?DELETE request on item is disabled by default. To enable it implement
_delete method in your model.Models are fat. _create, _update, _delete methods are supposed to
provide different logic on CRUD operationsDjango-style filtering in list request:__gt,__gte,__lt,__lte,__in,__isnull,__like,__ilike,__neare supported. Use/?model_field__<filter_type>=<filter_condition>for complex or/?model_field=<filter_condition>for simple filtering.Django-style order by: use/?order_by=<field_1>,<field_2>etcSerialization is provided by Peewee:playhouse.shortcuts.model_to_dict.recurse,excludeandmax_depthparams are implemented in base class for better
experience. If you want to serialize recurse foreign keys, do not
forget to modifyget_querysetmethod (see Peewee docs for
details, use.join()and.select())Validation is provided out-of-the box via jsonschema. Just set input
schemas for base methods (e.g. post_schema_input,
get_schema_input etc). Request query is validated forGETandHEAD. Request body is validated forPOST,PUTandDELETE.Pagination is activated by default for lists. Usedefault_limitandmax_limitfor customization. Pagination params are set
through headers (X-Limit, X-Offset) or query:/?limit=100&offset=5. Total amount of items is not returned by
default. HEAD request should be sent or total param set to 1:/?total=1List handler supports default filtering and ordering. Usedefault_filteranddefault_order_byclass properties.Exampleimportasyncioimportpeeweeimportpeewee_asyncfromplayhouse.db_urlimportparsefromtornadoimportwebfromtornado.ioloopimportIOLoopfromtcrudge.handlersimportApiListHandler,ApiItemHandlerfromtcrudge.modelsimportBaseModel# Configure Tornado to use asyncioIOLoop.configure('tornado.platform.asyncio.AsyncIOMainLoop')# Create databaseDATABASE_URL='postgresql://user:dbpass@pg/test'db_param=parse(DATABASE_URL)db=peewee_async.PooledPostgresqlDatabase(**db_param)# CRUDL ModelclassCompany(BaseModel):name=peewee.TextField()active=peewee.BooleanField()classMeta:database=db# CL HandlerclassCompanyDetailHandler(ApiItemHandler):model_cls=Company# RUD HandlerclassCompanyListHandler(ApiListHandler):model_cls=Companydefault_filter={'active':True}app_handlers=[('^/api/v1/companies/',CompanyListHandler),('^/api/v1/companies/([^/]+)/',CompanyDetailHandler)]application=web.Application(app_handlers)# ORMapplication.objects=peewee_async.Manager(db)withapplication.objects.allow_sync():# Creates table, if not existsCompany.create_table(True)application.listen(8080,'0.0.0.0')loop=asyncio.get_event_loop()# Start applicationloop.run_forever()Сontributors[Borisov Sergey] (https://github.com/juntatalor)[Shalamov Maxim] (https://github.com/mvshalamov)[Nikolaev Alexander] (https://github.com/wokli)[Krasavina Alina] (https://github.com/thaelathy)[Ivanov Denis] (https://github.com/steinerr)