foydalanuvchi uchun qoshish, ochirish va tahrirlash apilari qoshildi
This commit is contained in:
@@ -31,7 +31,7 @@ class UserListSerializer(serializers.ModelSerializer):
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
class UserCreateSerializer(serializers.Serializer):
|
class UserAdminCreateSerializer(serializers.Serializer):
|
||||||
first_name = serializers.CharField()
|
first_name = serializers.CharField()
|
||||||
last_name = serializers.CharField()
|
last_name = serializers.CharField()
|
||||||
region_id = serializers.IntegerField()
|
region_id = serializers.IntegerField()
|
||||||
|
|||||||
@@ -9,6 +9,9 @@ urlpatterns = [
|
|||||||
path('user/', include(
|
path('user/', include(
|
||||||
[
|
[
|
||||||
path('list/', user_views.UserListApiView.as_view(), name='user-list-api'),
|
path('list/', user_views.UserListApiView.as_view(), name='user-list-api'),
|
||||||
|
path('create/', user_views.UserCreateApiView.as_view(), name='user-create-api'),
|
||||||
|
path('<int:id>/delete/', user_views.UserDeleteApiView.as_view(), name='user-delete-api'),
|
||||||
|
path('<int:id>/update/', user_views.UserUpdateApiView.as_view(), name='user-update-api'),
|
||||||
],
|
],
|
||||||
)),
|
)),
|
||||||
]
|
]
|
||||||
@@ -3,8 +3,7 @@ from django.shortcuts import get_object_or_404
|
|||||||
from django.db.models import Q
|
from django.db.models import Q
|
||||||
|
|
||||||
# rest framework
|
# rest framework
|
||||||
from rest_framework import generics
|
from rest_framework import generics, views
|
||||||
from rest_framework.pagination import PageNumberPagination
|
|
||||||
from rest_framework.permissions import IsAdminUser
|
from rest_framework.permissions import IsAdminUser
|
||||||
|
|
||||||
# drf yasg
|
# drf yasg
|
||||||
@@ -33,6 +32,20 @@ class UserListApiView(generics.GenericAPIView, ResponseMixin):
|
|||||||
description='Search by first_name or last_name',
|
description='Search by first_name or last_name',
|
||||||
type=openapi.TYPE_STRING,
|
type=openapi.TYPE_STRING,
|
||||||
required=False,
|
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={
|
responses={
|
||||||
@@ -81,12 +94,19 @@ class UserListApiView(generics.GenericAPIView, ResponseMixin):
|
|||||||
queryset = self.queryset.exclude(id=request.user.id)
|
queryset = self.queryset.exclude(id=request.user.id)
|
||||||
# filters
|
# filters
|
||||||
search = request.query_params.get('search')
|
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:
|
if search:
|
||||||
queryset = queryset.filter(
|
queryset = queryset.filter(
|
||||||
Q(first_name__istartswith=search) |
|
Q(first_name__istartswith=search) |
|
||||||
Q(last_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)
|
page = self.paginate_queryset(queryset=queryset)
|
||||||
if page is not None:
|
if page is not None:
|
||||||
serializer = self.serializer_class(page, many=True)
|
serializer = self.serializer_class(page, many=True)
|
||||||
@@ -103,3 +123,230 @@ class UserListApiView(generics.GenericAPIView, ResponseMixin):
|
|||||||
)
|
)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return self.error_response(str(e), message="xatolik")
|
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")
|
||||||
|
|
||||||
Reference in New Issue
Block a user