schon/engine/core/utils/db.py
Egor fureunoir Gorbunov dc7f8be926 Features: 1) None;
Fixes: 1) Add `# ty: ignore` comments to suppress type errors in multiple files; 2) Correct method argument annotations and definitions to align with type hints; 3) Fix cases of invalid or missing imports and unresolved attributes;

Extra: Refactor method definitions to use tuple-based method declarations; replace custom type aliases with `Any`; improve caching utility and error handling logic in utility scripts.
2025-12-19 16:43:39 +03:00

43 lines
1.2 KiB
Python

import logging
from typing import Any, Callable
from django.db.models import Model
from django.db.models.constants import LOOKUP_SEP
from django_extensions.db.fields import AutoSlugField
from slugify import slugify
logger = logging.getLogger(__name__)
def unicode_slugify_function(content: Any) -> str:
return slugify(
text=str(content),
allow_unicode=True,
max_length=88,
word_boundary=True,
save_order=True,
regex_pattern=r"(?:[^\w-]|_)+",
separator="-",
)
class TweakedAutoSlugField(AutoSlugField):
def get_slug_fields(
self, model_instance: Model, lookup_value: str | Callable[[Any], Any]
) -> str | Model:
if callable(lookup_value):
return f"{lookup_value(model_instance)}"
lookup_value_path = lookup_value.split(LOOKUP_SEP) # ty: ignore[possibly-missing-attribute]
attr = model_instance
for elem in lookup_value_path:
try:
attr: Any = getattr(attr, elem)
except AttributeError:
attr: Any = ""
if callable(attr):
# noinspection PyCallingNonCallable
return f"{attr()}"
return attr