Skip to content

Commit

Permalink
Fixes netbox-community#1563: Made necessary updates for Django REST F…
Browse files Browse the repository at this point in the history
…ramework v3.7.0
  • Loading branch information
jeremystretch authored and John Anderson committed Oct 13, 2017
1 parent c883ac3 commit 794215a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
2 changes: 1 addition & 1 deletion netbox/netbox/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@
'utilities.api.TokenAuthentication',
),
'DEFAULT_FILTER_BACKENDS': (
'rest_framework.filters.DjangoFilterBackend',
'django_filters.rest_framework.DjangoFilterBackend',
),
'DEFAULT_PAGINATION_CLASS': 'utilities.api.OptionalLimitOffsetPagination',
'DEFAULT_PERMISSION_CLASSES': (
Expand Down
21 changes: 14 additions & 7 deletions netbox/utilities/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@
from django.contrib.contenttypes.models import ContentType

from rest_framework import authentication, exceptions
from rest_framework.compat import is_authenticated
from rest_framework.exceptions import APIException
from rest_framework.pagination import LimitOffsetPagination
from rest_framework.permissions import BasePermission, DjangoModelPermissions, SAFE_METHODS
from rest_framework.renderers import BrowsableAPIRenderer
from rest_framework.serializers import Field, ModelSerializer, ValidationError
from rest_framework.views import get_view_name as drf_get_view_name
from rest_framework.utils import formatting

from users.models import Token

Expand Down Expand Up @@ -75,7 +74,7 @@ class IsAuthenticatedOrLoginNotRequired(BasePermission):
def has_permission(self, request, view):
if not settings.LOGIN_REQUIRED:
return True
return request.user and is_authenticated(request.user)
return request.user.is_authenticated()


#
Expand Down Expand Up @@ -233,10 +232,18 @@ def get_view_name(view_cls, suffix=None):
Derive the view name from its associated model, if it has one. Fall back to DRF's built-in `get_view_name`.
"""
if hasattr(view_cls, 'queryset'):
# Determine the model name from the queryset.
name = view_cls.queryset.model._meta.verbose_name
name = ' '.join([w[0].upper() + w[1:] for w in name.split()]) # Capitalize each word
if suffix:
name = "{} {}".format(name, suffix)
return name

return drf_get_view_name(view_cls, suffix)
else:
# Replicate DRF's built-in behavior.
name = view_cls.__name__
name = formatting.remove_trailing_string(name, 'View')
name = formatting.remove_trailing_string(name, 'ViewSet')
name = formatting.camelcase_to_spaces(name)

if suffix:
name += ' ' + suffix

return name

0 comments on commit 794215a

Please sign in to comment.