From 4c7a332eac61c98e71624ee4619bb538d10cd0db Mon Sep 17 00:00:00 2001 From: mohsentaba Date: Tue, 27 Oct 2026 13:20:13 +0330 Subject: [PATCH] feat(hadis): enrich reference edition metadata and fix Accept-Language priority in hadis serializers --- apps/hadis/serializers/hadis.py | 90 +++++++++++++++++++++++++++++++-- 1 file changed, 85 insertions(+), 5 deletions(-) diff --git a/apps/hadis/serializers/hadis.py b/apps/hadis/serializers/hadis.py index 4f22a3d..abbebc0 100644 --- a/apps/hadis/serializers/hadis.py +++ b/apps/hadis/serializers/hadis.py @@ -24,12 +24,12 @@ def get_request_language_code(request=None, fallback="ru") -> str: query_params = getattr(request, "query_params", None) or getattr(request, "GET", None) if query_params: lang = query_params.get("language_code") or query_params.get("lang") - if not lang: - lang = getattr(request, "LANGUAGE_CODE", None) if not lang and hasattr(request, "headers"): lang = request.headers.get("Accept-Language") - elif not lang and hasattr(request, "META"): + if not lang and hasattr(request, "META"): lang = request.META.get("HTTP_ACCEPT_LANGUAGE") + if not lang: + lang = getattr(request, "LANGUAGE_CODE", None) if lang: lang = lang.split(",")[0].split(";")[0].split("-")[0].strip().lower() if not lang: @@ -1365,13 +1365,53 @@ class HadisReferenceSerializer(serializers.ModelSerializer): pub_src = edition.publisher_item.title if (getattr(edition, "publisher_item_id", None) and edition.publisher_item and edition.publisher_item.title) else edition.publisher pub = get_localized_text(pub_src, request) if isinstance(pub_src, list) else pub_src ed_title = get_localized_text(edition.title, request) if isinstance(getattr(edition, "title", None), list) else getattr(edition, "title", None) + city_src = ( + edition.city_of_publication_city.title + if (getattr(edition, "city_of_publication_city_id", None) and edition.city_of_publication_city and edition.city_of_publication_city.title) + else getattr(edition, "city_of_publication", None) + ) + country_src = ( + edition.country_of_publication_country.title + if (getattr(edition, "country_of_publication_country_id", None) and edition.country_of_publication_country and edition.country_of_publication_country.title) + else getattr(edition, "country_of_publication", None) + ) + city = get_localized_text(city_src, request) if city_src else "" + country = get_localized_text(country_src, request) if country_src else "" + vol_count = edition.volumes.count() if hasattr(edition, "volumes") else 0 + num_vols = vol_count if vol_count > 0 else edition.number_of_volumes + + def _serialize_contributor(c): + name = get_localized_text(c.name, request) if isinstance(getattr(c, "name", None), list) else (getattr(c, "name", None) or "") + author_obj = getattr(c, "author", None) + author_data = None + if author_obj: + author_data = { + "id": author_obj.id, + "slug": author_obj.slug, + "name": get_localized_text(author_obj.name, request) if isinstance(author_obj.name, list) else (author_obj.name or ""), + } + return { + "id": c.id, + "name": name, + "slug": author_obj.slug if (author_obj and author_obj.slug) else getattr(c, "slug", None), + "author_id": getattr(c, "author_id", None), + "author": author_data, + } + + researchers = [_serialize_contributor(r) for r in edition.researchers.all()] if hasattr(edition, "researchers") else [] + editors = [_serialize_contributor(e) for e in edition.editors.all()] if hasattr(edition, "editors") else [] + return { "id": edition.id, "title": ed_title or "", "edition_number": edition.edition_number, "publisher": pub or "", + "publication_place": {"city": city or "", "country": country or ""}, "year_of_publication": edition.year_of_publication or "", - "number_of_volumes": edition.number_of_volumes, + "number_of_volumes": num_vols, + "source_url": getattr(edition, "source_url", None) or "", + "researchers": researchers, + "editors": editors, } return None @@ -1927,13 +1967,53 @@ class BaseDetailedReferenceSerializer(serializers.ModelSerializer): pub_src = edition.publisher_item.title if (getattr(edition, "publisher_item_id", None) and edition.publisher_item and edition.publisher_item.title) else edition.publisher pub = get_localized_text(pub_src, request) if isinstance(pub_src, list) else pub_src ed_title = get_localized_text(edition.title, request) if isinstance(getattr(edition, "title", None), list) else getattr(edition, "title", None) + city_src = ( + edition.city_of_publication_city.title + if (getattr(edition, "city_of_publication_city_id", None) and edition.city_of_publication_city and edition.city_of_publication_city.title) + else getattr(edition, "city_of_publication", None) + ) + country_src = ( + edition.country_of_publication_country.title + if (getattr(edition, "country_of_publication_country_id", None) and edition.country_of_publication_country and edition.country_of_publication_country.title) + else getattr(edition, "country_of_publication", None) + ) + city = get_localized_text(city_src, request) if city_src else "" + country = get_localized_text(country_src, request) if country_src else "" + vol_count = edition.volumes.count() if hasattr(edition, "volumes") else 0 + num_vols = vol_count if vol_count > 0 else edition.number_of_volumes + + def _serialize_contributor(c): + name = get_localized_text(c.name, request) if isinstance(getattr(c, "name", None), list) else (getattr(c, "name", None) or "") + author_obj = getattr(c, "author", None) + author_data = None + if author_obj: + author_data = { + "id": author_obj.id, + "slug": author_obj.slug, + "name": get_localized_text(author_obj.name, request) if isinstance(author_obj.name, list) else (author_obj.name or ""), + } + return { + "id": c.id, + "name": name, + "slug": author_obj.slug if (author_obj and author_obj.slug) else getattr(c, "slug", None), + "author_id": getattr(c, "author_id", None), + "author": author_data, + } + + researchers = [_serialize_contributor(r) for r in edition.researchers.all()] if hasattr(edition, "researchers") else [] + editors = [_serialize_contributor(e) for e in edition.editors.all()] if hasattr(edition, "editors") else [] + return { "id": edition.id, "title": ed_title or "", "edition_number": edition.edition_number, "publisher": pub or "", + "publication_place": {"city": city or "", "country": country or ""}, "year_of_publication": edition.year_of_publication or "", - "number_of_volumes": edition.number_of_volumes, + "number_of_volumes": num_vols, + "source_url": getattr(edition, "source_url", None) or "", + "researchers": researchers, + "editors": editors, } return None