Browse Source

add more details for argument objects

master
Mohsen Taba 2 weeks ago
parent
commit
2d80812916
  1. 41
      apps/hadis/serializers/hadis.py
  2. 9
      apps/hadis/views/hadis.py

41
apps/hadis/serializers/hadis.py

@ -286,11 +286,50 @@ class HadisListSerializer(serializers.ModelSerializer):
translation = LocalizedField()
title = LocalizedField()
title_narrator = LocalizedField()
address = serializers.SerializerMethodField()
links = serializers.SerializerMethodField()
reference_images = serializers.SerializerMethodField()
images = serializers.SerializerMethodField()
class Meta:
model = Hadis
fields = ['id', 'number', 'slug', 'title', 'title_narrator', 'text',
'translation', 'category', 'status', 'bookmark', 'share_link']
'translation', 'category', 'status', 'bookmark', 'share_link',
'address', 'links', 'reference_images', 'images']
def get_address(self, obj):
formatted = format_address_output(obj.address)
if formatted:
return formatted
first_ref = obj.references.first()
if first_ref and hasattr(first_ref, 'address') and first_ref.address:
return format_address_output(first_ref.address)
return []
def get_links(self, obj):
return format_links_output(obj.links)
def get_reference_images(self, obj):
request = self.context.get('request')
images_list = []
for ref in obj.references.all():
for img in ref.images.all().order_by('priority'):
url = None
if img.thumbnail:
url = absolute_https_url(img.thumbnail.url, request) if request else absolute_https_url(img.thumbnail.url)
elif hasattr(img, 'image') and img.image:
url = absolute_https_url(img.image.url, request) if request else absolute_https_url(img.image.url)
if url:
images_list.append({
"id": img.id,
"image": url,
"thumbnail": url,
"priority": img.priority,
})
return images_list
def get_images(self, obj):
return self.get_reference_images(obj)
def get_category(self, obj):
"""Get category id and title"""

9
apps/hadis/views/hadis.py

@ -264,7 +264,14 @@ class HadisListView(ListAPIView):
status=True
).annotate(
layer_count=Count('transmitters__narrator_layer', distinct=True)
).select_related('category')
).select_related('category').prefetch_related(
Prefetch(
'references',
queryset=HadisReference.objects.prefetch_related(
Prefetch('images', queryset=ReferenceImage.objects.order_by('priority'))
)
)
)
# 👇 1. Apply Search Filter
search_query = self.request.query_params.get('search', None)

Loading…
Cancel
Save