Package: djangorestframework-guardian2
django-rest-framework-guardian2django-rest-framework-guardian2 provides django-guardian integrations for Django REST Framework.Installation & SetupTo use django-rest-framework-guardian2, install it into your environment.$pipinstalldjangorestframework-guardian2Ensure both Django REST Framework and django-guardian are configured and added to yourINSTALLED_APPSsetting.INSTALLED_APPS=['rest_framework','guardian',]AUTHENTICATION_BACKENDS=['django.contrib.auth.backends.ModelBackend','guardian.backends.ObjectPermissionBackend',]ObjectPermissionsFilterThe filter will ensure that querysets only returns objects for which the user has the appropriate view permission.If you're usingObjectPermissionsFilter, you'll probably also want to add an appropriate object permissions
class, to ensure that users can only operate on instances if they have the appropriate object permissions.  The easiest
way to do this is to subclassDjangoObjectPermissionsand add'view'permissions to theperms_mapattribute.An example using bothObjectPermissionsFilterandDjangoObjectPermissionsmight look like the following:permissions.py:fromrest_frameworkimportpermissionsclassCustomObjectPermissions(permissions.DjangoObjectPermissions):"""Similar to `DjangoObjectPermissions`, but adding 'view' permissions."""perms_map={'GET':['%(app_label)s.view_%(model_name)s'],'OPTIONS':['%(app_label)s.view_%(model_name)s'],'HEAD':['%(app_label)s.view_%(model_name)s'],'POST':['%(app_label)s.add_%(model_name)s'],'PUT':['%(app_label)s.change_%(model_name)s'],'PATCH':['%(app_label)s.change_%(model_name)s'],'DELETE':['%(app_label)s.delete_%(model_name)s'],}views.py:fromrest_frameworkimportviewsetsfromrest_framework_guardianimportfiltersfrommyapp.modelsimportEventfrommyapp.permissionsimportCustomObjectPermissionsfrommyapp.serializersimportEventSerializerclassEventViewSet(viewsets.ModelViewSet):"""Viewset that only lists events if user has 'view' permissions, and onlyallows operations on individual events if user has appropriate 'view', 'add','change' or 'delete' permissions."""queryset=Event.objects.all()serializer_class=EventSerializerpermission_classes=[CustomObjectPermissions]filter_backends=[filters.ObjectPermissionsFilter]ObjectPermissionsAssignmentMixinA serializer mixin that allows permissions to be easily assigned to users and/or groups.
So each time an object is created or updated, thepermissions_mapreturned bySerializer.get_permissions_mapwill be used to assign permission(s) to that object.Please note that the existing permissions will remain intact.A usage example might look like the following:fromrest_framework_guardian.serializersimportObjectPermissionsAssignmentMixinfromblog.modelsimportPostclassPostSerializer(ObjectPermissionsAssignmentMixin,serializers.ModelSerializer):classMeta:model=Postfields='__all__'defget_permissions_map(self,created):current_user=self.context['request'].userreaders=Group.objects.get(name='readers')supervisors=Group.objects.get(name='supervisors')return{'view_post':[current_user,readers],'change_post':[current_user],'delete_post':[current_user,supervisors]}Release ProcessUpdate changelogUpdate package version in setup.cfgCreate git tag for versionBuild & upload release to PyPI$rm-rfdist/build/
$pipinstall-Ubuildtwine
$python-mbuild
$twineupload-rtestdist/*
$twineuploaddist/*LicenseSee:LICENSEBSD 3-Clause LicenseCopyright (c) 2018, Ryan P Kilby
All rights reserved.Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.