diff --git a/core/apps/dashboard/serializers/user.py b/core/apps/dashboard/serializers/user.py index e7c32f5..667e30d 100644 --- a/core/apps/dashboard/serializers/user.py +++ b/core/apps/dashboard/serializers/user.py @@ -31,7 +31,7 @@ class UserListSerializer(serializers.ModelSerializer): } -class UserCreateSerializer(serializers.Serializer): +class UserAdminCreateSerializer(serializers.Serializer): first_name = serializers.CharField() last_name = serializers.CharField() region_id = serializers.IntegerField() diff --git a/core/apps/dashboard/urls.py b/core/apps/dashboard/urls.py index 1f915c5..096492a 100644 --- a/core/apps/dashboard/urls.py +++ b/core/apps/dashboard/urls.py @@ -9,6 +9,9 @@ urlpatterns = [ path('user/', include( [ path('list/', user_views.UserListApiView.as_view(), name='user-list-api'), + path('create/', user_views.UserCreateApiView.as_view(), name='user-create-api'), + path('/delete/', user_views.UserDeleteApiView.as_view(), name='user-delete-api'), + path('/update/', user_views.UserUpdateApiView.as_view(), name='user-update-api'), ], )), ] \ No newline at end of file diff --git a/core/apps/dashboard/views/user.py b/core/apps/dashboard/views/user.py index 6f32d7b..93ebdd6 100644 --- a/core/apps/dashboard/views/user.py +++ b/core/apps/dashboard/views/user.py @@ -3,8 +3,7 @@ from django.shortcuts import get_object_or_404 from django.db.models import Q # rest framework -from rest_framework import generics -from rest_framework.pagination import PageNumberPagination +from rest_framework import generics, views from rest_framework.permissions import IsAdminUser # drf yasg @@ -33,6 +32,20 @@ class UserListApiView(generics.GenericAPIView, ResponseMixin): description='Search by first_name or last_name', type=openapi.TYPE_STRING, required=False, + ), + openapi.Parameter( + name='is_active', + in_=openapi.IN_QUERY, + description="holati boyicha filter qilish", + type=openapi.TYPE_BOOLEAN, + required=False + ), + openapi.Parameter( + name="region_id", + in_=openapi.IN_QUERY, + description="region boyicha filter qilish", + type=openapi.TYPE_INTEGER, + required=False ) ], responses={ @@ -81,12 +94,19 @@ class UserListApiView(generics.GenericAPIView, ResponseMixin): queryset = self.queryset.exclude(id=request.user.id) # filters search = request.query_params.get('search') + is_active = request.query_params.get('is_active', None) + region_id = request.query_params.get('region_id') if search: queryset = queryset.filter( Q(first_name__istartswith=search) | Q(last_name__istartswith=search) ) + if is_active is not None: + queryset = queryset.filter(is_active=True if is_active.lower() == 'true' else False) + if region_id: + queryset = queryset.filter(region__id=region_id) + page = self.paginate_queryset(queryset=queryset) if page is not None: serializer = self.serializer_class(page, many=True) @@ -102,4 +122,231 @@ class UserListApiView(generics.GenericAPIView, ResponseMixin): message="Foydalanuvchilar ro'yxati", ) except Exception as e: - return self.error_response(str(e), message="xatolik") \ No newline at end of file + return self.error_response(str(e), message="xatolik") + + +class UserCreateApiView(generics.GenericAPIView, ResponseMixin): + serializer_class = serializers.UserAdminCreateSerializer + queryset = User.objects.all() + permission_classes = [IsAdminUser] + + @swagger_auto_schema( + responses={ + 201: openapi.Response( + schema=None, + description="Success", + examples={ + "application/json":{ + "status_code": 201, + "status": "success", + "message": "Foydalanuvchi qo'shildi", + "data": { + "id": 0, + "first_name": "string", + "last_name": "string", + "region": { + "id": 0, + "name": "string", + }, + "is_active": True, + "created_at": "string" + } + } + } + ), + 400: openapi.Response( + schema=None, + description="Failure", + examples={ + "application/json": { + "status_code": 400, + "status": "failure", + "message": "Foydalanuvchi qo'shilmadi", + "data": "string", + } + } + ), + 500: openapi.Response( + schema=None, + description="Failure", + examples={ + "application/json": { + "status_code": 500, + "status": "error", + "message": "xatolik", + "data": "string", + } + } + ), + } + ) + def post(self, request): + try: + serializer = self.serializer_class(data=request.data) + if serializer.is_valid(): + new_user = serializer.save() + return self.success_response( + data=serializers.UserListSerializer(new_user).data, + message="Foydalanuvchi qo'shildi", + status_code=201 + ) + + return self.failure_response( + data=serializer.errors, + message="Foydalanuvchi qo'shilmadi", + ) + except Exception as e: + return self.error_response(data=str(e), message='xatolik') + + + +class UserUpdateApiView(generics.GenericAPIView, ResponseMixin): + serializer_class = serializers.UserUpdateSerializer + queryset = User.objects.all() + permission_classes = [IsAdminUser] + + @swagger_auto_schema( + responses={ + 200: openapi.Response( + schema=None, + description="Success", + examples={ + "application/json": { + "status_code": 200, + "status": "success", + "message": "Foydalanuvchi tahrirlandi", + "data": { + "id": 0, + "first_name": "string", + "last_name": "string", + "region": { + "id": 0, + "name": "string" + }, + "is_active": True, + "created_at": "string" + } + } + } + ), + 404: openapi.Response( + schema=None, + description="Not Found", + examples={ + "application/json": { + "status_code": 404, + "success": "failure", + "message": "Foydalanuvchi topilmadi", + "data": {}, + } + } + ), + 400: openapi.Response( + schema=None, + description="Failure", + examples={ + "application/json": { + "status_code": 400, + "message": "Foydalanuvchi tahrirlanmadi", + "data": {} + } + } + ), + 500: openapi.Response( + schema=None, + description="Server Error", + examples={ + "application/json": { + "status_code": 500, + "status": "error", + "message": "xatolik", + "data": "string" + } + } + ), + } + ) + def patch(self, request, id): + try: + user = User.objects.filter(id=id).first() + if not user: + return self.failure_response( + data={}, message="Foydalanuvchi topilmadi", status_code=404 + ) + serializer = self.serializer_class(data=request.data, instance=user) + if serializer.is_valid(): + updated_user = serializer.save() + return self.success_response( + data=serializers.UserListSerializer(updated_user).data, + message="Foydalanuvchi tahrirlandi" + ) + return self.failure_response( + data=serializer.errors, + message="Foydalanuvchi tahrirlanmadi" + ) + except Exception as e: + return self.error_response(data=str(e), message="xatolik") + + + +class UserDeleteApiView(views.APIView, ResponseMixin): + permission_classes = [IsAdminUser] + + @swagger_auto_schema( + responses={ + 204: openapi.Response( + schema=None, + description="Success", + examples={ + "application/json": { + "status_code": 204, + "status": "success", + "message": "Foydalanuvchi o'chirildi", + "data": {} + } + } + ), + 404: openapi.Response( + schema=None, + description="Not Found", + examples={ + "application/json": { + "status_code": 404, + "success": "failure", + "message": "Foydalanuvchi topilmadi", + "data": {}, + } + } + ), + 500: openapi.Response( + schema=None, + description="Server Error", + examples={ + "application/json": { + "status_code": 500, + "status": "error", + "message": "xatolik", + "data": "string" + } + } + ), + } + ) + def delete(self, request, id): + try: + user = User.objects.filter(id=id).first() + if not user: + return self.failure_response( + data={}, + message="Foydalanuvchi topilmadi", + status_code=404 + ) + user.delete() + return self.success_response( + data={}, + message="Foydalanuvchi o'chirildi", + status_code=204 + ) + except Exception as e: + return self.error_response(data=str(e), message="xatolik") + \ No newline at end of file