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)
|
||||
)
|
||||
Reference in New Issue
Block a user