Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Use TypeVar for type-hinting of the user parameter #1496

Merged
merged 1 commit into from
Sep 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 18 additions & 10 deletions filer/cache.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import typing

from django.contrib.auth import get_user_model
from django.db.models import Model
from django.core.cache import cache


User = get_user_model()
UserModel = typing.TypeVar('UserModel', bound=Model)


def get_folder_perm_cache_key(user: User, permission: str) -> str:
def get_folder_perm_cache_key(user: UserModel, permission: str) -> str:
"""
Generates a unique cache key for a given user and permission.

Expand All @@ -18,7 +18,9 @@ def get_folder_perm_cache_key(user: User, permission: str) -> str:
for far more than 1,000 admin users to make the cached unit require less memory.

Parameters:
user (User): The user for whom the cache key is being generated.
user (UserModel): The user for whom the cache key is being generated.
The `user` can be an instance of the default `django.contrib.auth.models.User`
or any custom user model specified by `AUTH_USER_MODEL` in the settings.
permission (str): The permission for which the cache key is being generated.

Returns:
Expand All @@ -27,15 +29,17 @@ def get_folder_perm_cache_key(user: User, permission: str) -> str:
return f"filer:perm:{permission}"


def get_folder_permission_cache(user: User, permission: str) -> typing.Optional[dict]:
def get_folder_permission_cache(user: UserModel, permission: str) -> typing.Optional[dict]:
"""
Retrieves the cached folder permissions for a given user and permission.

If the cache value exists, it returns the permissions for the user.
If the cache value does not exist, it returns None.

Parameters:
user (User): The user for whom the permissions are being retrieved.
user (UserModel): The user for whom the permissions are being retrieved.
The `user` can be an instance of the default `django.contrib.auth.models.User`
or any custom user model specified by `AUTH_USER_MODEL` in the settings.
permission (str): The permission for which the permissions are being retrieved.

Returns:
Expand All @@ -47,15 +51,17 @@ def get_folder_permission_cache(user: User, permission: str) -> typing.Optional[
return None


def clear_folder_permission_cache(user: User, permission: typing.Optional[str] = None) -> None:
def clear_folder_permission_cache(user: UserModel, permission: typing.Optional[str] = None) -> None:
"""
Clears the cached folder permissions for a given user.

If a specific permission is provided, it clears the cache for that permission only.
If no specific permission is provided, it clears the cache for all permissions.

Parameters:
user (User): The user for whom the permissions are being cleared.
user (UserModel): The user for whom the permissions are being cleared.
The `user` can be an instance of the default `django.contrib.auth.models.User`
or any custom user model specified by `AUTH_USER_MODEL` in the settings.
permission (str, optional): The specific permission to clear. Defaults to None.
"""
if permission is None:
Expand All @@ -65,7 +71,7 @@ def clear_folder_permission_cache(user: User, permission: typing.Optional[str] =
cache.delete(get_folder_perm_cache_key(user, permission))


def update_folder_permission_cache(user: User, permission: str, id_list: typing.List[int]) -> None:
def update_folder_permission_cache(user: UserModel, permission: str, id_list: typing.List[int]) -> None:
"""
Updates the cached folder permissions for a given user and permission.

Expand All @@ -74,7 +80,9 @@ def update_folder_permission_cache(user: User, permission: str, id_list: typing.
Finally, it sets the updated permissions back into the cache.

Parameters:
user (User): The user for whom the permissions are being updated.
user (UserModel): The user for whom the permissions are being updated.
The `user` can be an instance of the default `django.contrib.auth.models.User`
or any custom user model specified by `AUTH_USER_MODEL` in the settings.
permission (str): The permission to update.
id_list (list): The list of IDs to set as the new permissions.
"""
Expand Down
Loading