-
Notifications
You must be signed in to change notification settings - Fork 697
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Simplify permission handling in views and permissions logic.
Refactored `get_permissions` in `cinema/views.py` to streamline behavior and rely more on `super()`. Updated comments in `user/permissions.py` for clarity and removed redundant code, improving maintainability and readability.
- Loading branch information
Showing
2 changed files
with
27 additions
and
18 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
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,24 +1,36 @@ | ||
from rest_framework.permissions import SAFE_METHODS, BasePermission | ||
|
||
|
||
# class IsAdminOrIfAuthenticatedReadOnly(BasePermission): | ||
# def has_permission(self, request, view): | ||
# # If the user is an admin, we allow everything. | ||
# if request.user and request.user.is_staff: | ||
# return True | ||
# | ||
# # If the request is "read" (GET/HEAD/OPTIONS), | ||
# # then we allow authenticated | ||
# if request.method in SAFE_METHODS: | ||
# return bool(request.user and request.user.is_authenticated) | ||
# | ||
# # Allow POST to regular authenticated users | ||
# if ( | ||
# request.method == "POST" | ||
# and request.user | ||
# and request.user.is_authenticated | ||
# ): | ||
# return True | ||
# | ||
# # Everything else (POST, PUT, PATCH, DELETE) is prohibited | ||
# return False | ||
class IsAdminOrIfAuthenticatedReadOnly(BasePermission): | ||
def has_permission(self, request, view): | ||
# If the user is an admin, we allow everything. | ||
# Разрешаем всё админам | ||
if request.user and request.user.is_staff: | ||
return True | ||
|
||
# If the request is "read" (GET/HEAD/OPTIONS), | ||
# then we allow authenticated | ||
# Разрешаем GET/HEAD/OPTIONS аутентифицированным | ||
if request.method in SAFE_METHODS: | ||
return bool(request.user and request.user.is_authenticated) | ||
|
||
# Allow POST to regular authenticated users | ||
if ( | ||
request.method == "POST" | ||
and request.user | ||
and request.user.is_authenticated | ||
): | ||
return True | ||
|
||
# Everything else (POST, PUT, PATCH, DELETE) is prohibited | ||
# Иначе (POST/PUT/PATCH/DELETE) — запрещаем | ||
return False |