You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
200 lines
7.9 KiB
200 lines
7.9 KiB
from django.db import models
|
|
from django.utils.translation import gettext_lazy as _
|
|
from django.core.exceptions import ValidationError
|
|
from mptt.models import MPTTModel, TreeForeignKey
|
|
from django.utils.text import slugify
|
|
|
|
|
|
class HadisSect(models.Model):
|
|
class SectType(models.TextChoices):
|
|
SHIA = 'shia', _('Shia')
|
|
SUNNI = 'sunni', _('Sunni')
|
|
|
|
sect_type = models.CharField(max_length=10, choices=SectType.choices, unique=True, verbose_name=_('Sect Name'))
|
|
title = models.JSONField(default = list , verbose_name=_('Title'))
|
|
description = models.JSONField(default = list , verbose_name=_('Description'))
|
|
is_active = models.BooleanField(default=True, verbose_name=_('Is Active'))
|
|
order = models.IntegerField(default=0, verbose_name=_('order'))
|
|
|
|
def __str__(self):
|
|
return f"{self.sect_type}: {self.title[0]['text']}"
|
|
|
|
def get_title(self,lang):
|
|
"""
|
|
Get title for a specific language
|
|
"""
|
|
|
|
if not self.title or not isinstance(self.title, list):
|
|
return None
|
|
|
|
for tr in self.title:
|
|
if isinstance(tr, dict) and tr.get('language_code') == lang:
|
|
return tr.get('text', '')
|
|
|
|
for tr in self.title:
|
|
if isinstance(tr, dict) and tr.get('language_code') == 'en':
|
|
return tr.get('text', '')
|
|
return None
|
|
|
|
def get_description(self,lang):
|
|
"""
|
|
Get title for a specific language
|
|
"""
|
|
|
|
if not self.description or not isinstance(self.description, list):
|
|
return None
|
|
|
|
for tr in self.description:
|
|
if isinstance(tr, dict) and tr.get('language_code') == lang:
|
|
return tr.get('text', '')
|
|
|
|
for tr in self.description:
|
|
if isinstance(tr, dict) and tr.get('language_code') == 'en':
|
|
return tr.get('text', '')
|
|
return None
|
|
|
|
|
|
|
|
class Meta:
|
|
verbose_name = _('Hadis Sect')
|
|
verbose_name_plural = _('Hadis Sects')
|
|
ordering = ('order',)
|
|
|
|
|
|
class HadisCategory(MPTTModel):
|
|
class SourceType(models.TextChoices):
|
|
QURAN = 'quran', _('Quran')
|
|
HADITH = 'hadith', _('Hadith')
|
|
HISTORY = 'history', _('History')
|
|
FATWA = 'fatwa', _('Fatwa')
|
|
QUOTE = 'quote', _('Quote')
|
|
|
|
parent = TreeForeignKey('self', on_delete=models.CASCADE, null=True, blank=True, related_name='children')
|
|
sect = models.ForeignKey(HadisSect, on_delete=models.PROTECT, verbose_name=_('Sect'), null=False, blank=False)
|
|
source_type = models.CharField(max_length=10, choices=SourceType.choices, verbose_name=_('Source Type'))
|
|
title = models.JSONField(default = list , verbose_name=_('Title'))
|
|
description = models.JSONField(default = list , verbose_name=_('Description'))
|
|
order = models.IntegerField(default=0, verbose_name=_('order'))
|
|
xmind_file = models.FileField(upload_to='hadis/xmind_files/', verbose_name=_('xmind file'), null=True, blank=True)
|
|
slug = models.SlugField(max_length=255, null=True, blank=True,allow_unicode=True)
|
|
content_type = None
|
|
language = None
|
|
language_id = None
|
|
|
|
def clean(self):
|
|
super().clean()
|
|
if self.parent and self.sect_id != self.parent.sect_id:
|
|
raise ValidationError(
|
|
_('Child category must have the same sect_type as its parent. '
|
|
f'Parent sect: {self.parent.sect.sect_type}, '
|
|
f'Your sect: {self.sect.sect_type}')
|
|
)
|
|
|
|
def save(self, *args, **kwargs):
|
|
self.full_clean()
|
|
if not self.slug or (isinstance(self.slug, str) and self.slug.strip() == ''):
|
|
# Try to get text from title field with robust error handling
|
|
try:
|
|
if self.title and isinstance(self.title, list) and len(self.title) > 0:
|
|
first_item = self.title[0]
|
|
if isinstance(first_item, dict):
|
|
title_text = first_item.get('text', '').strip()
|
|
if title_text:
|
|
base_slug = slugify(title_text, allow_unicode=True)
|
|
slug = base_slug
|
|
counter = 1
|
|
while HadisCategory.objects.filter(slug=slug).exclude(pk=self.pk).exists():
|
|
slug = f"{base_slug}-{counter}"
|
|
counter += 1
|
|
self.slug = slug
|
|
else:
|
|
# Fallback if text is empty
|
|
import time
|
|
suffix = int(time.time() * 1000) % 1000000
|
|
base_slug = f"category-{suffix}"
|
|
counter = 1
|
|
while HadisCategory.objects.filter(slug=base_slug).exclude(pk=self.pk).exists():
|
|
base_slug = f"category-{suffix}-{counter}"
|
|
counter += 1
|
|
self.slug = base_slug
|
|
else:
|
|
# Fallback if structure is invalid
|
|
import time
|
|
suffix = int(time.time() * 1000) % 1000000
|
|
base_slug = f"category-{suffix}"
|
|
counter = 1
|
|
while HadisCategory.objects.filter(slug=base_slug).exclude(pk=self.pk).exists():
|
|
base_slug = f"category-{suffix}-{counter}"
|
|
counter += 1
|
|
self.slug = base_slug
|
|
else:
|
|
# Fallback if title is empty or invalid
|
|
import time
|
|
suffix = int(time.time() * 1000) % 1000000
|
|
base_slug = f"category-{suffix}"
|
|
counter = 1
|
|
while HadisCategory.objects.filter(slug=base_slug).exclude(pk=self.pk).exists():
|
|
base_slug = f"category-{suffix}-{counter}"
|
|
counter += 1
|
|
self.slug = base_slug
|
|
except (IndexError, KeyError, AttributeError, TypeError):
|
|
# Fallback on any error
|
|
import time
|
|
suffix = int(time.time() * 1000) % 1000000
|
|
base_slug = f"category-{suffix}"
|
|
counter = 1
|
|
while HadisCategory.objects.filter(slug=base_slug).exclude(pk=self.pk).exists():
|
|
base_slug = f"category-{suffix}-{counter}"
|
|
counter += 1
|
|
self.slug = base_slug
|
|
super().save(*args, **kwargs)
|
|
|
|
|
|
|
|
class Meta:
|
|
indexes = [
|
|
models.Index(fields=['parent', 'sect']),
|
|
models.Index(fields=['sect', 'order'])
|
|
]
|
|
verbose_name = _('Hadis Category')
|
|
verbose_name_plural = _('Hadis Categories')
|
|
ordering = ('order',)
|
|
|
|
def __str__(self):
|
|
return f"{self.sect.sect_type}: {self.source_type} - {self.title[0]['text']}"
|
|
|
|
def get_title(self,lang):
|
|
"""
|
|
Get title for a specific language
|
|
"""
|
|
|
|
if not self.title or not isinstance(self.title, list):
|
|
return None
|
|
|
|
for tr in self.title:
|
|
if isinstance(tr, dict) and tr.get('language_code') == lang:
|
|
return tr.get('text', '')
|
|
|
|
for tr in self.title:
|
|
if isinstance(tr, dict) and tr.get('language_code') == 'en':
|
|
return tr.get('text', '')
|
|
return None
|
|
|
|
def get_description(self,lang):
|
|
"""
|
|
Get title for a specific language
|
|
"""
|
|
|
|
if not self.description or not isinstance(self.description, list):
|
|
return None
|
|
|
|
for tr in self.description:
|
|
if isinstance(tr, dict) and tr.get('language_code') == lang:
|
|
return tr.get('text', '')
|
|
|
|
for tr in self.description:
|
|
if isinstance(tr, dict) and tr.get('language_code') == 'en':
|
|
return tr.get('text', '')
|
|
return None
|
|
|
|
|