Skip to content

Commit

Permalink
fix: Use TypeVar bound to Model for type-hinting of the user paramete…
Browse files Browse the repository at this point in the history
…r in cache module (django-cms#1495)
  • Loading branch information
payamnj committed Sep 15, 2024
1 parent 44255c8 commit 55c18f4
Showing 1 changed file with 18 additions and 10 deletions.
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

0 comments on commit 55c18f4

Please sign in to comment.