accounts: role create and list apis add
This commit is contained in:
110
core/apps/accounts/views/role/create.py
Normal file
110
core/apps/accounts/views/role/create.py
Normal file
@@ -0,0 +1,110 @@
|
||||
# rest framework
|
||||
from rest_framework import generics, permissions
|
||||
|
||||
# drf yasg
|
||||
from drf_yasg import openapi
|
||||
from drf_yasg.utils import swagger_auto_schema
|
||||
|
||||
|
||||
# accounts
|
||||
from core.apps.accounts.models import Role
|
||||
from core.apps.accounts.serializers.role.create import CreateRoleSerializer
|
||||
from core.apps.accounts.serializers.role.list import ListRoleSerializer
|
||||
|
||||
# utils
|
||||
from core.utils.response.mixin import ResponseMixin
|
||||
|
||||
|
||||
class CreateRoleApiView(generics.GenericAPIView, ResponseMixin):
|
||||
serializer_class = CreateRoleSerializer
|
||||
queryset = Role.objects.all()
|
||||
permission_classes = [permissions.IsAuthenticated]
|
||||
|
||||
@swagger_auto_schema(
|
||||
tags=['role'],
|
||||
operation_summary="Create a new role in the system",
|
||||
operation_description="""
|
||||
Create a new role with a name and optional comment.
|
||||
|
||||
Authentication:
|
||||
- Requires a valid Bearer access token.
|
||||
|
||||
Process:
|
||||
- Accepts role details in JSON format, validated using CreateRoleSerializer.
|
||||
- If the data is valid, a new role is created and returned.
|
||||
|
||||
Request:
|
||||
- Fields can include:
|
||||
- name (string, required): The name of the role.
|
||||
- comment (string, optional): Additional information about the role.
|
||||
|
||||
Response:
|
||||
- 200 OK: Role successfully created and returned.
|
||||
- 400 Bad Request: Validation failed, invalid input data.
|
||||
- 500 Internal Server Error: Unexpected error occurred while creating the role.
|
||||
|
||||
Notes:
|
||||
- Only authenticated users can create roles.
|
||||
- The response includes the newly created role's details with timestamps.
|
||||
""",
|
||||
responses={
|
||||
200: openapi.Response(
|
||||
schema=None,
|
||||
description="Success",
|
||||
examples={
|
||||
"application/json": {
|
||||
"status_code": 200,
|
||||
"status": "success",
|
||||
"message": "Role successfully created",
|
||||
"data": {
|
||||
"id": 0,
|
||||
"name": "string",
|
||||
"comment": "string",
|
||||
"created_at": "string",
|
||||
"updated_at": "string"
|
||||
}
|
||||
}
|
||||
}
|
||||
),
|
||||
400: openapi.Response(
|
||||
schema=None,
|
||||
description="Failure",
|
||||
examples={
|
||||
"application/json": {
|
||||
"status_code": 400,
|
||||
"status": "failure",
|
||||
"message": "Kiritayotgan malumotingizni tekshirib ko'ring",
|
||||
"data": "string"
|
||||
}
|
||||
}
|
||||
),
|
||||
500: openapi.Response(
|
||||
schema=None,
|
||||
description="Error",
|
||||
examples={
|
||||
"application/json": {
|
||||
"status_code": 500,
|
||||
"status": "error",
|
||||
"message": "Xatolik, Iltimos backend dasturchiga murojaat qiling",
|
||||
"data": "string"
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
)
|
||||
def post(self, request):
|
||||
try:
|
||||
serializer = self.serializer_class(data=request.data)
|
||||
if serializer.is_valid():
|
||||
obj = serializer.save()
|
||||
return self.created_response(
|
||||
data=ListRoleSerializer(obj).data,
|
||||
message="Role successfully created"
|
||||
)
|
||||
return self.failure_response(
|
||||
data=serializer.errors
|
||||
)
|
||||
except Exception as e:
|
||||
return self.error_response(
|
||||
data=str(e)
|
||||
)
|
||||
106
core/apps/accounts/views/role/list.py
Normal file
106
core/apps/accounts/views/role/list.py
Normal file
@@ -0,0 +1,106 @@
|
||||
# rest framework
|
||||
from rest_framework import generics, permissions
|
||||
|
||||
# drf yasg
|
||||
from drf_yasg import openapi
|
||||
from drf_yasg.utils import swagger_auto_schema
|
||||
|
||||
|
||||
# accounts
|
||||
from core.apps.accounts.models import Role
|
||||
from core.apps.accounts.serializers.role.list import ListRoleSerializer
|
||||
|
||||
# utils
|
||||
from core.utils.response.mixin import ResponseMixin
|
||||
|
||||
|
||||
class ListRoleApiView(generics.GenericAPIView, ResponseMixin):
|
||||
serializer_class = ListRoleSerializer
|
||||
queryset = Role.objects.all()
|
||||
permission_classes = [permissions.IsAuthenticated]
|
||||
|
||||
@swagger_auto_schema(
|
||||
tags=['role'],
|
||||
operation_summary="Retrieve a paginated list of roles.",
|
||||
operation_description="""
|
||||
Get a list of all roles in the system, with optional pagination.
|
||||
|
||||
Authentication:
|
||||
- Requires a valid Bearer access token.
|
||||
|
||||
Process:
|
||||
- Retrieves all roles that are not marked as deleted.
|
||||
- Supports pagination using the configured pagination class (limit/offset).
|
||||
- Returns serialized role data along with pagination metadata.
|
||||
|
||||
Response:
|
||||
- 200 OK: Successfully returns a paginated list of roles.
|
||||
- Includes fields: count, next, previous, results.
|
||||
- Each role includes id, name, comment, created_at, and updated_at.
|
||||
- 500 Internal Server Error: Unexpected error occurred while fetching roles.
|
||||
|
||||
Notes:
|
||||
- Only authenticated users can access this endpoint.
|
||||
- Roles marked as deleted (`is_deleted=True`) are excluded from the response.
|
||||
- Pagination fields (`next` and `previous`) provide URLs to navigate pages if results exceed page limit.
|
||||
|
||||
""",
|
||||
responses={
|
||||
200: openapi.Response(
|
||||
description="Succes",
|
||||
schema=None,
|
||||
examples={
|
||||
"application/json": {
|
||||
"status_code": 200,
|
||||
"status": "success",
|
||||
"message": "Roles list",
|
||||
"data": {
|
||||
"count": 0,
|
||||
"next": "string",
|
||||
"previous": "string",
|
||||
"results": [
|
||||
{
|
||||
"id": 1,
|
||||
"name": "string",
|
||||
"comment": "string",
|
||||
"created_at": "string",
|
||||
"updated_at": "string"
|
||||
},
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
),
|
||||
500: openapi.Response(
|
||||
description="Error",
|
||||
schema=None,
|
||||
examples={
|
||||
"application/json": {
|
||||
"status_code": 500,
|
||||
"status": "error",
|
||||
"message": "Xatolik, Iltimos backend dasturchiga murojaat qiling",
|
||||
"data": "string"
|
||||
}
|
||||
}
|
||||
),
|
||||
}
|
||||
)
|
||||
def get(self, request):
|
||||
try:
|
||||
queryset = self.queryset.filter(is_deleted=False)
|
||||
page = self.paginate_queryset(queryset)
|
||||
if page is not None:
|
||||
serializer = self.serializer_class(page, many=True)
|
||||
return self.success_response(
|
||||
data=self.get_paginated_response(serializer.data).data,
|
||||
message="Roles list"
|
||||
)
|
||||
serializer = self.serializer_class(queryset, many=True)
|
||||
return self.success_response(
|
||||
data=serializer.data,
|
||||
message="Roles list"
|
||||
)
|
||||
except Exception as e:
|
||||
return self.error_response(
|
||||
data=str(e)
|
||||
)
|
||||
52
core/apps/accounts/views/role/update.py
Normal file
52
core/apps/accounts/views/role/update.py
Normal file
@@ -0,0 +1,52 @@
|
||||
# rest framework
|
||||
from rest_framework import generics, permissions
|
||||
|
||||
# drf yasg
|
||||
from drf_yasg import openapi
|
||||
from drf_yasg.utils import swagger_auto_schema
|
||||
|
||||
|
||||
# accounts
|
||||
from core.apps.accounts.models import Role
|
||||
from core.apps.accounts.serializers.role.update import UpdateRoleSerializer
|
||||
from core.apps.accounts.serializers.role.list import ListRoleSerializer
|
||||
|
||||
# utils
|
||||
from core.utils.response.mixin import ResponseMixin
|
||||
|
||||
|
||||
class UpdateRoleApiView(generics.GenericAPIView, ResponseMixin):
|
||||
serializer_class = UpdateRoleSerializer
|
||||
queryset = Role.objects.filter(is_deleted=False)
|
||||
permission_classes = [permissions.IsAuthenticated]
|
||||
|
||||
@swagger_auto_schema(
|
||||
tags=['role'],
|
||||
operation_summary="",
|
||||
operation_description="""
|
||||
|
||||
""",
|
||||
responses={}
|
||||
)
|
||||
def patch(self, request, id):
|
||||
try:
|
||||
instance = Role.objects.filter(id=id).first()
|
||||
if not instance:
|
||||
return self.not_found_response(data={}, message="Role not found with this id")
|
||||
serializer = self.serializer_class(data=request.data, instance=instance, partial=True)
|
||||
if serializer.is_valid():
|
||||
updated_instance = serializer.save()
|
||||
return self.success_response(
|
||||
data=ListRoleSerializer(updated_instance).data,
|
||||
message="Role successfully updated"
|
||||
)
|
||||
return self.failure_response(
|
||||
data=serializer.errors,
|
||||
)
|
||||
except Exception as e:
|
||||
return self.error_response(
|
||||
data=str(e)
|
||||
)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user