parseHelp.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. from django.core.management.base import BaseCommand, CommandError
  2. import json
  3. import tempfile
  4. import os
  5. from io import BytesIO
  6. from adm.storage import MediaStorage
  7. from adm.constants import CTS
  8. class Command(BaseCommand):
  9. def add_arguments(self, parser):
  10. parser.add_argument('file', type=str, help='JSON file')
  11. def handle(self, *args, **options):
  12. file = options['file']
  13. extension = '.json'
  14. if file:
  15. with open(file) as json_data:
  16. content = json.load(json_data) #, encoding='utf-8'
  17. content = content[0]
  18. sections = []
  19. for key, section in content.items():
  20. if "type" in section and section["type"]=="info":
  21. sections.append({
  22. **section,
  23. 'id': key,
  24. 'type': 'help',
  25. 'category': key,
  26. '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).'
  27. })
  28. for section in sections:
  29. filename = 'help-' + section['id']
  30. json_data = json.dumps(section, ensure_ascii=False)
  31. temp_dir = tempfile.gettempdir()
  32. file_path = "%s%s%s%s" % (temp_dir, os.sep, filename, extension)
  33. with open(file_path, 'w') as f:
  34. f.write(json_data)
  35. media_storage = MediaStorage(CTS.CHATBOT_STORAGE_PATH)
  36. path_bucket = os.path.join(media_storage.location, 'docs/', filename + extension)
  37. if media_storage.exists(path_bucket):
  38. media_storage.delete(path_bucket)
  39. media_storage.save(path_bucket, BytesIO(json_data.encode("utf-8")))