[ADD] HELPDESK: Added custom command to create faqs

This commit is contained in:
Horilla
2024-09-05 23:30:45 +05:30
parent 21264f59d2
commit ab0803b23f
2 changed files with 34 additions and 23 deletions

View File

@@ -53,4 +53,4 @@
"answer": "employee --> create --> Fill out the form"
}
]
}
}

View File

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