from django.core.management.base import BaseCommand, CommandError import json import tempfile import os from io import BytesIO from adm.storage import MediaStorage from adm.constants import CTS class Command(BaseCommand): def add_arguments(self, parser): parser.add_argument('file', type=str, help='JSON file') def handle(self, *args, **options): file = options['file'] extension = '.json' if file: with open(file) as json_data: content = json.load(json_data) #, encoding='utf-8' content = content[0] sections = [] for key, section in content.items(): if "type" in section and section["type"]=="info": sections.append({ **section, 'id': key, 'type': 'help', 'category': key, 'description': 'This is a section of the Driver`s app Help page which is the source of truth of it`s category. Drivers can access it`s content by going to the Help page (❓) and tapping on the section`s title. Each section has a list of items, which can help Drivers on a specific topic (item`s title).' }) for section in sections: filename = 'help-' + section['id'] json_data = json.dumps(section, ensure_ascii=False) temp_dir = tempfile.gettempdir() file_path = "%s%s%s%s" % (temp_dir, os.sep, filename, extension) with open(file_path, 'w') as f: f.write(json_data) media_storage = MediaStorage(CTS.CHATBOT_STORAGE_PATH) path_bucket = os.path.join(media_storage.location, 'docs/', filename + extension) if media_storage.exists(path_bucket): media_storage.delete(path_bucket) media_storage.save(path_bucket, BytesIO(json_data.encode("utf-8")))