Browse Source

feat(hadis): sort edition contributors by role order in public v2 and hadith serializers

master
Mohsen Taba 2 weeks ago
parent
commit
ce8b97bf3c
  1. 5
      apps/hadis/serializers/hadis.py
  2. 7
      apps/hadis/serializers/reference_v2.py

5
apps/hadis/serializers/hadis.py

@ -2043,7 +2043,7 @@ def get_cached_edition_metadata():
).prefetch_related(
Prefetch('researchers', queryset=BookResearcher.objects.select_related('author')),
Prefetch('editors', queryset=BookEditor.objects.select_related('author')),
Prefetch('contributors', queryset=BookEditionContributor.objects.select_related('author', 'role').prefetch_related('volumes')),
Prefetch('contributors', queryset=BookEditionContributor.objects.select_related('author', 'role').prefetch_related('volumes').order_by('role__order', 'id')),
)
)
by_id = {}
@ -2233,7 +2233,8 @@ def _build_edition_info_dict(obj, edition, request):
raw_researchers = edition._cached_researchers if hasattr(edition, '_cached_researchers') else (edition.researchers.all() if hasattr(edition, "researchers") else [])
raw_editors = edition._cached_editors if hasattr(edition, '_cached_editors') else (edition.editors.all() if hasattr(edition, "editors") else [])
raw_contributors = edition._cached_contributors if hasattr(edition, '_cached_contributors') else (edition.contributors.all() if hasattr(edition, "contributors") else [])
raw_contributors = list(edition._cached_contributors if hasattr(edition, '_cached_contributors') else (edition.contributors.all() if hasattr(edition, "contributors") else []))
raw_contributors.sort(key=lambda c: (getattr(c.role, 'order', 999999) if getattr(c, 'role_id', None) and getattr(c, 'role', None) else 999999, c.id or 0))
researchers = [_serialize_contributor(r) for r in raw_researchers]
editors = [_serialize_contributor(e) for e in raw_editors]
contributors = [_serialize_contributor(c) for c in raw_contributors]

7
apps/hadis/serializers/reference_v2.py

@ -175,7 +175,7 @@ class BookEditionV2Serializer(serializers.ModelSerializer):
publication_place = serializers.SerializerMethodField()
researchers = BookResearcherV2Serializer(many=True, read_only=True)
editors = BookEditorV2Serializer(many=True, read_only=True)
contributors = BookEditionContributorV2Serializer(many=True, read_only=True)
contributors = serializers.SerializerMethodField()
number_of_volumes = serializers.SerializerMethodField()
class Meta:
@ -186,6 +186,11 @@ class BookEditionV2Serializer(serializers.ModelSerializer):
'researchers', 'editors', 'contributors'
]
def get_contributors(self, obj):
items = list(obj.contributors.select_related('role', 'author').prefetch_related('volumes').all())
items.sort(key=lambda c: (getattr(c.role, 'order', 999999) if getattr(c, 'role_id', None) and c.role else 999999, c.id or 0))
return BookEditionContributorV2Serializer(items, many=True, context=self.context).data
def get_publisher(self, obj):
request = self.context.get("request")
if getattr(obj, "publisher_item_id", None) and obj.publisher_item and obj.publisher_item.title:

Loading…
Cancel
Save