From 62892149474f6b9ddccc1f7eeb7b04bba1e872d0 Mon Sep 17 00:00:00 2001 From: mohsentaba Date: Mon, 22 Dec 2025 10:06:11 +0330 Subject: [PATCH] slug for hadis urls and serializers. --- apps/hadis/docs.py | 24 ++++++++++++------------ apps/hadis/serializers/hadis.py | 8 ++++---- apps/hadis/urls.py | 8 ++++---- apps/hadis/views/hadis.py | 31 ++++++++++++++++++------------- 4 files changed, 38 insertions(+), 33 deletions(-) diff --git a/apps/hadis/docs.py b/apps/hadis/docs.py index 3f7f9ab..251508e 100644 --- a/apps/hadis/docs.py +++ b/apps/hadis/docs.py @@ -392,10 +392,10 @@ hadis_basic_swagger = swagger_auto_schema( tags=['Hadis'], manual_parameters=[ openapi.Parameter( - 'hadis_id', + 'hadis_slug', openapi.IN_PATH, - description="ID of the hadis", - type=openapi.TYPE_INTEGER, + description="Slug of the hadis", + type=openapi.TYPE_STRING, required=True ) ], @@ -436,10 +436,10 @@ hadis_detail_swagger = swagger_auto_schema( tags=['Hadis'], manual_parameters=[ openapi.Parameter( - 'hadis_id', + 'hadis_slug', openapi.IN_PATH, - description="ID of the hadis", - type=openapi.TYPE_INTEGER, + description="Slug of the hadis", + type=openapi.TYPE_STRING, required=True ) ], @@ -511,10 +511,10 @@ hadis_transmitters_swagger = swagger_auto_schema( manual_parameters=[ # Path Parameter: ID openapi.Parameter( - 'hadis_id', + 'hadis_slug', openapi.IN_PATH, - description="The ID of the Hadis to fetch", - type=openapi.TYPE_INTEGER, + description="The Slug of the Hadis to fetch", + type=openapi.TYPE_STRING, required=True ), # Query Parameter: Layer Filter (Optional) @@ -627,10 +627,10 @@ hadis_corrections_swagger = swagger_auto_schema( tags=['Hadis'], manual_parameters=[ openapi.Parameter( - 'hadis_id', + 'hadis_slug', openapi.IN_PATH, - description="Unique identifier of the hadis. Must be a valid hadis ID that exists in the system. Only active hadis (status=True) are accessible.", - type=openapi.TYPE_INTEGER, + description="Slug of the hadis. Must be a valid hadis slug that exists in the system. Only active hadis (status=True) are accessible.", + type=openapi.TYPE_STRING, required=True, example=1 ) diff --git a/apps/hadis/serializers/hadis.py b/apps/hadis/serializers/hadis.py index 9eb534a..6320931 100644 --- a/apps/hadis/serializers/hadis.py +++ b/apps/hadis/serializers/hadis.py @@ -48,7 +48,7 @@ class HadisSyncSerializer(serializers.ModelSerializer): model = Hadis fields = [ # header (no-extend) - 'id', 'number', 'category_id', 'title', 'title_narrator', 'text', 'translation', + 'id', 'number', 'slug', 'category_id', 'title', 'title_narrator', 'text', 'translation', # grouped sections 'detail', 'narrators', 'explanations', 'corrections', ] @@ -150,7 +150,7 @@ class HadisListSerializer(serializers.ModelSerializer): class Meta: model = Hadis - fields = ['id', 'number', 'title','title_narrator', 'text' , + fields = ['id', 'number', 'slug', 'title','title_narrator', 'text' , 'translation','category','share_link'] def get_category(self, obj): @@ -505,7 +505,7 @@ class HadisBasicSerializer(serializers.ModelSerializer): class Meta: model = Hadis fields = [ - 'id', 'title', 'title_narrator', 'text', + 'id', 'slug', 'title', 'title_narrator', 'text', 'translation', 'share_link','explanation','category' ] @@ -540,7 +540,7 @@ class HadisDetailSerializer(serializers.ModelSerializer): class Meta: model = Hadis fields = [ - 'id', 'number', + 'id', 'number', 'slug', 'hadis_status_text','hadis_status', 'links','share_link', 'tags', 'references','reference_images','address' ] diff --git a/apps/hadis/urls.py b/apps/hadis/urls.py index 8810a90..c984ad4 100644 --- a/apps/hadis/urls.py +++ b/apps/hadis/urls.py @@ -16,10 +16,10 @@ urlpatterns = [ path('sync/references/', BookReferenceSyncView.as_view(), name='reference-sync'), path('info/', HadisInfoView.as_view(), name='hadis-info'), path('category//', HadisListView.as_view(), name='hadis-list'), - path('/', HadisBasicView.as_view(), name='hadis-basic'), - path('/detail/', HadisDetailView.as_view(), name='hadis-detail'), - path('/transmitters/', HadisTransmittersView.as_view(), name='hadis-transmitters'), - path('/corrections/', HadisCorrectionsView.as_view(), name='hadis-corrections'), + path('/', HadisBasicView.as_view(), name='hadis-basic'), + path('/detail/', HadisDetailView.as_view(), name='hadis-detail'), + path('/transmitters/', HadisTransmittersView.as_view(), name='hadis-transmitters'), + path('/corrections/', HadisCorrectionsView.as_view(), name='hadis-corrections'), path('categories////', HadisCategorySelectBySectSourceView.as_view(), name='categories-tree-by-sect-source'), path('categories///', HadisCategorySelectBySectView.as_view(), name='categories-tree-by-sect'), path('categories//', CategoriesBySectView.as_view(), name='categories-by-sect'), diff --git a/apps/hadis/views/hadis.py b/apps/hadis/views/hadis.py index 3a5aadb..651aa20 100644 --- a/apps/hadis/views/hadis.py +++ b/apps/hadis/views/hadis.py @@ -85,11 +85,11 @@ class HadisListView(ListAPIView): class HadisBasicView(RetrieveAPIView): """ - API view to retrieve basic Hadis information by hadis_id + API view to retrieve basic Hadis information by hadis_slug """ serializer_class = HadisBasicSerializer - lookup_field = 'id' - lookup_url_kwarg = 'hadis_id' + lookup_field = 'slug' + lookup_url_kwarg = 'hadis_slug' @hadis_basic_swagger def get(self, request, *args, **kwargs): @@ -101,11 +101,11 @@ class HadisBasicView(RetrieveAPIView): class HadisDetailView(RetrieveAPIView): """ - API view to retrieve detailed Hadis information by hadis_id (excluding transmitters and corrections) + API view to retrieve detailed Hadis information by hadis_slug (excluding transmitters and corrections) """ serializer_class = HadisDetailSerializer - lookup_field = 'id' - lookup_url_kwarg = 'hadis_id' + lookup_field = 'slug' + lookup_url_kwarg = 'hadis_slug' @hadis_detail_swagger def get(self, request, *args, **kwargs): @@ -129,7 +129,8 @@ class HadisTransmittersView(RetrieveAPIView): if a ?layer=slug param is provided. """ serializer_class = HadisTransmitterListSerializer - lookup_url_kwarg = 'hadis_id' + lookup_field = 'slug' + lookup_url_kwarg = 'hadis_slug' @hadis_transmitters_swagger def get(self, request, *args, **kwargs): @@ -162,8 +163,8 @@ class HadisCorrectionsView(ListAPIView): API view to retrieve corrections for a specific hadis """ serializer_class = HadisCorrectionSerializer - lookup_field = 'id' - lookup_url_kwarg = 'hadis_id' + lookup_field = 'slug' + lookup_url_kwarg = 'hadis_slug' @hadis_corrections_swagger def get(self, request, *args, **kwargs): @@ -187,7 +188,11 @@ class HadisCorrectionsView(ListAPIView): # }) def get_queryset(self): - hadis_id = self.kwargs.get('hadis_id') - if not HadisCorrection.objects.filter(hadis=hadis_id).exists(): - return Hadis.objects.none() - return HadisCorrection.objects.filter(hadis=hadis_id) + hadis_slug = self.kwargs.get('hadis_slug') + try: + hadis = Hadis.objects.get(slug=hadis_slug, status=True) + if not HadisCorrection.objects.filter(hadis=hadis).exists(): + return Hadis.objects.none() + return HadisCorrection.objects.filter(hadis=hadis) + except Hadis.DoesNotExist: + return HadisCorrection.objects.none()