-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add typing support to
get_solo
(#128)
* Add typing support to `get_solo` * Deprecate `get_cache` * Update changelog
- Loading branch information
Showing
11 changed files
with
98 additions
and
53 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,5 +1,6 @@ | ||
include *.md | ||
include LICENSE | ||
include CHANGES | ||
include solo/py.typed | ||
recursive-include solo/templates * | ||
recursive-include solo/locale *.mo *.po |
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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
[tool.mypy] | ||
ignore_missing_imports = true | ||
strict = true | ||
exclude = "solo/tests" |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,81 @@ | ||
from __future__ import annotations | ||
|
||
import sys | ||
import warnings | ||
from typing import Any | ||
|
||
from django.conf import settings | ||
from django.core.cache import BaseCache, caches | ||
from django.db import models | ||
|
||
try: | ||
from django.core.cache import caches | ||
get_cache = lambda cache_name: caches[cache_name] | ||
except ImportError: | ||
from django.core.cache import get_cache | ||
|
||
from solo import settings as solo_settings | ||
|
||
if sys.version_info >= (3, 11): | ||
from typing import Self | ||
else: | ||
from typing_extensions import Self | ||
|
||
|
||
DEFAULT_SINGLETON_INSTANCE_ID = 1 | ||
|
||
|
||
def get_cache(cache_name: str) -> BaseCache: | ||
warnings.warn( | ||
"'get_cache' is deprecated and will be removed in django-solo 2.4.0. " | ||
"Instead, use 'caches' from 'django.core.cache'.", | ||
DeprecationWarning, | ||
stacklevel=2, | ||
) | ||
return caches[cache_name] # type: ignore[no-any-return] # mypy bug, unable to get a MRE | ||
|
||
|
||
class SingletonModel(models.Model): | ||
singleton_instance_id = DEFAULT_SINGLETON_INSTANCE_ID | ||
|
||
class Meta: | ||
abstract = True | ||
|
||
def save(self, *args, **kwargs): | ||
def save(self, *args: Any, **kwargs: Any) -> None: | ||
self.pk = self.singleton_instance_id | ||
super(SingletonModel, self).save(*args, **kwargs) | ||
super().save(*args, **kwargs) | ||
self.set_to_cache() | ||
|
||
def delete(self, *args, **kwargs): | ||
def delete(self, *args: Any, **kwargs: Any) -> tuple[int, dict[str, int]]: | ||
self.clear_cache() | ||
super(SingletonModel, self).delete(*args, **kwargs) | ||
return super().delete(*args, **kwargs) | ||
|
||
@classmethod | ||
def clear_cache(cls): | ||
def clear_cache(cls) -> None: | ||
cache_name = getattr(settings, 'SOLO_CACHE', solo_settings.SOLO_CACHE) | ||
if cache_name: | ||
cache = get_cache(cache_name) | ||
cache = caches[cache_name] | ||
cache_key = cls.get_cache_key() | ||
cache.delete(cache_key) | ||
|
||
def set_to_cache(self): | ||
def set_to_cache(self) -> None: | ||
cache_name = getattr(settings, 'SOLO_CACHE', solo_settings.SOLO_CACHE) | ||
if not cache_name: | ||
return None | ||
cache = get_cache(cache_name) | ||
cache = caches[cache_name] | ||
cache_key = self.get_cache_key() | ||
timeout = getattr(settings, 'SOLO_CACHE_TIMEOUT', solo_settings.SOLO_CACHE_TIMEOUT) | ||
cache.set(cache_key, self, timeout) | ||
|
||
@classmethod | ||
def get_cache_key(cls): | ||
def get_cache_key(cls) -> str: | ||
prefix = getattr(settings, 'SOLO_CACHE_PREFIX', solo_settings.SOLO_CACHE_PREFIX) | ||
return '%s:%s' % (prefix, cls.__name__.lower()) | ||
|
||
@classmethod | ||
def get_solo(cls): | ||
def get_solo(cls) -> Self: | ||
cache_name = getattr(settings, 'SOLO_CACHE', solo_settings.SOLO_CACHE) | ||
if not cache_name: | ||
obj, created = cls.objects.get_or_create(pk=cls.singleton_instance_id) | ||
return obj | ||
cache = get_cache(cache_name) | ||
obj, _ = cls.objects.get_or_create(pk=cls.singleton_instance_id) | ||
return obj # type: ignore[return-value] | ||
cache = caches[cache_name] | ||
cache_key = cls.get_cache_key() | ||
obj = cache.get(cache_key) | ||
if not obj: | ||
obj, created = cls.objects.get_or_create(pk=cls.singleton_instance_id) | ||
obj, _ = cls.objects.get_or_create(pk=cls.singleton_instance_id) | ||
obj.set_to_cache() | ||
return obj | ||
return obj # type: ignore[return-value] |
Empty file.
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,17 +1,21 @@ | ||
from __future__ import annotations | ||
|
||
from django.conf import settings | ||
|
||
# template parameters | ||
GET_SOLO_TEMPLATE_TAG_NAME = getattr(settings, | ||
'GET_SOLO_TEMPLATE_TAG_NAME', 'get_solo') | ||
GET_SOLO_TEMPLATE_TAG_NAME: str = getattr( | ||
settings, 'GET_SOLO_TEMPLATE_TAG_NAME', 'get_solo' | ||
) | ||
|
||
SOLO_ADMIN_SKIP_OBJECT_LIST_PAGE = getattr(settings, | ||
'SOLO_ADMIN_SKIP_OBJECT_LIST_PAGE', True) | ||
SOLO_ADMIN_SKIP_OBJECT_LIST_PAGE: bool = getattr( | ||
settings, 'SOLO_ADMIN_SKIP_OBJECT_LIST_PAGE', True | ||
) | ||
|
||
# The cache that should be used, e.g. 'default'. Refers to Django CACHES setting. | ||
# Set to None to disable caching. | ||
SOLO_CACHE = None | ||
SOLO_CACHE: str | None = None | ||
|
||
SOLO_CACHE_TIMEOUT = 60*5 | ||
SOLO_CACHE_TIMEOUT = 60 * 5 | ||
|
||
SOLO_CACHE_PREFIX = 'solo' | ||
DEFAULT_AUTO_FIELD = 'django.db.models.AutoField' |
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