mirror of
https://github.com/SectorLabs/django-localized-fields.git
synced 2025-10-29 18:18:57 +03:00
Support for slugging from multiple fields
This commit is contained in:
@@ -1,12 +1,14 @@
|
||||
from typing import Callable, Tuple
|
||||
from typing import Callable, Tuple, Union
|
||||
from datetime import datetime
|
||||
|
||||
from django import forms
|
||||
from django.conf import settings
|
||||
from django.utils import translation
|
||||
from django.utils.text import slugify
|
||||
|
||||
from .field import LocalizedField
|
||||
from ..value import LocalizedValue
|
||||
from ..util import resolve_object_property
|
||||
|
||||
|
||||
class LocalizedAutoSlugField(LocalizedField):
|
||||
@@ -147,7 +149,7 @@ class LocalizedAutoSlugField(LocalizedField):
|
||||
]
|
||||
|
||||
@staticmethod
|
||||
def _get_populate_from_value(instance, field_name: str, language: str):
|
||||
def _get_populate_from_value(instance, field_name: Union[str, Tuple[str]], language: str):
|
||||
"""Gets the value to create a slug from in the specified language.
|
||||
|
||||
Arguments:
|
||||
@@ -164,5 +166,17 @@ class LocalizedAutoSlugField(LocalizedField):
|
||||
The text to generate a slug for.
|
||||
"""
|
||||
|
||||
value = getattr(instance, field_name, None)
|
||||
return value.get(language)
|
||||
def get_field_value(name):
|
||||
value = resolve_object_property(instance, name)
|
||||
with translation.override(language):
|
||||
return str(value)
|
||||
|
||||
if isinstance(field_name, tuple) or isinstance(field_name, list):
|
||||
value = '-'.join([
|
||||
value
|
||||
for value in [get_field_value(name) for name in field_name]
|
||||
if value
|
||||
])
|
||||
return value
|
||||
|
||||
return get_field_value(field_name)
|
||||
|
||||
@@ -19,3 +19,26 @@ def get_language_codes() -> List[str]:
|
||||
lang_code
|
||||
for lang_code, _ in settings.LANGUAGES
|
||||
]
|
||||
|
||||
|
||||
def resolve_object_property(obj, path: str):
|
||||
"""Resolves the value of a property on an object.
|
||||
|
||||
Is able to resolve nested properties. For example,
|
||||
a path can be specified:
|
||||
|
||||
'other.beer.name'
|
||||
|
||||
Raises:
|
||||
AttributeError:
|
||||
In case the property could not be resolved.
|
||||
|
||||
Returns:
|
||||
The value of the specified property.
|
||||
"""
|
||||
|
||||
value = obj
|
||||
for path_part in path.split('.'):
|
||||
value = getattr(value, path_part)
|
||||
|
||||
return value
|
||||
|
||||
Reference in New Issue
Block a user