Browse Source

slug for hadis urls and serializers.

master
Mohsen Taba 5 months ago
parent
commit
6289214947
  1. 24
      apps/hadis/docs.py
  2. 8
      apps/hadis/serializers/hadis.py
  3. 8
      apps/hadis/urls.py
  4. 31
      apps/hadis/views/hadis.py

24
apps/hadis/docs.py

@ -392,10 +392,10 @@ hadis_basic_swagger = swagger_auto_schema(
tags=['Hadis'], tags=['Hadis'],
manual_parameters=[ manual_parameters=[
openapi.Parameter( openapi.Parameter(
'hadis_id',
'hadis_slug',
openapi.IN_PATH, openapi.IN_PATH,
description="ID of the hadis",
type=openapi.TYPE_INTEGER,
description="Slug of the hadis",
type=openapi.TYPE_STRING,
required=True required=True
) )
], ],
@ -436,10 +436,10 @@ hadis_detail_swagger = swagger_auto_schema(
tags=['Hadis'], tags=['Hadis'],
manual_parameters=[ manual_parameters=[
openapi.Parameter( openapi.Parameter(
'hadis_id',
'hadis_slug',
openapi.IN_PATH, openapi.IN_PATH,
description="ID of the hadis",
type=openapi.TYPE_INTEGER,
description="Slug of the hadis",
type=openapi.TYPE_STRING,
required=True required=True
) )
], ],
@ -511,10 +511,10 @@ hadis_transmitters_swagger = swagger_auto_schema(
manual_parameters=[ manual_parameters=[
# Path Parameter: ID # Path Parameter: ID
openapi.Parameter( openapi.Parameter(
'hadis_id',
'hadis_slug',
openapi.IN_PATH, 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 required=True
), ),
# Query Parameter: Layer Filter (Optional) # Query Parameter: Layer Filter (Optional)
@ -627,10 +627,10 @@ hadis_corrections_swagger = swagger_auto_schema(
tags=['Hadis'], tags=['Hadis'],
manual_parameters=[ manual_parameters=[
openapi.Parameter( openapi.Parameter(
'hadis_id',
'hadis_slug',
openapi.IN_PATH, 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, required=True,
example=1 example=1
) )

8
apps/hadis/serializers/hadis.py

@ -48,7 +48,7 @@ class HadisSyncSerializer(serializers.ModelSerializer):
model = Hadis model = Hadis
fields = [ fields = [
# header (no-extend) # header (no-extend)
'id', 'number', 'category_id', 'title', 'title_narrator', 'text', 'translation',
'id', 'number', 'slug', 'category_id', 'title', 'title_narrator', 'text', 'translation',
# grouped sections # grouped sections
'detail', 'narrators', 'explanations', 'corrections', 'detail', 'narrators', 'explanations', 'corrections',
] ]
@ -150,7 +150,7 @@ class HadisListSerializer(serializers.ModelSerializer):
class Meta: class Meta:
model = Hadis model = Hadis
fields = ['id', 'number', 'title','title_narrator', 'text' ,
fields = ['id', 'number', 'slug', 'title','title_narrator', 'text' ,
'translation','category','share_link'] 'translation','category','share_link']
def get_category(self, obj): def get_category(self, obj):
@ -505,7 +505,7 @@ class HadisBasicSerializer(serializers.ModelSerializer):
class Meta: class Meta:
model = Hadis model = Hadis
fields = [ fields = [
'id', 'title', 'title_narrator', 'text',
'id', 'slug', 'title', 'title_narrator', 'text',
'translation', 'share_link','explanation','category' 'translation', 'share_link','explanation','category'
] ]
@ -540,7 +540,7 @@ class HadisDetailSerializer(serializers.ModelSerializer):
class Meta: class Meta:
model = Hadis model = Hadis
fields = [ fields = [
'id', 'number',
'id', 'number', 'slug',
'hadis_status_text','hadis_status', 'links','share_link', 'hadis_status_text','hadis_status', 'links','share_link',
'tags', 'references','reference_images','address' 'tags', 'references','reference_images','address'
] ]

8
apps/hadis/urls.py

@ -16,10 +16,10 @@ urlpatterns = [
path('sync/references/', BookReferenceSyncView.as_view(), name='reference-sync'), path('sync/references/', BookReferenceSyncView.as_view(), name='reference-sync'),
path('info/', HadisInfoView.as_view(), name='hadis-info'), path('info/', HadisInfoView.as_view(), name='hadis-info'),
path('category/<int:category_id>/', HadisListView.as_view(), name='hadis-list'), path('category/<int:category_id>/', HadisListView.as_view(), name='hadis-list'),
path('<int:hadis_id>/', HadisBasicView.as_view(), name='hadis-basic'),
path('<int:hadis_id>/detail/', HadisDetailView.as_view(), name='hadis-detail'),
path('<int:hadis_id>/transmitters/', HadisTransmittersView.as_view(), name='hadis-transmitters'),
path('<int:hadis_id>/corrections/', HadisCorrectionsView.as_view(), name='hadis-corrections'),
path('<str:hadis_slug>/', HadisBasicView.as_view(), name='hadis-basic'),
path('<str:hadis_slug>/detail/', HadisDetailView.as_view(), name='hadis-detail'),
path('<str:hadis_slug>/transmitters/', HadisTransmittersView.as_view(), name='hadis-transmitters'),
path('<str:hadis_slug>/corrections/', HadisCorrectionsView.as_view(), name='hadis-corrections'),
path('categories/<str:sect_type>/<str:slug>/<str:source_type>/', HadisCategorySelectBySectSourceView.as_view(), name='categories-tree-by-sect-source'), path('categories/<str:sect_type>/<str:slug>/<str:source_type>/', HadisCategorySelectBySectSourceView.as_view(), name='categories-tree-by-sect-source'),
path('categories/<str:sect_type>/<str:slug>/', HadisCategorySelectBySectView.as_view(), name='categories-tree-by-sect'), path('categories/<str:sect_type>/<str:slug>/', HadisCategorySelectBySectView.as_view(), name='categories-tree-by-sect'),
path('categories/<str:sect_type>/', CategoriesBySectView.as_view(), name='categories-by-sect'), path('categories/<str:sect_type>/', CategoriesBySectView.as_view(), name='categories-by-sect'),

31
apps/hadis/views/hadis.py

@ -85,11 +85,11 @@ class HadisListView(ListAPIView):
class HadisBasicView(RetrieveAPIView): 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 serializer_class = HadisBasicSerializer
lookup_field = 'id'
lookup_url_kwarg = 'hadis_id'
lookup_field = 'slug'
lookup_url_kwarg = 'hadis_slug'
@hadis_basic_swagger @hadis_basic_swagger
def get(self, request, *args, **kwargs): def get(self, request, *args, **kwargs):
@ -101,11 +101,11 @@ class HadisBasicView(RetrieveAPIView):
class HadisDetailView(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 serializer_class = HadisDetailSerializer
lookup_field = 'id'
lookup_url_kwarg = 'hadis_id'
lookup_field = 'slug'
lookup_url_kwarg = 'hadis_slug'
@hadis_detail_swagger @hadis_detail_swagger
def get(self, request, *args, **kwargs): def get(self, request, *args, **kwargs):
@ -129,7 +129,8 @@ class HadisTransmittersView(RetrieveAPIView):
if a ?layer=slug param is provided. if a ?layer=slug param is provided.
""" """
serializer_class = HadisTransmitterListSerializer serializer_class = HadisTransmitterListSerializer
lookup_url_kwarg = 'hadis_id'
lookup_field = 'slug'
lookup_url_kwarg = 'hadis_slug'
@hadis_transmitters_swagger @hadis_transmitters_swagger
def get(self, request, *args, **kwargs): def get(self, request, *args, **kwargs):
@ -162,8 +163,8 @@ class HadisCorrectionsView(ListAPIView):
API view to retrieve corrections for a specific hadis API view to retrieve corrections for a specific hadis
""" """
serializer_class = HadisCorrectionSerializer serializer_class = HadisCorrectionSerializer
lookup_field = 'id'
lookup_url_kwarg = 'hadis_id'
lookup_field = 'slug'
lookup_url_kwarg = 'hadis_slug'
@hadis_corrections_swagger @hadis_corrections_swagger
def get(self, request, *args, **kwargs): def get(self, request, *args, **kwargs):
@ -187,7 +188,11 @@ class HadisCorrectionsView(ListAPIView):
# }) # })
def get_queryset(self): 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()
Loading…
Cancel
Save