Skip to main content

Automagic REST: Django REST Framework PostgreSQL Builder

Project description

Automagic REST

pypi

Automagic REST automatically builds a full Django app as a Django REST Framework read-only environment for a set of tables in a PostgreSQL database.

This is very much in heavy development, being extracted from a production system and genericized for open source release.

Installation

To get started, pip install automagic-rest and add automagic_rest to your INSTALLED_APPS setting in Django.

Configuration and Customization

Setting up a secondary database in Django is recommended. For the following examples, we'll set up one called my_pg_data with the user my_pg_user:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'pg_web_db',
        'USER': 'web_user',
        'PASSWORD': '',
        'HOST': 'pg-web.domain.com',
    },
    'my_pg_data': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'pg_data_db',
        'USER': 'my_pg_user',
        'PASSWORD': '',
        'HOST': 'pg-data.domain.com',
    },
}

By default, Automagic REST will create a directory called data_path at the root of your Django project, where manage.py lives. The follow options can be passed to the command:

  • --database (default: my_pg_data): the name of the Django database as defined in the DATABASES setting.
  • --owner (default: my_pg_user): the name of the PostgreSQL user which owns the schemata to be processed. This will normally be the same as the USER in the DATABASES setting for the database above.
  • --path (default: data_path): the path to write the models and serializers to. This path will be completely deleted and rewritten whenever the command is run, so be careful!

Example: python manage.py build_data_models --database=my_data --owner=my_user --path=my_data_path

Methods are provided which can be overridden to customize the endpoint with your own Django management command.

class automagic_rest.management.commands.build_data_models.Command

get_db (default: my_pg_data): the name of the PostgreSQL database in Django's settings that we will introspect to build the API.

get_owner (default: my_pg_user): the name of the PostgreSQL user that owns the schemata we will introspect.

get_allowed_schemata (default: None): if set, returns a list of schemata in the database to be built. If None, selects all schemata for the specific user.

get_root_python_path (default: data_path): a Python path where you would like to write the models, serializers, and routes. IMPORTANT: this path will be wiped and re-written every time the build command is run. It should be a dedicated directory with nothing else in it.

get_view (default: automagic_rest.views.GenericViewSet): the view to use.

get_router (default: rest_framework.routers.DefaultRouter): the router to use.

get_max_digits_default (default: 100): the number of max_digits to provide for NUMERIC field types that are not explicitly set in PostgreSQL.

get_decimal_places_default (default: 25): the number of decimal_places to provide for NUMERIC field types that are not explicitly set in PostgreSQL.

sanitize_sql_identifier: this method takes a string, and sanitizes it for injections into SQL, allowing only alphanumerics and underscores.

metadata_sql: this method returns the SQL used to pull the metadata from PostgreSQL to build the endpoints.

To customize the build command, here is an example:

# my_app/home/management/commands/my_build_data_models.py
from automagic_rest.management.commands import build_data_models


class Command(build_data_models.Command):
    """
    My specific overrides for DRF PG Builder command.
    """

    def get_db(self, options):
        """
        Returns our customized Django DB name.
        """
        return "my_data"

    def get_owner(self, options):
        """
        Returns our customized schema owner.
        """
        return "my_user"

    def get_root_python_path(self, options):
        """
        Returns our customized build path.
        """
        return "my_data_path"

    def get_view(self):
        """
        Returns our customized view path.
        """
        return "my_app.views.MyDataViewSet"

    def get_allowed_schemata(self, options, cursor):
        """
        Return a list of allowed schemata we want to create RESTful
        endpoints for. If None, will create endpoints for all schemata
        owned by the schema owner user.
        """
        allowed_schemata = ['my_data', 'public_data']

        return allowed_schemata

class views.GenericViewSet

The view has several methods and attributes which can be overridden as well.

Attributes

index_sql: this attribute defines SQL to return the first column in each index for the current table for the Model. These will be used to dynamically make all indexed fields searchable and filterable.

Methods

get_serializer_class_name (default: rest_framework.serializers.ModelSerializer): the full path of the serializer class to use.

