Browse Source
feat(hadis): implement is_topic architectural separation for categories and topics
feat(hadis): implement is_topic architectural separation for categories and topics
- Add is_topic boolean field to HadisCategory model with indexing - Enforce validation rules in clean(): * Topics cannot have children and must define a source_type * Structural categories cannot contain hadiths and have source_type=None * Parents cannot be topics * Hadiths can only be attached to topics (is_topic=True) - Update admin serializers and views to support is_topic filtering and mapping - Expose is_topic in public category serializers (SimpleCategory, HadisCategorySelectSerializer, CategorySerializer) - Include migration 0051_add_category_is_topicmaster
7 changed files with 170 additions and 59 deletions
-
34apps/hadis/migrations/0051_add_category_is_topic.py
-
57apps/hadis/models/category.py
-
11apps/hadis/models/hadis.py
-
15apps/hadis/serializers/category.py
-
39apps/hadis/serializers/serializers_admin.py
-
3apps/hadis/views/category.py
-
8apps/hadis/views_admin.py
@ -0,0 +1,34 @@ |
|||
# Generated by Django 4.2.30 on 2026-10-15 09:29 |
|||
|
|||
from django.db import migrations, models |
|||
|
|||
|
|||
def populate_is_topic(apps, schema_editor): |
|||
HadisCategory = apps.get_model('hadis', 'HadisCategory') |
|||
# All categories that have children -> is_topic=False, source_type=None |
|||
parent_ids = list(HadisCategory.objects.filter(children__isnull=False).values_list('id', flat=True).distinct()) |
|||
HadisCategory.objects.filter(id__in=parent_ids).update(is_topic=False, source_type=None) |
|||
|
|||
# All leaf categories (no children) -> is_topic=True |
|||
HadisCategory.objects.exclude(id__in=parent_ids).update(is_topic=True) |
|||
|
|||
|
|||
class Migration(migrations.Migration): |
|||
|
|||
dependencies = [ |
|||
('hadis', '0050_hadis_categories'), |
|||
] |
|||
|
|||
operations = [ |
|||
migrations.AddField( |
|||
model_name='hadiscategory', |
|||
name='is_topic', |
|||
field=models.BooleanField(db_index=True, default=False, verbose_name='Is Topic'), |
|||
), |
|||
migrations.AlterField( |
|||
model_name='hadiscategory', |
|||
name='source_type', |
|||
field=models.CharField(blank=True, choices=[('quran', 'Quran'), ('hadith', 'Hadith'), ('history', 'History'), ('fatwa', 'Fatwa'), ('quote', 'Quote')], max_length=10, null=True, verbose_name='Source Type'), |
|||
), |
|||
migrations.RunPython(populate_is_topic, reverse_code=migrations.RunPython.noop), |
|||
] |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue