-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
♻️ [#390] Refactor endpoint to use new permissions
- Loading branch information
1 parent
ba0f3b7
commit 29c6885
Showing
6 changed files
with
70 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,49 @@ | ||
from django.utils.translation import gettext_lazy as _ | ||
|
||
from drf_spectacular.utils import extend_schema_field | ||
from rest_framework import serializers | ||
|
||
from ..models import User | ||
|
||
|
||
class RoleSerializer(serializers.Serializer): | ||
can_start_destruction = serializers.BooleanField() | ||
can_review_destruction = serializers.BooleanField() | ||
can_review_final_list = serializers.BooleanField() | ||
|
||
class Meta: | ||
fields = ( | ||
"name", | ||
"can_start_destruction", | ||
"can_review_destruction", | ||
"can_review_final_list", | ||
"can_view_case_details", | ||
) | ||
|
||
|
||
class UserSerializer(serializers.ModelSerializer): | ||
role = RoleSerializer() | ||
role = serializers.SerializerMethodField( | ||
help_text=_("The role of the user within the application logic."), | ||
allow_null=True, | ||
) | ||
|
||
class Meta: | ||
model = User | ||
fields = ("pk", "username", "first_name", "last_name", "email", "role") | ||
|
||
@extend_schema_field(RoleSerializer) | ||
def get_role(self, user: User) -> dict | None: | ||
serializer = RoleSerializer( | ||
data={ | ||
"can_review_destruction": user.has_perm( | ||
"accounts.can_review_destruction" | ||
), | ||
"can_start_destruction": user.has_perm( | ||
"accounts.can_start_destruction" | ||
), | ||
"can_review_final_list": user.has_perm( | ||
"accounts.can_review_final_list" | ||
), | ||
} | ||
) | ||
serializer.is_valid() | ||
|
||
return serializer.data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters