Browse Source

feat: implement standard routing links for Hadis collection, tag, and status models with corresponding serializers

master
Mohsen Taba 2 months ago
parent
commit
ca695dcb01
  1. 13
      apps/hadis/models/category.py
  2. 6
      apps/hadis/models/hadis.py
  3. 6
      apps/hadis/serializers/category.py
  4. 4
      apps/hadis/serializers/hadis.py

13
apps/hadis/models/category.py

@ -3,6 +3,7 @@ from django.utils.translation import gettext_lazy as _
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
from mptt.models import MPTTModel, TreeForeignKey from mptt.models import MPTTModel, TreeForeignKey
from django.utils.text import slugify from django.utils.text import slugify
from django.conf import settings
class HadisSect(models.Model): class HadisSect(models.Model):
@ -149,6 +150,18 @@ class HadisCategory(MPTTModel):
self.slug = base_slug self.slug = base_slug
super().save(*args, **kwargs) super().save(*args, **kwargs)
@property
def share_link(self):
if self.slug:
return f"{settings.DOVODI_DOMAIN}/arguments/category/{self.slug}"
return None
@property
def xmind_share_link(self):
if self.slug and self.xmind_file:
return f"{settings.DOVODI_DOMAIN}/xmind/{self.slug}"
return None
class Meta: class Meta:

6
apps/hadis/models/hadis.py

@ -326,8 +326,7 @@ class Hadis(models.Model):
# Generate/update share_link before saving # Generate/update share_link before saving
if self.slug: if self.slug:
category_slug = self.category.slug if self.category and self.category.slug else 'uncategorized'
self.share_link = f"{settings.DOVODI_DOMAIN}/arguments/hadith/{category_slug}/{self.slug}"
self.share_link = f"{settings.DOVODI_DOMAIN}/arguments/hadith/{self.slug}"
# Reset embedded_in if text or translation changes # Reset embedded_in if text or translation changes
if self.pk: if self.pk:
@ -538,8 +537,7 @@ class HadisCorrection(models.Model):
# Generate/update share_link before saving # Generate/update share_link before saving
if self.slug and self.hadis and self.hadis.slug: if self.slug and self.hadis and self.hadis.slug:
category_slug = self.hadis.category.slug if self.hadis.category and self.hadis.category.slug else 'uncategorized'
self.share_link = f"{settings.DOVODI_DOMAIN}/arguments/hadith/{category_slug}/{self.hadis.slug}/corrections/{self.slug}"
self.share_link = f"{settings.DOVODI_DOMAIN}/arguments/hadith/{self.hadis.slug}/corrections/{self.slug}"
# Reset embedded_in if text or translation changes # Reset embedded_in if text or translation changes
if self.pk: if self.pk:

6
apps/hadis/serializers/category.py

@ -120,7 +120,7 @@ class HadisCategorySelectSerializer(serializers.ModelSerializer):
class Meta: class Meta:
model = HadisCategory model = HadisCategory
fields = ['id', 'title', 'source_type','slug', 'sect_id', fields = ['id', 'title', 'source_type','slug', 'sect_id',
'sect_type','description','children_count','has_hadis','hadis_count']
'sect_type','description','children_count','has_hadis','hadis_count', 'share_link', 'xmind_share_link']
def get_has_hadis(self, obj): def get_has_hadis(self, obj):
"""Check if category can have hadis (no active children) and has hadis""" """Check if category can have hadis (no active children) and has hadis"""
@ -167,7 +167,7 @@ class HadisCategorySelectSourceSerializer(serializers.ModelSerializer):
class Meta: class Meta:
model = HadisCategory model = HadisCategory
fields = ['id', 'title', 'source_type','slug', 'sect_id', fields = ['id', 'title', 'source_type','slug', 'sect_id',
'sect_type','description','children_count','has_hadis','hadis_count']
'sect_type','description','children_count','has_hadis','hadis_count', 'share_link', 'xmind_share_link']
def get_has_hadis(self, obj): def get_has_hadis(self, obj):
"""Check if category can have hadis (no active children) and has hadis""" """Check if category can have hadis (no active children) and has hadis"""
@ -209,7 +209,7 @@ class CategorySerializer(serializers.ModelSerializer):
model = HadisCategory model = HadisCategory
fields = ['id', 'title', 'sect_id', 'sect_type','source_type', fields = ['id', 'title', 'sect_id', 'sect_type','source_type',
'description','slug', 'description','slug',
'children_count','has_hadis','hadis_count']
'children_count','has_hadis','hadis_count', 'share_link', 'xmind_share_link']
def get_children_count(self, obj): def get_children_count(self, obj):
# """Get count of active children categories that have children or hadis""" # """Get count of active children categories that have children or hadis"""

4
apps/hadis/serializers/hadis.py

@ -88,6 +88,7 @@ class HadisSyncSerializer(serializers.ModelSerializer):
for a in authors for a in authors
], ],
'description': book.description if book else None, 'description': book.description if book else None,
'share_link': book.share_link if book else None,
}) })
for img in reference.images.all(): for img in reference.images.all():
@ -579,10 +580,11 @@ class HadisReferenceSerializer(serializers.ModelSerializer):
book_title = serializers.SerializerMethodField() book_title = serializers.SerializerMethodField()
book_authors = serializers.SerializerMethodField() book_authors = serializers.SerializerMethodField()
book_description = serializers.SerializerMethodField() book_description = serializers.SerializerMethodField()
share_link = serializers.CharField(source='book_reference.share_link', read_only=True)
class Meta: class Meta:
model = HadisReference model = HadisReference
fields = [ fields = [
'id', 'book_title', 'book_description','book_authors'
'id', 'book_title', 'book_description','book_authors','share_link'
] ]
# def get_type_name(self, obj): # def get_type_name(self, obj):

Loading…
Cancel
Save