get_permission (default: None): returns a permission class to use for the endpoint. When left at the default of None, uses the default permission class set by Django REST Framework.

get_estimate_count_limit (default: 999_999): to prevent long-running SELECT COUNT(*) queries, the view estimates the number of rows in the table by examing the query plan. If greater than this number, it will estimate pagination counts for vastly improved speed.

To follow on the example above, here is an example of an overridden view, which sets the permission type and includes a mixin for naming Excel file downloads:

from rest_framework.permissions import IsAuthenticated
from drf_renderer_xlsx.mixins import XLSXFileMixin

class MyGenericViewSet(XLSXFileMixin, GenericViewSet):
    """
    """
    """
    Override the defaults from DRF PG Builder.
    """
    filename = 'my_export.xlsx'

    def get_permission(self):
        return IsAuthenticated

After the Files Are Built

After running the build command, you should have a directory created that you defined as path (or overrode with get_root_python_path()) that contains models, serializers, and a urls.py file. Include the urls.py file with a route from your Django project, and you should be able to visit the Django REST Framework browsable API.

Release Notes

0.2.4

  • Support for DRF version 3.11 (base_name must become basename in routes). Must use at least DRF version 3.9.

0.2.3

  • Add support for oid field types in the field mapping. Last version to support DRF versions before 3.9.

0.2.2

  • Ensure defaults for field type NUMERIC populate.

0.2.1

  • Pull the reserved word list from Python dynamically.

0.2.0

  • Refactored to use a generic serializer created on the fly. This is potentially a breaking change if you overrode the get_serializer method of the build_data_models command.
    • This has been replaced by a view method called get_serializer_class_name.
    • The serializer is now built on-the-fly rather than by the code generator.

0.1.2

  • Add support for DecimalField with max_digits and decimal_places from information_schema.columns fields.

0.1.1

  • Switched to naming models and serializers with a combination of schema_name and table_name to avoid model naming conflicts in Django if the same table exists across multiple schemata.

0.1.0

  • Initial release.

Maintainer

This package is maintained by the staff of Wharton Research Data Services. We are thrilled that The Wharton School allows us a certain amount of time to contribute to open-source projects. We add features as they are necessary for our projects, and try to keep up with Issues and Pull Requests as best we can. Due to constraints of time (our full time jobs!), Feature Requests without a Pull Request may not be implemented, but we are always open to new ideas and grateful for contributions and our package users.

Contributors (Thank You!)

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

automagic-rest-0.2.5.tar.gz (16.3 kB view details)

Uploaded Source

Built Distribution

automagic_rest-0.2.5-py3-none-any.whl (14.1 kB view details)

Uploaded Python 3

File details

Details for the file automagic-rest-0.2.5.tar.gz.

File metadata

  • Download URL: automagic-rest-0.2.5.tar.gz
  • Upload date:
  • Size: 16.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.3

File hashes

Hashes for automagic-rest-0.2.5.tar.gz
Algorithm Hash digest
SHA256 8a4cae78eefd8c82e5e42f4d8b45673b7b7879bcce69f2409757d315f828097f
MD5 e6669bea9fe050e866264c1184f06e0f
BLAKE2b-256 2ce4610e84a9cf5778edef1b06c46e29b1b1bd0887eceb2580fcdebbd694e720

See more details on using hashes here.

File details

Details for the file automagic_rest-0.2.5-py3-none-any.whl.

File metadata

  • Download URL: automagic_rest-0.2.5-py3-none-any.whl
  • Upload date:
  • Size: 14.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.3

File hashes

Hashes for automagic_rest-0.2.5-py3-none-any.whl
Algorithm Hash digest
SHA256 54d638e2ddfb2b38e9a3b42651b5b11151dfaefa7d33c2afa128cb54103cca97
MD5 e7851c1f5ab534efdeb6449f3f536243
BLAKE2b-256 77a7967f036ceacfd6e98c1384d3062e023755337e348eb6bda9795bf89ee8d0

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