Package: drf-rehive-extras
DRF Rehive ExtrasExtra utilities for using Django REST Framework.FeaturesPython >= 3.6Django >= 3.0Generic 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 startedInstall the package:pipinstalldrf-rehive-extrasAdd "drf_rehive_extras" to yourINSTALLED_APPSsettings like this:INSTALLED_APPS=[# ...'drf_rehive_extras',]UsageSchema generationThis library allows you to usedrf-spectacularto generate Open API schemas that match the Rehive eveloped request/response format as defined by thedrf-rehive-extrasviews/serializers.To use schema generation, installdrf-spectacular(in addition todrf-rehive-extrasas described above):pipinstalldrf-spectacular[sidecar]And add the following to theINSTALLED_APPSsettings:INSTALLED_APPS=[# ...'drf_spectacular','drf_spectacular_sidecar',]Finally, configure theREST_FRAMEWORKsettings with:REST_FRAMEWORK={'DEFAULT_SCHEMA_CLASS':'drf_rehive_extras.schema.BaseAutoSchema',}Theschema.BaseAutoSchemaclass also includes functionality to attach
additional documentation to the schema via yaml files.To generate additional documentation, create adocs.yamlfile in adocsfolder 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:Pythonlabel:Python SDKsource:>Then, in yoursettingsfile specify the above directory using theADDITIONAL_DOCS_DIRSsettings:importosADDITIONAL_DOCS_DIRS=["/root/app/docs/",]PaginationThis library adds extra pagination viaPageNumberPaginationandCursorPaginationthat can be used to generate paginated lists that return the results in the Rehive eveloped request/response format.You can use them like this:fromdrf_rehive_extras.paginationimportPageNumberPaginationclassExampleView(ListAPIView)pagination_class=PageNumberPaginationThese pagination classes will be automatically applied to any views that inherit from thedrf-rehive-extrasgenerics and mixins.SerializersThis library includes base serializers that can be used to ensure all serializers share the same Rehive base:BaseModelSerializer: A DRFModelSerializerthat includesrest_flex_fieldsfunctionality.ActionResponseSerializer: A serializer that can be used to generate action responses.Serializers fieldsThis library adds extra serializer fields that can be used in DRF serializers:MetadataFieldTimestampFieldEnumFieldThese fields can be used like normal serializer fields.ViewsThis 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:fromdrf_rehive_extras.genericsimportListAPIViewclassListExampleModelView(ListAPIView):serializer_class=ExampleModelSerializerdefget_queryset(self):ifgetattr(self,'swagger_fake_view',False):returnExampleModel.objects.none()returnExampleModel.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 aserializer_classesattribute 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 theresponse_status_codesattribute.response_status_codes={"POST":status.HTTP_202_ACCEPTED}If possible, all generic views will attempt to add a_resourceand_resource_idto the request object. This will only be done if there is a single model instance and the instance contains aRESOURCEand/orRESOURCE_IDattribute.Finally, in addition to the normal DRF generic views, the library contains an extraActionAPIViewthat 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:fromdrf_rehive_extras.genericsimportActionAPIViewfromdrf_rehive_extras.serializersimportActionResponseSerializerclassExampleActionView(ActionAPIView):serializer_class=ExampleActionSerializerserializer_classes={"POST":(ExampleActionSerializer,ActionResponseSerializer,)}