fix: users.py file code clear
This commit is contained in:
@@ -1,18 +1,14 @@
|
|||||||
import uuid
|
import uuid
|
||||||
|
from typing import cast
|
||||||
|
|
||||||
from drf_spectacular.utils import extend_schema
|
from drf_spectacular.utils import extend_schema
|
||||||
|
|
||||||
from rest_framework.generics import GenericAPIView # type: ignore
|
from rest_framework.generics import GenericAPIView # type: ignore
|
||||||
from rest_framework.decorators import action # type: ignore
|
from rest_framework.permissions import IsAdminUser # type: ignore
|
||||||
from rest_framework.permissions import IsAdminUser
|
|
||||||
from rest_framework import status # type: ignore
|
from rest_framework import status # type: ignore
|
||||||
from rest_framework.request import HttpRequest # type: ignore
|
from rest_framework.request import HttpRequest # type: ignore
|
||||||
from rest_framework.response import Response # type: ignore
|
from rest_framework.response import Response # type: ignore
|
||||||
from rest_framework.permissions import ( # type: ignore
|
from rest_framework.permissions import IsAdminUser # type: ignore
|
||||||
IsAdminUser,
|
|
||||||
)
|
|
||||||
|
|
||||||
from rest_framework.generics import get_object_or_404 # type: ignore
|
|
||||||
from django.contrib.auth import get_user_model
|
from django.contrib.auth import get_user_model
|
||||||
from django.db import transaction
|
from django.db import transaction
|
||||||
|
|
||||||
@@ -31,7 +27,7 @@ UserModel = get_user_model()
|
|||||||
|
|
||||||
|
|
||||||
######################################################################
|
######################################################################
|
||||||
# /users/{id}/companies
|
# @api-view | POST, GET - users/{id}/companies
|
||||||
######################################################################
|
######################################################################
|
||||||
@extend_schema(tags=["User Companies"])
|
@extend_schema(tags=["User Companies"])
|
||||||
class UserCompanyApiView(BaseApiViewMixin, GenericAPIView): # type: ignore
|
class UserCompanyApiView(BaseApiViewMixin, GenericAPIView): # type: ignore
|
||||||
@@ -70,21 +66,14 @@ class UserCompanyApiView(BaseApiViewMixin, GenericAPIView): # type: ignore
|
|||||||
summary="Create Company For User",
|
summary="Create Company For User",
|
||||||
description="Create Company For User",
|
description="Create Company For User",
|
||||||
)
|
)
|
||||||
def post(
|
def post(self, request: HttpRequest, *args: object, **kwargs: object) -> Response:
|
||||||
self,
|
|
||||||
request: HttpRequest,
|
|
||||||
pk: uuid.UUID,
|
|
||||||
*args: object,
|
|
||||||
**kwargs: object,
|
|
||||||
) -> Response:
|
|
||||||
with transaction.atomic():
|
with transaction.atomic():
|
||||||
ser = CreateCompanySerializer(data=request.data) # type: ignore
|
ser = cast(CreateCompanySerializer, self.get_serializer(data=request.data)) # type: ignore
|
||||||
ser.is_valid(raise_exception=True)
|
ser.is_valid(raise_exception=True)
|
||||||
company = ser.save() # type: ignore
|
company = ser.save()
|
||||||
|
|
||||||
user = get_object_or_404(UserModel, pk=pk)
|
user = self.get_object()
|
||||||
|
|
||||||
account = CompanyAccountModel(company=company, user=user)
|
account = CompanyAccountModel(company=company, user=user)
|
||||||
account.save()
|
account.save()
|
||||||
|
|
||||||
return Response(data=ser.data, status=status.HTTP_201_CREATED)
|
return Response(data=ser.data, status=status.HTTP_201_CREATED)
|
||||||
|
|||||||
Reference in New Issue
Block a user