2024-09-05 17:07:03 +05:30
|
|
|
import json
|
2024-09-05 23:30:45 +05:30
|
|
|
|
2024-09-05 17:07:03 +05:30
|
|
|
from django.core.management.base import BaseCommand
|
2024-09-05 23:30:45 +05:30
|
|
|
|
|
|
|
|
from helpdesk.models import FAQ, FAQCategory
|
|
|
|
|
|
2024-09-05 17:07:03 +05:30
|
|
|
|
|
|
|
|
class Command(BaseCommand):
|
2024-09-05 23:30:45 +05:30
|
|
|
help = "Create FAQ categories and FAQs from a JSON file"
|
2024-09-05 17:07:03 +05:30
|
|
|
|
|
|
|
|
def add_arguments(self, parser):
|
|
|
|
|
parser.add_argument(
|
2024-09-05 23:30:45 +05:30
|
|
|
"file_path",
|
|
|
|
|
type=str,
|
|
|
|
|
help="The path to the JSON file containing FAQ categories and FAQs",
|
2024-09-05 17:07:03 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def handle(self, *args, **kwargs):
|
2024-09-05 23:30:45 +05:30
|
|
|
file_path = kwargs["file_path"]
|
|
|
|
|
|
2024-09-05 17:07:03 +05:30
|
|
|
try:
|
2024-09-05 23:30:45 +05:30
|
|
|
with open(file_path, "r") as file:
|
2024-09-05 17:07:03 +05:30
|
|
|
data_dict = json.load(file)
|
|
|
|
|
|
2024-09-05 23:30:45 +05:30
|
|
|
faq_categories = data_dict.get("FAQ_CATEGORY", [])
|
|
|
|
|
faqs = data_dict.get("FAQS", [])
|
2024-09-05 17:07:03 +05:30
|
|
|
|
|
|
|
|
for category_data in faq_categories:
|
2024-09-05 23:30:45 +05:30
|
|
|
title = category_data.get("title")
|
|
|
|
|
description = category_data.get("description")
|
2024-09-05 17:07:03 +05:30
|
|
|
category, created = FAQCategory.objects.get_or_create(
|
2024-09-05 23:30:45 +05:30
|
|
|
title=title, defaults={"description": description}
|
2024-09-05 17:07:03 +05:30
|
|
|
)
|
|
|
|
|
if not created:
|
|
|
|
|
category.description = description
|
|
|
|
|
category.save()
|
|
|
|
|
|
|
|
|
|
for faq_data in faqs:
|
2024-09-05 23:30:45 +05:30
|
|
|
category_title = faq_data.get("category")
|
|
|
|
|
question = faq_data.get("question")
|
|
|
|
|
answer = faq_data.get("answer")
|
2024-09-05 17:07:03 +05:30
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
category = FAQCategory.objects.get(title=category_title)
|
|
|
|
|
except FAQCategory.DoesNotExist:
|
2024-09-05 23:30:45 +05:30
|
|
|
self.stdout.write(
|
|
|
|
|
self.style.ERROR(
|
|
|
|
|
f'Category "{category_title}" does not exist. Skipping FAQ: {question}'
|
|
|
|
|
)
|
|
|
|
|
)
|
2024-09-05 17:07:03 +05:30
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
FAQ.objects.get_or_create(
|
|
|
|
|
question=question,
|
|
|
|
|
defaults={
|
2024-09-05 23:30:45 +05:30
|
|
|
"answer": answer,
|
|
|
|
|
"category": category,
|
|
|
|
|
},
|
2024-09-05 17:07:03 +05:30
|
|
|
)
|
2024-09-05 23:30:45 +05:30
|
|
|
self.stdout.write(
|
|
|
|
|
self.style.SUCCESS("Successfully created FAQs and FAQ categories.")
|
|
|
|
|
)
|
2024-09-05 17:07:03 +05:30
|
|
|
|
|
|
|
|
except FileNotFoundError:
|
2024-09-05 23:30:45 +05:30
|
|
|
self.stdout.write(self.style.ERROR(f"File not found: {file_path}"))
|
2024-09-05 17:07:03 +05:30
|
|
|
except json.JSONDecodeError as e:
|
2024-09-05 23:30:45 +05:30
|
|
|
self.stdout.write(self.style.ERROR(f"Invalid JSON format: {e}"))
|
2024-09-05 17:07:03 +05:30
|
|
|
except Exception as e:
|
2024-09-05 23:30:45 +05:30
|
|
|
self.stdout.write(self.style.ERROR(f"Error: {e}"))
|