mirror of
https://github.com/bookwyrm-social/bookwyrm.git
synced 2024-11-25 02:51:13 +00:00
commit
4158c0fafa
4 changed files with 20 additions and 14 deletions
|
@ -1,4 +1,5 @@
|
||||||
""" template filters """
|
""" template filters """
|
||||||
|
import datetime
|
||||||
from typing import Any, Optional
|
from typing import Any, Optional
|
||||||
from dateutil.relativedelta import relativedelta
|
from dateutil.relativedelta import relativedelta
|
||||||
from django import template
|
from django import template
|
||||||
|
@ -34,11 +35,13 @@ def get_replies(status: models.Status) -> Any:
|
||||||
@register.filter(name="parent")
|
@register.filter(name="parent")
|
||||||
def get_parent(status: models.Status) -> Any:
|
def get_parent(status: models.Status) -> Any:
|
||||||
"""get the reply parent for a status"""
|
"""get the reply parent for a status"""
|
||||||
return (
|
if status.reply_parent_id:
|
||||||
models.Status.objects.filter(id=status.reply_parent_id)
|
return (
|
||||||
.select_subclasses()
|
models.Status.objects.filter(id=status.reply_parent_id)
|
||||||
.first()
|
.select_subclasses()
|
||||||
)
|
.first()
|
||||||
|
)
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
@register.filter(name="boosted_status")
|
@register.filter(name="boosted_status")
|
||||||
|
@ -53,7 +56,7 @@ def get_boosted(boost: models.Boost) -> Any:
|
||||||
|
|
||||||
|
|
||||||
@register.filter(name="published_date")
|
@register.filter(name="published_date")
|
||||||
def get_published_date(date: str) -> Any:
|
def get_published_date(date: datetime.datetime) -> str | None:
|
||||||
"""less verbose combo of humanize filters"""
|
"""less verbose combo of humanize filters"""
|
||||||
if not date:
|
if not date:
|
||||||
return ""
|
return ""
|
||||||
|
|
|
@ -93,7 +93,7 @@ def get_book_cover_thumbnail(
|
||||||
|
|
||||||
|
|
||||||
@register.filter(name="get_isni_bio")
|
@register.filter(name="get_isni_bio")
|
||||||
def get_isni_bio(existing: int, author: Author) -> str:
|
def get_isni_bio(existing: list[str], author: Author) -> str:
|
||||||
"""Returns the isni bio string if an existing author has an isni listed"""
|
"""Returns the isni bio string if an existing author has an isni listed"""
|
||||||
auth_isni = re.sub(r"\D", "", str(author.isni))
|
auth_isni = re.sub(r"\D", "", str(author.isni))
|
||||||
if len(existing) == 0:
|
if len(existing) == 0:
|
||||||
|
|
|
@ -1,17 +1,20 @@
|
||||||
""" Custom handler for caching """
|
""" Custom handler for caching """
|
||||||
from typing import Any, Callable, Tuple, Union
|
from typing import Callable, Optional, ParamSpec, TypeVar, cast
|
||||||
|
|
||||||
from django.core.cache import cache
|
from django.core.cache import cache
|
||||||
|
|
||||||
|
Args = ParamSpec("Args")
|
||||||
|
Ret = TypeVar("Ret")
|
||||||
|
|
||||||
|
|
||||||
def get_or_set(
|
def get_or_set(
|
||||||
cache_key: str,
|
cache_key: str,
|
||||||
function: Callable[..., Any],
|
function: Callable[Args, Ret],
|
||||||
*args: Tuple[Any, ...],
|
*args: Args.args,
|
||||||
timeout: Union[float, None] = None
|
timeout: Optional[float] = None
|
||||||
) -> Any:
|
) -> Ret:
|
||||||
"""Django's built-in get_or_set isn't cutting it"""
|
"""Django's built-in get_or_set isn't cutting it"""
|
||||||
value = cache.get(cache_key)
|
value = cast(Optional[Ret], cache.get(cache_key))
|
||||||
if value is None:
|
if value is None:
|
||||||
value = function(*args)
|
value = function(*args)
|
||||||
cache.set(cache_key, value, timeout=timeout)
|
cache.set(cache_key, value, timeout=timeout)
|
||||||
|
|
2
mypy.ini
2
mypy.ini
|
@ -25,4 +25,4 @@ ignore_errors = False
|
||||||
[mypy-bookwyrm.templatetags.*]
|
[mypy-bookwyrm.templatetags.*]
|
||||||
ignore_errors = False
|
ignore_errors = False
|
||||||
allow_untyped_calls = True
|
allow_untyped_calls = True
|
||||||
disable_error_code = attr-defined, arg-type, misc
|
disable_error_code = attr-defined
|
||||||
|
|
Loading…
Reference in a new issue