Skip to main content

Automatically generated a RESTful API services for CRUD operation and queries on database

Project description

Inspired by sandman2, based on sqlalchemy-filters and Flask-ResponseBuilder

Automatically generate a RESTful APIs for CRUD operation and advanced search on a database. If a list of Model is not provided, all tables are affected, instead you can customize:

  • resource name

  • fields name

  • resource url

  • allowed methods

  • hidden fields

Features

  • HATEOAS support

  • conditional requests via ETag header

  • full range of CRUD operations

  • filtering, sorting and pagination

  • customizable responses via query string

  • custom FETCH method for advanced search

  • JSON and XML response based on Accept header

  • export to csv available

  • meta resource description

Quickstart

Install flask_autocrud using pip:

$ pip install Flask-AutoCRUD

Example usage

from flask import Flask

from flask_autocrud import AutoCrud
from flask_sqlalchemy import SQLAlchemy


app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite+pysqlite:///db.sqlite3'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['AUTOCRUD_METADATA_ENABLED'] = True

db = SQLAlchemy(app)
AutoCrud(app, db)

app.run(debug=True)

Go to http://127.0.0.1:5000/resources and see all available resources with its endpoint. NOTE: you must set SQLALCHEMY_DATABASE_URI with your database.

If you want to see an example use with Flask-Admin see in example folder.

Filtering and Sorting

Add filters as query string parameters, they are used in AND. NOTE: At this time OR operator are not implemented.

You can use entity fields as parameter with the following placeholders:

  • null value: null

  • in operator: list separated by ;

  • not operator: ! means: not equal, not null, not in

  • comparators: __gt__ (grater), __lt__ (lesser), __gte__ (grater-equal), __lte__ (lesser-equal)

  • like operator: % for example: %%test%, %test% or %%test NOTE first % are not used in expression, it only indicated that value must be used with like operator.

Other parameters:

  • Use _fields parameter to get only the fields listed as value, separated by ;.

  • Use _limit and _page parameters for pagination.

  • Sorting is implemented with _sort parameter. The value is a list of field separated by ; You can prepend - to reverse order.

  • Use _export parameter to export data into csv format.

  • Use _extended in order to fetch data of related resources.

  • Use _as_table in order to flatten nested dict useful if you want render response as table

Example requests:

  • /invoice?InvoiceId=(35;344)

  • /invoice?Total=__lte__10&sort=Total

  • /invoice?fields=BillingCountry;Total;InvoiceId&InvoiceId=!355;344&sort=-InvoiceId

  • /invoice?fields=Total;InvoiceId&BillingPostalCode=!null&BillingCountry=%%ermany

  • /invoice?fields=Total;InvoiceDate;InvoiceId;CustomerId&page=2&limit=10

  • /invoice?InvoiceDate=(2009-01-01;2009-02-01 00:00:00)

Example FETCH:

curl --request FETCH \
    --url http://127.0.0.1:5000/customer \
    --header 'content-type: application/json' \
    --data '{
        "fields": [
            "Address",
            "City"
        ],
        "related": {
            "Employee": [
                "FirstName",
                "LastName"
            ],
            "Invoice": [
                "*"
            ]
        },
        "filters": [
            {
                "model": "Customer",
                "field": "SupportRepId",
                "op": "==",
                "value": 5
            },
            {
                "model": "Invoice",
                "field": "Total",
                "op": ">",
                "value": 6
            }
        ],
        "sorting": [
            {
                "model": "Invoice",
                "field": "Total",
                "direction": "asc"
            },
            {
                "model": "Customer",
                "field": "Address",
                "direction": "desc"
            }
        ]
    }'

Configuration

  1. AUTOCRUD_METADATA_ENABLED: (default: True) enable metadata endpoint for a resource

  2. AUTOCRUD_METADATA_URL: (default: ‘/meta) added at the end of url resource

  3. AUTOCRUD_READ_ONLY: (default: False) enable only http GET method

  4. AUTOCRUD_BASE_URL: (default: ‘’) prefix url for resources

  5. AUTOCRUD_RESOURCES_URL: (default: ‘/resources’) url for all available resources

  6. AUTOCRUD_RESOURCES_URL_ENABLED: (default: True) enable route for resources list

  7. AUTOCRUD_SUBDOMAIN: (default: None) bind autocrud endpoints to a subdomain

  8. AUTOCRUD_MAX_QUERY_LIMIT: (default 1000) max query limit, 0 means no limit

  9. AUTOCRUD_FETCH_ENABLED: (default True) enable or disable FETCH method

  10. AUTOCRUD_QUERY_STRING_FILTERS_ENABLED: (default True) enable or disable filters in querystring

  11. AUTOCRUD_EXPORT_ENABLED: (default True) enable or disable export to csv

  12. AUTOCRUD_DATABASE_SCHEMA: (default None) database schema to consider

  13. AUTOCRUD_CONDITIONAL_REQUEST_ENABLED: (default True) allow conditional request

License MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

Flask-AutoCRUD-2.0.1.tar.gz (17.5 kB view details)

Uploaded Source

Built Distribution

Flask_AutoCRUD-2.0.1-py3-none-any.whl (25.8 kB view details)

Uploaded Python 3

File details

Details for the file Flask-AutoCRUD-2.0.1.tar.gz.

File metadata

  • Download URL: Flask-AutoCRUD-2.0.1.tar.gz
  • Upload date:
  • Size: 17.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/2.7.15

File hashes

Hashes for Flask-AutoCRUD-2.0.1.tar.gz
Algorithm Hash digest
SHA256 149216f2d8e2e5c51647550dc4c30b3ec781a5497ca786f42fa0c483ab0f92a8
MD5 0c441d99f4a5b13f49b44cb9a1aa0baa
BLAKE2b-256 320c7b2ac5d69cee782402d9c713d525c8ab048c7482cde405f36f0d1629e4af

See more details on using hashes here.

File details

Details for the file Flask_AutoCRUD-2.0.1-py3-none-any.whl.

File metadata

  • Download URL: Flask_AutoCRUD-2.0.1-py3-none-any.whl
  • Upload date:
  • Size: 25.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/2.7.15

File hashes

Hashes for Flask_AutoCRUD-2.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e481b613dd13f5e604b0077772bb93401256afb039df843379ae2021a137e5ea
MD5 f0563dd5d5b55302dc67cd9bf884dea1
BLAKE2b-256 cd4dfcd5debe6b40de5b4b70269755a922071e486ac8ca33db70fd4ab07e7861

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page