Skip to main content

Extras for DRF

Project description

Rehive Logo

DRF Rehive Extras

Extra utilities for using Django REST Framework.

Features

  • Python >= 3.6
  • Django >= 3.0
  • Generic views and mixins for all CRUD.
  • Custom pagination that supports cursor and page based pagination.
  • Integrated support for flex fields and django filters.
  • Base serializers.
  • Metadata, timestamp, and enum serializer fields.
  • Schema generation support via drf-spectacular.

Getting started

  1. Install the package:
pip install drf-rehive-extras
  1. Add "drf_rehive_extras" to your INSTALLED_APPS settings like this:
INSTALLED_APPS = [
    # ...
    'drf_rehive_extras',
]

Usage

Schema generation

This library allows you to use drf-spectacular to generate Open API schemas that match the Rehive eveloped request/response format as defined by the drf-rehive-extras views/serializers.

To use schema generation, install drf-spectacular (in addition to drf-rehive-extras as described above):

pip install drf-spectacular[sidecar]

And add the following to the INSTALLED_APPS settings:

INSTALLED_APPS = [
    # ...
    'drf_spectacular',
    'drf_spectacular_sidecar',
]

Finally, configure the REST_FRAMEWORK settings with:

REST_FRAMEWORK = {
  'DEFAULT_SCHEMA_CLASS': 'drf_rehive_extras.schema.BaseAutoSchema',
}

The schema.BaseAutoSchema class also includes functionality to attach additional documentation to the schema via yaml files.

To generate additional documentation, create a docs.yaml file in a docs folder in your Django app. The file should be formatted like this:

app.views.ExampleView:
    GET:
        operationId:
        summary:
        description:
        x-code-samples:
            - lang:
              label:
              source: >
            - lang: Python
              label: Python SDK
              source: >

Then, in your settings file specify the above directory using the ADDITIONAL_DOCS_DIRS settings:

import os

ADDITIONAL_DOCS_DIRS = [
    "/root/app/docs/",
]

Pagination

This library adds extra pagination via PageNumberPagination and CursorPagination that can be used to generate paginated lists that return the results in the Rehive eveloped request/response format.

You can use them like this:

from drf_rehive_extras.pagination import PageNumberPagination

class ExampleView(ListAPIView)
    pagination_class = PageNumberPagination

These pagination classes will be automatically applied to any views that inherit from the drf-rehive-extras generics and mixins.

Serializers

This library includes base serializers that can be used to ensure all serializers share the same Rehive base:

  • BaseModelSerializer : A DRF ModelSerializer that includes rest_flex_fields functionality.
  • ActionResponseSerializer : A serializer that can be used to generate action responses.

Serializers fields

This library adds extra serializer fields that can be used in DRF serializers:

  • MetadataField
  • TimestampField
  • EnumField

These fields can be used like normal serializer fields.

Views

This library includes a collection of generic views and mixins that can be used to ensure all views follow the same Rehive standard.

The generic views can be used like this:

from drf_rehive_extras.generics import ListAPIView

class ListExampleModelView(ListAPIView):
    serializer_class = ExampleModelSerializer

    def get_queryset(self):
        if getattr(self, 'swagger_fake_view', False):
            return ExampleModel.objects.none()

        return ExampleModel.objects.all().order_by('-created')

The generic views allow for the customization of the serializer based on the method. This can be done by adding a serializer_classes attribute to the view:

# Single shared request/response serializer.
serializer_classes = {
    "POST": ExampleModelRequestSerializer
}

# Multiple serializers, the first for the request and the second for the response.
serializer_classes = {
    "POST": (ExampleModelRequestSerializer, ExampleModelResponseSerializer,)
}

The generic views also support explicitly modifying the response status for a given method. This can be done via the response_status_codes attribute.

response_status_codes = {"POST": status.HTTP_202_ACCEPTED}

If possible, all generic views will attempt to add a _resource and _resource_id to the request object. This will only be done if there is a single model instance and the instance contains a RESOURCE and/or RESOURCE_ID attribute.

Finally, in addition to the normal DRF generic views, the library contains an extra ActionAPIView that can be used for simple actions. These actions will default to a 200 response and will only ever return a {"status": "success"} response.

Usage is as follows:

from drf_rehive_extras.generics import ActionAPIView
from drf_rehive_extras.serializers import ActionResponseSerializer

class ExampleActionView(ActionAPIView):
    serializer_class = ExampleActionSerializer
    serializer_classes = {
        "POST": (ExampleActionSerializer, ActionResponseSerializer,)
    }

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

drf_rehive_extras-2.2.4.tar.gz (15.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

drf_rehive_extras-2.2.4-py3-none-any.whl (15.0 kB view details)

Uploaded Python 3

File details

Details for the file drf_rehive_extras-2.2.4.tar.gz.

File metadata

  • Download URL: drf_rehive_extras-2.2.4.tar.gz
  • Upload date:
  • Size: 15.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.20

File hashes

Hashes for drf_rehive_extras-2.2.4.tar.gz
Algorithm Hash digest
SHA256 23e70364632f4c3e20bac965b39c413d2312995f1ae434b0229f22887fef81ec
MD5 6c51319a82783361db7868d2c6e5076d
BLAKE2b-256 2279dbbd92513ae3b64f34af2bd31acbe2ff5d622b84a20f1875b1a7c31edff2

See more details on using hashes here.

File details

Details for the file drf_rehive_extras-2.2.4-py3-none-any.whl.

File metadata

  • Download URL: drf_rehive_extras-2.2.4-py3-none-any.whl
  • Upload date:
  • Size: 15.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.8.3 keyring/23.4.1 rfc3986/1.5.0 colorama/0.4.4 CPython/3.6.13

File hashes

Hashes for drf_rehive_extras-2.2.4-py3-none-any.whl
Algorithm Hash digest
SHA256 3281b1c5617e71ec0bb77e24dccba1272f41e9b28d7486167babb8f9873440b8
MD5 3b94c5a90487323f9a01c95e0322c73a
BLAKE2b-256 01a906541381142953ac39e8e610d9cb01cb8efd775acf6338e8e9a7923ee9c7

See more details on using hashes here.

Supported by

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