json_export.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. import json
  2. from src.convert.converter_factory import converter_factory
  3. from src.export.strategies.export import export
  4. from src.models.abstract_reference import abstract_reference
  5. class json_export(export):
  6. """Класс стратегии для экспорта в JSON"""
  7. def export_header(self, model: abstract_reference):
  8. """
  9. Создать заголовок экспорта
  10. Args:
  11. model (abstract_reference): модель, по которой нужно построить заголовок
  12. """
  13. return ""
  14. def export_model(self, model: abstract_reference):
  15. """
  16. Экспортировать модель
  17. Args:
  18. model (abstract_reference): модель, строку с которой нужно создать
  19. """
  20. data = converter_factory.convert(model)
  21. return json.dumps(data, indent=4) + ","
  22. def postprocess(self, text):
  23. """Пост-обработка текста перед экспортом (по надобности)"""
  24. return f'{{\n "items": [{text[:-1]}\n ]\n}}'
  25. @property
  26. def mimetype(self):
  27. return "application/json"