|
1 | 1 | from logging import getLogger |
2 | 2 |
|
3 | | -from django.shortcuts import render |
4 | | -from django.views import View |
5 | 3 | from djoser.views import UserViewSet as BaseUserViewSet |
6 | 4 | from drf_spectacular.utils import OpenApiResponse, extend_schema |
7 | 5 | from rest_framework import status |
8 | 6 | from rest_framework.decorators import action |
9 | 7 | from rest_framework.response import Response |
10 | | -from rest_framework_simplejwt.tokens import RefreshToken |
11 | | -from rest_framework_simplejwt.views import ( |
12 | | - TokenObtainPairView as BaseTokenObtainPairView, |
13 | | - TokenRefreshView as BaseTokenRefreshView, |
14 | | - TokenVerifyView as BaseTokenVerifyView, TokenBlacklistView, |
15 | | -) |
16 | 8 |
|
17 | 9 | from .models import Profile |
18 | | -from .serializers import UserProfileSerializer, RefreshTokenSerializer |
| 10 | +from .serializers import UserProfileSerializer |
19 | 11 |
|
20 | 12 | logger = getLogger(__name__) |
21 | 13 |
|
@@ -226,123 +218,28 @@ def reset_password_confirm(self, request, *args, **kwargs): |
226 | 218 | logger.error(f"Error during password reset confirmation: {e}", exc_info=True) |
227 | 219 | raise |
228 | 220 |
|
229 | | - |
230 | | -class TokenObtainPairView(BaseTokenObtainPairView): |
231 | | - """ |
232 | | - Handle POST requests to obtain a new pair of access and refresh tokens. |
233 | | - """ |
234 | | - |
235 | 221 | @extend_schema( |
236 | | - operation_id="token_obtain", |
237 | | - description="Obtain a new pair of access and refresh tokens.", |
238 | | - tags=["User Authentication"], |
239 | | - responses={ |
240 | | - 200: OpenApiResponse(description="Token successfully obtained."), |
241 | | - 400: OpenApiResponse(description="Invalid credentials."), |
242 | | - } |
243 | | - ) |
244 | | - def post(self, request, *args, **kwargs): |
245 | | - try: |
246 | | - response = super().post(request, *args, **kwargs) |
247 | | - return Response({ |
248 | | - "message": "Token successfully obtained", |
249 | | - "data": response.data |
250 | | - }, status=response.status_code) |
251 | | - except Exception as e: |
252 | | - logger.error(f"Error during token obtain: {e}", exc_info=True) |
253 | | - raise |
254 | | - |
255 | | - |
256 | | -class TokenRefreshView(BaseTokenRefreshView): |
257 | | - """ |
258 | | - Handle POST requests to refresh an access token using a refresh token. |
259 | | - """ |
260 | | - |
261 | | - @extend_schema( |
262 | | - operation_id="token_refresh", |
263 | | - description="Refresh an access token using a refresh token.", |
264 | | - tags=["User Authentication"], |
265 | | - responses={ |
266 | | - 200: OpenApiResponse(description="Access token successfully refreshed."), |
267 | | - 400: OpenApiResponse(description="Invalid refresh token."), |
268 | | - } |
269 | | - ) |
270 | | - def post(self, request, *args, **kwargs): |
271 | | - try: |
272 | | - response = super().post(request, *args, **kwargs) |
273 | | - return Response({ |
274 | | - "message": "Access token successfully refreshed", |
275 | | - "data": response.data |
276 | | - }, status=response.status_code) |
277 | | - except Exception as e: |
278 | | - logger.error(f"Error during token refresh: {e}", exc_info=True) |
279 | | - raise |
280 | | - |
281 | | - |
282 | | -class TokenVerifyView(BaseTokenVerifyView): |
283 | | - """ |
284 | | - Verify if an access token is valid. |
285 | | - """ |
286 | | - |
287 | | - @extend_schema( |
288 | | - operation_id="token_verify", |
289 | | - description="Verify if an access token is valid.", |
290 | | - tags=["User Authentication"], |
| 222 | + operation_id="user_staff_check", |
| 223 | + description="Check if the authenticated user has staff privileges.", |
| 224 | + tags=["User Management"], |
291 | 225 | responses={ |
292 | | - 200: OpenApiResponse(description="Token is valid."), |
293 | | - 401: OpenApiResponse(description="Token is invalid or expired."), |
| 226 | + 200: OpenApiResponse(description="Staff status retrieved successfully."), |
| 227 | + 401: OpenApiResponse(description="Authentication required."), |
294 | 228 | } |
295 | 229 | ) |
296 | | - def post(self, request, *args, **kwargs): |
| 230 | + @action(detail=False, methods=['get']) |
| 231 | + def staff_check(self, request): |
| 232 | + """ |
| 233 | + Check if the authenticated user is a staff member. |
| 234 | + """ |
297 | 235 | try: |
298 | | - response = super().post(request, *args, **kwargs) |
| 236 | + is_staff = request.user.is_staff |
299 | 237 | return Response({ |
300 | | - "message": "Token is valid", |
301 | | - "data": response.data |
302 | | - }, status=response.status_code) |
| 238 | + "is_staff": is_staff, |
| 239 | + "message": f"User {'is' if is_staff else 'is not'} a staff member" |
| 240 | + }, status=status.HTTP_200_OK) |
303 | 241 | except Exception as e: |
304 | | - logger.error(f"Error during token verification: {e}", exc_info=True) |
305 | | - raise |
306 | | - |
307 | | - |
308 | | -class TokenDestroyView(TokenBlacklistView): |
309 | | - """ |
310 | | - Log out the user by blacklisting their refresh token. |
311 | | - """ |
312 | | - serializer_class = RefreshTokenSerializer |
313 | | - |
314 | | - @extend_schema( |
315 | | - operation_id="logout_user", |
316 | | - description="Log out the user by blacklisting their refresh token.", |
317 | | - tags=["User Authentication"], |
318 | | - request=RefreshTokenSerializer, |
319 | | - responses={ |
320 | | - 205: OpenApiResponse(description="Successfully logged out"), |
321 | | - 400: OpenApiResponse(description="Invalid Token"), |
322 | | - }, |
323 | | - ) |
324 | | - def post(self, request, *args, **kwargs): |
325 | | - serializer = self.get_serializer(data=request.data) |
326 | | - try: |
327 | | - serializer.is_valid(raise_exception=True) |
328 | | - refresh_token = serializer.validated_data["refresh"] |
329 | | - token = RefreshToken(refresh_token) |
330 | | - token.blacklist() |
| 242 | + logger.error("Error checking staff status: %s", e, exc_info=True) |
331 | 243 | return Response({ |
332 | | - "message": "Successfully logged out" |
333 | | - }, status=status.HTTP_205_RESET_CONTENT) |
334 | | - except Exception as e: |
335 | | - logger.error(f"Error during logout: {e}", exc_info=True) |
336 | | - raise |
337 | | - |
338 | | - |
339 | | -class ActivateView(View): |
340 | | - def get(self, request, uid, token): |
341 | | - return render( |
342 | | - request, |
343 | | - 'account/activate.html', |
344 | | - { |
345 | | - 'uid': uid, |
346 | | - 'token': token, |
347 | | - } |
348 | | - ) |
| 244 | + "error": "Unable to check staff status" |
| 245 | + }, status=status.HTTP_500_INTERNAL_SERVER_ERROR) |
0 commit comments