|
|
|
@ -0,0 +1,54 @@ |
|
|
|
import csv |
|
|
|
import json |
|
|
|
import os |
|
|
|
from django.core.management.base import BaseCommand |
|
|
|
from django.db import transaction |
|
|
|
from django.conf import settings |
|
|
|
from apps.dobodbi_calendar.models import CalendarOccasions |
|
|
|
|
|
|
|
class Command(BaseCommand): |
|
|
|
help = 'Imports calendar occasions from calendar.csv file.' |
|
|
|
|
|
|
|
def handle(self, *args, **options): |
|
|
|
self.stdout.write(self.style.WARNING('Starting to import calendar occasions from CSV...')) |
|
|
|
|
|
|
|
csv_path = os.path.join(settings.BASE_DIR, 'apps', 'dobodbi_calendar', 'calendar.csv') |
|
|
|
if not os.path.exists(csv_path): |
|
|
|
self.stdout.write(self.style.ERROR(f'CSV file not found at: {csv_path}')) |
|
|
|
return |
|
|
|
|
|
|
|
created_count = 0 |
|
|
|
updated_count = 0 |
|
|
|
|
|
|
|
with open(csv_path, mode='r', encoding='utf-8') as f: |
|
|
|
reader = csv.DictReader(f) |
|
|
|
|
|
|
|
with transaction.atomic(): |
|
|
|
for row in reader: |
|
|
|
occasion_id = int(row['id']) |
|
|
|
occasion_type = row['occasion_type'] |
|
|
|
dates = json.loads(row['dates']) |
|
|
|
is_yearly = row['is_yearly'].lower() == 'true' |
|
|
|
translations = json.loads(row['translations']) |
|
|
|
is_global = row['is_global'].lower() == 'true' |
|
|
|
|
|
|
|
obj, created = CalendarOccasions.objects.update_or_create( |
|
|
|
id=occasion_id, |
|
|
|
defaults={ |
|
|
|
'occasion_type': occasion_type, |
|
|
|
'dates': dates, |
|
|
|
'is_yearly': is_yearly, |
|
|
|
'title': translations, |
|
|
|
'is_global': is_global, |
|
|
|
'event_type': 'religious', |
|
|
|
} |
|
|
|
) |
|
|
|
|
|
|
|
if created: |
|
|
|
created_count += 1 |
|
|
|
else: |
|
|
|
updated_count += 1 |
|
|
|
|
|
|
|
self.stdout.write(self.style.SUCCESS(f'Successfully imported calendar occasions.')) |
|
|
|
self.stdout.write(self.style.SUCCESS(f'Created: {created_count}')) |
|
|
|
self.stdout.write(self.style.SUCCESS(f'Updated: {updated_count}')) |