Skip to main content

No project description provided

Project description

drfseriperm

Library for Django REST framework which provides permissions-based serialization


Installing

[!NOTE] It's recommended to activate Virtual Environment before installing drfseriperm

To clone and install required packages use the following command:

# linux/macOS
$ python3 -m pip install drfseriperm

# windows
$ py -3 -m pip install drfseriperm

Quick example

import django.contrib.auth.models

import rest_framework.permissions
import rest_framework.serializers

import drfseriperm


class DefaultUserFFP(drfseriperm.FieldsForPermissions):
    include = ['id', 'username', 'date_joined']
    extra_kwargs = {
        'username': {'read_only': True},
        'date_joined': {'read_only': True},
    }

    
class StaffFFP(drfseriperm.FieldsForPermissions):
    include = ['email']
    permissions = [rest_framework.permissions.IsAdminUser]
    extra_kwargs = {
        'username': {'read_only': False},
        'email': {'read_only': True},
    }


class UserSerializer(drfseriperm.PermissionBasedModelSerializerMixin,
                     rest_framework.serializers.ModelSerializer):

    class Meta:
        model = django.contrib.auth.models.User
        list_fields_for_permissions = [
            DefaultUserFFP,
            StaffFFP,
        ]

As can be seen from the example, while using drfseriperm we should inherit our classes from the FieldsForPermissions (FFP) class, which works with include, exclude, extra_kwargs, permissions and http_methods attributes. Then FFP instances are built by the PermissionBasedModelSerializerMixin from list_fields_for_permissions meta attribute and then converted to the list of the available for the user fields


FieldsForPermissions

Container and formatter for parameters to be passed into PermissionBasedModelSerializerMixin.
Parameters:

  • include — names of fields to be included for some permission group
  • exclude — names of fields to be excluded for some permission group
  • permissions — permissions (DRF BasePermission instance) which are required to consider the FFP
  • extra_kwargs — extra kwargs for each fields, has the same structure as the DRF extra_kwargs meta attribute (more information on its official documentation). Note that these kwargs are applied to fields when the complete list of fields is built, so even if field is not specified in include or exclude it will anyway receive the extra kwargs
  • http_methods — names of http methods for which FFP is applied All of them can be either specified as a class attributes or passed as init params

PermissionBasedSerializerMixin

Mixin for DRF ModelSerializer which filters fields available for the user based on their permissions and request method. This mixin works with attributes specified in Meta class of a ModelSerializer subclass. Meta attributes:

  • list_fields_for_permissions - list containing subclasses or instances of FieldsForPermissions. DRF ALL_FIELDS (or __all__) are also appropriate values (to dynamically get this list "get_list_ffps()" method can be overriden, defaults to empty list)
  • inherit_list_fields_for_permissions - boolean value indicating if an ffp should inherit fields of a previous available for request ffp (to dynamically get this value "get_ffps_inherit_state()" method can be overriden, defaults to True)
  • reverse_list_fields_for_permissions - boolean value showing if mixin should apply ffps specified in list_fields_for_permission in reversed order or not (to_dynamically get this value, "get_ffps_reverse_state()" method can be overriden, default to False)
  • inherit_fields_for_permissions_extra_kwargs - boolean value letting mixin know if ffps should inherit extra kwargs of a previous available for request ffp (to dynamically get this value "get_extra_kwargs_inherit_state()" method can be overriden, defaults to True)
  • raise_for_no_fields - boolean value standing for raising an exception in case if no fields are available for request method (e.g. if all the ffps have http_methods parameter set to ['GET'], then for other http methods the exception will be raised, to dynamically get this value "get_raise_for_no_fields_state()" method can be overriden, defaults to True)
  • no_fields_exception - DRF APIException subclass or instance, which is an exception to be raised in case if there are no fields available for a request and raise_for_no_fields is set to True (to dynamically get this value, "get_no_fields_exception()" method can be overriden, default to DRF MethodNotAllowed with 405 http status)

Also PermissionBasedSerializerMixin has respect to native meta attributes of ModelSerializer related to fields:

  • fields - list of field names, which will be included in all the ffps passed
  • exclude - list of field names, which will be excluded from all the ffps passed
  • extra_kwargs - dictionary containing field_name-field_properties pairs, which will be considered by all the ffps passed

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

drfseriperm-1.0.1.tar.gz (10.7 kB view details)

Uploaded Source

Built Distribution

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

drfseriperm-1.0.1-py3-none-any.whl (10.2 kB view details)

Uploaded Python 3

File details

Details for the file drfseriperm-1.0.1.tar.gz.

File metadata

  • Download URL: drfseriperm-1.0.1.tar.gz
  • Upload date:
  • Size: 10.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.9

File hashes

Hashes for drfseriperm-1.0.1.tar.gz
Algorithm Hash digest
SHA256 f7f156bf6d7c786387e945874864662854fe3b1e6c288295c4178dc634b95a08
MD5 1a7d14fa749deda018b2b3507404f1a7
BLAKE2b-256 a0ef47c62464da00bfa95c4c60c898af1f65fe4a8044dde7ecdb3eb8c8f0cffc

See more details on using hashes here.

File details

Details for the file drfseriperm-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: drfseriperm-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 10.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.9

File hashes

Hashes for drfseriperm-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 44ab36de3ad83b2e4b7262f5b6ba6152f57c04f16a437a859151ca5301d7ddd9
MD5 183cbf64d4953f0d581ea0f7f629ec76
BLAKE2b-256 ccc0386df946e0e1abdf7f805c3506132544fc38442e6c1e6b731bc2f6780c0f

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