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.
138 lines
4.8 KiB
138 lines
4.8 KiB
"""
|
|
Basic data seeding management command for Hadis app models.
|
|
This command creates only the essential records needed for the app to function.
|
|
"""
|
|
|
|
from django.core.management.base import BaseCommand, CommandError
|
|
from django.db import transaction
|
|
|
|
# Import models
|
|
from apps.hadis.models import HadisSect, HadisStatus, HadisTag
|
|
|
|
|
|
class Command(BaseCommand):
|
|
help = 'Seed basic data for Hadis app models'
|
|
|
|
def add_arguments(self, parser):
|
|
parser.add_argument(
|
|
'--clear',
|
|
action='store_true',
|
|
help='Clear existing basic data before seeding',
|
|
)
|
|
|
|
def handle(self, **options):
|
|
if options['clear']:
|
|
self.clear_existing_data()
|
|
|
|
try:
|
|
with transaction.atomic():
|
|
self.stdout.write(
|
|
self.style.SUCCESS('Starting basic Hadis data seeding...')
|
|
)
|
|
|
|
# Seed basic data
|
|
statuses = self.seed_hadis_statuses()
|
|
tags = self.seed_hadis_tags()
|
|
sects = self.seed_hadis_sects()
|
|
|
|
self.stdout.write(
|
|
self.style.SUCCESS(
|
|
f'Successfully seeded basic data: '
|
|
f'{len(statuses)} statuses, {len(tags)} tags, {len(sects)} sects'
|
|
)
|
|
)
|
|
|
|
except Exception as e:
|
|
self.stdout.write(
|
|
self.style.ERROR(f'Error during seeding: {str(e)}')
|
|
)
|
|
raise CommandError(f'Seeding failed: {str(e)}')
|
|
|
|
def clear_existing_data(self):
|
|
"""Clear existing basic data"""
|
|
self.stdout.write("Clearing existing basic data...")
|
|
|
|
HadisSect.objects.all().delete()
|
|
HadisStatus.objects.all().delete()
|
|
HadisTag.objects.all().delete()
|
|
|
|
self.stdout.write("Basic data cleared.")
|
|
|
|
def seed_hadis_statuses(self):
|
|
"""Create HadisStatus records"""
|
|
self.stdout.write("Creating Hadis Statuses...")
|
|
|
|
statuses_data = [
|
|
{'title': 'Достоверный', 'color': 'green', 'order': 1},
|
|
{'title': 'Хороший', 'color': 'blue', 'order': 2},
|
|
{'title': 'Слабый', 'color': 'yellow', 'order': 3},
|
|
{'title': 'Выдуманный', 'color': 'red', 'order': 4},
|
|
]
|
|
|
|
statuses = []
|
|
for data in statuses_data:
|
|
status, created = HadisStatus.objects.get_or_create(
|
|
title=data['title'],
|
|
defaults=data
|
|
)
|
|
statuses.append(status)
|
|
if created:
|
|
self.stdout.write(f" Created status: {status.title}")
|
|
|
|
return statuses
|
|
|
|
def seed_hadis_tags(self):
|
|
"""Create HadisTag records"""
|
|
self.stdout.write("Creating Hadis Tags...")
|
|
|
|
tags_data = [
|
|
'Поклонение', 'Молитва', 'Пост', 'Хадж', 'Закят',
|
|
'Нравственность', 'Терпение', 'Справедливость',
|
|
'Фикх', 'Предписания', 'Толкование', 'Коран',
|
|
'Имамат', 'Мольба', 'Единобожие'
|
|
]
|
|
|
|
tags = []
|
|
for tag_title in tags_data:
|
|
tag, created = HadisTag.objects.get_or_create(
|
|
title=tag_title,
|
|
defaults={'status': True}
|
|
)
|
|
tags.append(tag)
|
|
if created:
|
|
self.stdout.write(f" Created tag: {tag.title}")
|
|
|
|
return tags
|
|
|
|
def seed_hadis_sects(self):
|
|
"""Create HadisSect records"""
|
|
self.stdout.write("Creating Hadis Sects...")
|
|
|
|
sects_data = [
|
|
{'sect_type': 'shia', 'title': 'Шииты-двунадесятники', 'is_active': True, 'order': 1},
|
|
{'sect_type': 'sunni', 'title': 'Сунниты', 'is_active': True, 'order': 2},
|
|
]
|
|
|
|
sects = []
|
|
for data in sects_data:
|
|
self.stdout.write(f" Processing sect: {data['sect_type']}")
|
|
|
|
# Check if sect exists
|
|
try:
|
|
sect = HadisSect.objects.get(sect_type=data['sect_type'])
|
|
self.stdout.write(f" Sect already exists: {sect.title}")
|
|
sects.append(sect)
|
|
except HadisSect.DoesNotExist:
|
|
# Create new sect
|
|
self.stdout.write(f" Creating new sect: {data['sect_type']}")
|
|
sect = HadisSect(
|
|
sect_type=data['sect_type'],
|
|
title=data['title'],
|
|
is_active=data['is_active'],
|
|
order=data['order']
|
|
)
|
|
sect.save()
|
|
self.stdout.write(f" Created sect: {sect.title}")
|
|
sects.append(sect)
|
|
|
|
return sects
|