Я написал настраиваемое разрешение, чтобы пользователь сделал запрос с ролью Владелец зарядной станции , чтобы иметь возможность выполнять все доступные действия в конечной точке, ['list','create','retrieve','update','partial_update','destroy']
мое разрешение:
class IsChargingStationOwner(LoginRequiredMixin,permissions.BasePermission):
message = 'You do not have the role of Charging Station Owner'
def has_permission(self, request, view):
if request.user.is_authenticated:
current_user = request.user
current_role = current_user.role
if current_role.name == "Charging Station Owner" and view.action in ['list','create','retrieve','update','partial_update','destroy']:
return True
else:
return False
мое мнение:
class StationAvailabilityList(generics.ListAPIView):
serializer_class = StationAvailabilitySerializer
permission_classes = [IsChargingStationOwner]
def get_queryset(self):
#get the station owner
station_owner = self.request.user
#get the station id
station_id = self.kwargs['station_id']
#get the station
station = Station.objects.get(id = station_id)
#get the station availability records
availabilities = StationAvailability.objects.filter(station = station)
return availabilities
Когда я запускаю конечную точку, я выхожу с ошибкой:
AttributeError: 'StationAvailabilityList' object has no attribute 'action'
Вот полный Traceback :
Request Method: GET
Request URL: http://127.0.0.1:8000/api/stations/availability/88edd36b-7127-4968-ae5f-97a7e14c6dcf/
Django Version: 3.0.6
Python Version: 3.7.8
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.gis',
'rest_framework',
'rest_framework_gis',
'djoser',
'corsheaders',
'users',
'stations',
'wallet',
'utils']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'corsheaders.middleware.CorsMiddleware']
Traceback (most recent call last):
File "/home/gtopal/.local/share/virtualenvs/ev-loader-backend-q-Kph_Wy/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/home/gtopal/.local/share/virtualenvs/ev-loader-backend-q-Kph_Wy/lib/python3.7/site-packages/django/core/handlers/base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/gtopal/.local/share/virtualenvs/ev-loader-backend-q-Kph_Wy/lib/python3.7/site-packages/django/core/handlers/base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/gtopal/.local/share/virtualenvs/ev-loader-backend-q-Kph_Wy/lib/python3.7/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "/home/gtopal/.local/share/virtualenvs/ev-loader-backend-q-Kph_Wy/lib/python3.7/site-packages/django/views/generic/base.py", line 71, in view
return self.dispatch(request, *args, **kwargs)
File "/home/gtopal/.local/share/virtualenvs/ev-loader-backend-q-Kph_Wy/lib/python3.7/site-packages/rest_framework/views.py", line 505, in dispatch
response = self.handle_exception(exc)
File "/home/gtopal/.local/share/virtualenvs/ev-loader-backend-q-Kph_Wy/lib/python3.7/site-packages/rest_framework/views.py", line 465, in handle_exception
self.raise_uncaught_exception(exc)
File "/home/gtopal/.local/share/virtualenvs/ev-loader-backend-q-Kph_Wy/lib/python3.7/site-packages/rest_framework/views.py", line 476, in raise_uncaught_exception
raise exc
File "/home/gtopal/.local/share/virtualenvs/ev-loader-backend-q-Kph_Wy/lib/python3.7/site-packages/rest_framework/views.py", line 493, in dispatch
self.initial(request, *args, **kwargs)
File "/home/gtopal/.local/share/virtualenvs/ev-loader-backend-q-Kph_Wy/lib/python3.7/site-packages/rest_framework/views.py", line 411, in initial
self.check_permissions(request)
File "/home/gtopal/.local/share/virtualenvs/ev-loader-backend-q-Kph_Wy/lib/python3.7/site-packages/rest_framework/views.py", line 332, in check_permissions
if not permission.has_permission(request, self):
File "/home/gtopal/Desktop/ev-loader-backend/stations/permissions.py", line 17, in has_permission
if current_role.name == "Charging Station Owner" and view.action in ['list','create','retrieve','update','partial_update','destroy']:
Exception Type: AttributeError at /api/stations/availability/88edd36b-7127-4968-ae5f-97a7e14c6dcf/
Exception Value: 'StationAvailabilityList' object has no attribute 'action'
Кто-нибудь может мне помочь разобраться, что происходит? Почему у меня нет доступа к конечной точке?
конечная точка:
path('availability/<uuid:station_id>/', StationAvailabilityList.as_view(),name = 'station_availability_list'),