add/fix: small changes on docs and comment style
This commit is contained in:
@@ -4,24 +4,16 @@ Accounts app urls
|
||||
|
||||
from django.urls import path, include
|
||||
from rest_framework_simplejwt import views as jwt_views
|
||||
from .views import (
|
||||
RegisterView,
|
||||
ResetPasswordView,
|
||||
MeView,
|
||||
ChangePasswordView,
|
||||
MeCompanyView,
|
||||
)
|
||||
from . import views
|
||||
from rest_framework.routers import DefaultRouter # type: ignore
|
||||
|
||||
|
||||
router = DefaultRouter()
|
||||
router.register("auth", RegisterView, basename="auth") # type: ignore
|
||||
router.register("auth", ResetPasswordView, basename="reset-password") # type: ignore
|
||||
router.register("auth", MeView, basename="me") # type: ignore
|
||||
router.register("auth", ChangePasswordView, basename="change-password") # type: ignore
|
||||
|
||||
router.register(r"me/companies", MeCompanyView, "me-company") # type: ignore
|
||||
|
||||
router.register("auth", views.RegisterView, basename="auth") # type: ignore
|
||||
router.register("auth", views.ResetPasswordView, basename="reset-password") # type: ignore
|
||||
router.register("auth", views.MeView, basename="me") # type: ignore
|
||||
router.register("auth", views.ChangePasswordView, basename="change-password") # type: ignore
|
||||
|
||||
urlpatterns = [ # type: ignore
|
||||
path("", include(router.urls)), # type: ignore
|
||||
@@ -32,4 +24,14 @@ urlpatterns = [ # type: ignore
|
||||
jwt_views.TokenRefreshView.as_view(),
|
||||
name="token_refresh",
|
||||
),
|
||||
path(
|
||||
r"users/<uuid:pk>/companies",
|
||||
views.UserCompanyApiView.as_view(),
|
||||
name="user-company-api-view"
|
||||
),
|
||||
path(
|
||||
r"me/companies",
|
||||
views.MeCompanyApiView.as_view(),
|
||||
name="me-company-api-view"
|
||||
)
|
||||
]
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
from rest_framework.viewsets import GenericViewSet # type: ignore
|
||||
from rest_framework.generics import GenericAPIView # type: ignore
|
||||
from rest_framework.decorators import action # type: ignore
|
||||
from rest_framework import status # type: ignore
|
||||
from rest_framework.request import HttpRequest # type: ignore
|
||||
@@ -7,8 +7,7 @@ from rest_framework.permissions import ( # type: ignore
|
||||
IsAuthenticated
|
||||
)
|
||||
|
||||
from django_core.mixins import BaseViewSetMixin # type: ignore
|
||||
|
||||
from core.utils.views import BaseApiViewMixin
|
||||
from core.apps.companies.serializers import (
|
||||
RetrieveCompanySerializer,
|
||||
CreateCompanySerializer,
|
||||
@@ -21,50 +20,29 @@ from core.apps.companies.models import (
|
||||
from django.db import transaction
|
||||
|
||||
|
||||
class MeCompanyView(BaseViewSetMixin, GenericViewSet):
|
||||
######################################################################
|
||||
# @api-view | POST, GET - me/companies
|
||||
######################################################################
|
||||
class MeCompanyApiView(BaseApiViewMixin, GenericAPIView): # type: ignore
|
||||
permission_classes = [IsAuthenticated]
|
||||
|
||||
action_permission_classes = {}
|
||||
action_serializer_class = {
|
||||
"create": CreateCompanySerializer,
|
||||
"list": RetrieveCompanySerializer,
|
||||
method_permission_classes = {}
|
||||
method_serializer_class = {
|
||||
"post": CreateCompanySerializer,
|
||||
"get": RetrieveCompanySerializer,
|
||||
}
|
||||
|
||||
def list(
|
||||
self,
|
||||
request: HttpRequest,
|
||||
*args: object,
|
||||
**kwargs: object
|
||||
) -> Response:
|
||||
|
||||
companies = CompanyModel.objects.filter(
|
||||
accounts__user=request.user
|
||||
)
|
||||
|
||||
return Response(
|
||||
RetrieveCompanySerializer(instance=companies, many=True).data,
|
||||
status=status.HTTP_200_OK
|
||||
)
|
||||
def get(self, request: HttpRequest, *args: object, **kwargs: object) -> Response:
|
||||
companies = CompanyModel.objects.filter(accounts__user=request.user)
|
||||
ser = RetrieveCompanySerializer(instance=companies, many=True)
|
||||
return Response(ser.data, status.HTTP_200_OK)
|
||||
|
||||
def create(
|
||||
self,
|
||||
request: HttpRequest,
|
||||
*args: object,
|
||||
**kwargs: object
|
||||
) -> Response:
|
||||
|
||||
def create(self, request: HttpRequest, *args: object, **kwargs: object) -> Response:
|
||||
with transaction.atomic():
|
||||
serializer = CreateCompanySerializer(data=request.data) # type: ignore
|
||||
serializer.is_valid(raise_exception=True)
|
||||
company = serializer.save() # type: ignore
|
||||
|
||||
account = CompanyAccountModel(
|
||||
company=company,
|
||||
user=request.user
|
||||
)
|
||||
account = CompanyAccountModel(company=company, user=request.user)
|
||||
account.save()
|
||||
|
||||
return Response(
|
||||
data=serializer.data,
|
||||
status=status.HTTP_201_CREATED
|
||||
)
|
||||
return Response(serializer.data, status.HTTP_201_CREATED)
|
||||
@@ -2,24 +2,25 @@ import uuid
|
||||
|
||||
from drf_spectacular.utils import extend_schema
|
||||
|
||||
from rest_framework.viewsets import GenericViewSet # type: ignore
|
||||
from rest_framework.generics import GenericAPIView # type: ignore
|
||||
from rest_framework.decorators import action # type: ignore
|
||||
from rest_framework.permissions import IsAdminUser
|
||||
from rest_framework import status # type: ignore
|
||||
from rest_framework.request import HttpRequest # type: ignore
|
||||
from rest_framework.response import Response # type: ignore
|
||||
from rest_framework.permissions import ( # type: ignore
|
||||
IsAdminUser,
|
||||
)
|
||||
from django_core.mixins import BaseViewSetMixin
|
||||
|
||||
from rest_framework.generics import get_object_or_404 # type: ignore
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.db import transaction
|
||||
|
||||
|
||||
from core.utils.views import BaseApiViewMixin
|
||||
from core.apps.companies.serializers import (
|
||||
CreateCompanySerializer,
|
||||
RetrieveCompanySerializer
|
||||
RetrieveCompanySerializer,
|
||||
BaseCompanySerializer,
|
||||
)
|
||||
from core.apps.companies.models import (
|
||||
CompanyModel,
|
||||
@@ -29,47 +30,53 @@ from core.apps.companies.models import (
|
||||
UserModel = get_user_model()
|
||||
|
||||
|
||||
class UserCompaniesView(BaseViewSetMixin, GenericViewSet):
|
||||
######################################################################
|
||||
# /users/{id}/companies
|
||||
######################################################################
|
||||
@extend_schema(tags=["User Companies"])
|
||||
class UserCompanyApiView(BaseApiViewMixin, GenericAPIView): # type: ignore
|
||||
queryset = UserModel.objects.all()
|
||||
permission_classes = [IsAdminUser]
|
||||
serializer_class = BaseCompanySerializer
|
||||
|
||||
action_permission_classes = {}
|
||||
action_permission_classes = {
|
||||
"list_company": RetrieveCompanySerializer,
|
||||
"create_company": CreateCompanySerializer,
|
||||
method_permission_classes = {
|
||||
"get": [IsAdminUser],
|
||||
"post": [IsAdminUser],
|
||||
}
|
||||
method_serializer_class = {
|
||||
"get": RetrieveCompanySerializer,
|
||||
"post": CreateCompanySerializer,
|
||||
}
|
||||
|
||||
@extend_schema(
|
||||
summary="Get list of companies",
|
||||
description="Get list of companies",
|
||||
summary="Get List Of Companies For User",
|
||||
description="Get List Of Companies For User",
|
||||
)
|
||||
@action(url_path="companies", detail=True, methods=["GET"])
|
||||
def list_company(
|
||||
def get(
|
||||
self,
|
||||
request: HttpRequest,
|
||||
pk: uuid.UUID,
|
||||
*args: object,
|
||||
**kwargs: object,
|
||||
) -> Response:
|
||||
|
||||
companies = CompanyModel.objects.filter(accounts__user__pk=pk)
|
||||
return Response(
|
||||
data=RetrieveCompanySerializer(instance=companies, many=True),
|
||||
status=status.HTTP_200_OK
|
||||
)
|
||||
|
||||
|
||||
|
||||
@extend_schema(
|
||||
summary="Create Company",
|
||||
description="Create Company",
|
||||
summary="Create Company For User",
|
||||
description="Create Company For User",
|
||||
)
|
||||
@action(url_path="companies", detail=True, methods=["POST"])
|
||||
def create_company(
|
||||
def post(
|
||||
self,
|
||||
request: HttpRequest,
|
||||
pk: uuid.UUID,
|
||||
*args: object,
|
||||
**kwargs: object,
|
||||
) -> Response:
|
||||
|
||||
with transaction.atomic():
|
||||
ser = CreateCompanySerializer(data=request.data) # type: ignore
|
||||
ser.is_valid(raise_exception=True)
|
||||
@@ -81,4 +88,3 @@ class UserCompaniesView(BaseViewSetMixin, GenericViewSet):
|
||||
account.save()
|
||||
|
||||
return Response(data=ser.data, status=status.HTTP_201_CREATED)
|
||||
|
||||
Reference in New Issue
Block a user