schon/core/utils/db.py
Egor fureunoir Gorbunov 9898212855 Features: 1) Introduced LoggingError exception for invalid log level handling in log method; 2) Updated logging framework to dynamically initialize loggers using __name__ instead of hardcoded "django".
Fixes: 1) Fixed missing `exc_info` flag in critical and error logs to provide richer error context; 2) Addressed redundant code and unused imports in the logging logic for cleaner execution.

Extra: Refactored `LOGGING` configuration for improved readability and runtime adaptability; optimized related log-handling logic throughout the codebase.
2025-11-04 14:40:50 +03:00

41 lines
1.1 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)
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