control one permission with same name for each page
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
from rest_framework_simplejwt.authentication import JWTAuthentication
|
||||
from rest_framework.permissions import AllowAny, IsAuthenticated
|
||||
from apps.core.exceptions import ConflictException
|
||||
from rest_framework.exceptions import APIException
|
||||
from apps.authorization.api.v1.serializers import (
|
||||
RoleSerializer,
|
||||
@@ -64,6 +65,16 @@ class PermissionViewSet(viewsets.ModelViewSet):
|
||||
filter_backends = [filters.SearchFilter]
|
||||
search_fields = ['page__name', ]
|
||||
|
||||
def create(self, request, *args, **kwargs):
|
||||
if self.queryset.filter(name=request.data['name'], page_id=request.data['page']).exists():
|
||||
raise ConflictException('a permission with this page exists.')
|
||||
serializer = self.serializer_class(data=request.data)
|
||||
if serializer.is_valid():
|
||||
serializer.save()
|
||||
return Response(serializer.data, status=status.HTTP_200_OK)
|
||||
else:
|
||||
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
|
||||
class UserRelationViewSet(viewsets.ModelViewSet):
|
||||
""" Crud Operations for User Relations """
|
||||
|
||||
18
apps/authorization/migrations/0017_alter_permissions_name.py
Normal file
18
apps/authorization/migrations/0017_alter_permissions_name.py
Normal file
@@ -0,0 +1,18 @@
|
||||
# Generated by Django 5.0 on 2025-06-08 11:10
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('authorization', '0016_alter_permissions_name'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name='permissions',
|
||||
name='name',
|
||||
field=models.CharField(max_length=50),
|
||||
),
|
||||
]
|
||||
@@ -21,7 +21,7 @@ class Page(BaseModel):
|
||||
class Permissions(BaseModel):
|
||||
""" permission level of users """
|
||||
|
||||
name = models.CharField(max_length=50, unique=True)
|
||||
name = models.CharField(max_length=50)
|
||||
description = models.TextField(max_length=500)
|
||||
category_choices = (
|
||||
('api', 'Api'),
|
||||
|
||||
@@ -4,3 +4,12 @@
|
||||
you can check out the Django Rest Framework documentation at:
|
||||
https://www.django-rest-framework.org/api-guide/exceptions/#custom-exception-handling
|
||||
"""
|
||||
|
||||
from rest_framework.exceptions import APIException
|
||||
from rest_framework import status
|
||||
|
||||
|
||||
class ConflictException(APIException):
|
||||
status_code = status.HTTP_409_CONFLICT
|
||||
default_detail = "Object already exists."
|
||||
default_code = "conflict"
|
||||
|
||||
Reference in New Issue
Block a user