#7 Dodati su export/import za template, isto tako template za document
This commit is contained in:
22
backend/core/management/commands/export_template.py
Normal file
22
backend/core/management/commands/export_template.py
Normal file
@@ -0,0 +1,22 @@
|
||||
import yaml
|
||||
from django.core.management.base import BaseCommand
|
||||
from backend.core.models import DocumentTemplate
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = "Export document template to a YAML file"
|
||||
|
||||
def add_arguments(self, parser):
|
||||
parser.add_argument('yaml_file', type=str, help="The YAML file to export template to")
|
||||
|
||||
def handle(self, *args, **options):
|
||||
yaml_file_path = options['yaml_file']
|
||||
|
||||
template = DocumentTemplate.objects.first()
|
||||
|
||||
if template:
|
||||
with open(yaml_file_path, 'w') as yaml_file:
|
||||
yaml_file.write(template.content)
|
||||
|
||||
self.stdout.write(self.style.SUCCESS('Template exported successfully'))
|
||||
else:
|
||||
self.stdout.write(self.style.ERROR('No template found in the database'))
|
||||
23
backend/core/management/commands/import_template.py
Normal file
23
backend/core/management/commands/import_template.py
Normal file
@@ -0,0 +1,23 @@
|
||||
import yaml
|
||||
from django.core.management.base import BaseCommand
|
||||
from backend.core.models import DocumentTemplate
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = "Import document template from a YAML file"
|
||||
|
||||
def add_arguments(self, parser):
|
||||
parser.add_argument('yaml_file', type=str, help="YAML file to import template from")
|
||||
|
||||
def handle(self, *args, **options):
|
||||
yaml_file_path = options['yaml_file']
|
||||
|
||||
with open(yaml_file_path, 'r') as file:
|
||||
content = file.read()
|
||||
yaml_data = yaml.safe_load(content)
|
||||
|
||||
DocumentTemplate.objects.update_or_create(
|
||||
name="Default Template",
|
||||
defaults={"content": content}
|
||||
)
|
||||
|
||||
self.stdout.write(self.style.SUCCESS("Template imported successfully."))
|
||||
@@ -0,0 +1,34 @@
|
||||
import os
|
||||
from io import StringIO
|
||||
from django.core.management import call_command
|
||||
from django.test import TestCase
|
||||
from backend.core.models import DocumentTemplate
|
||||
|
||||
|
||||
class DocumentTemplateExportCommandTest(TestCase):
|
||||
def setUp(self):
|
||||
self.template_content = """
|
||||
- segment_type: "title"
|
||||
content: "Document Title"
|
||||
- segment_type: "subtitle"
|
||||
content: "Document Subtitle"
|
||||
"""
|
||||
self.template = DocumentTemplate.objects.create(
|
||||
name="Default Template",
|
||||
content=self.template_content
|
||||
)
|
||||
self.export_file = 'exported_template.yaml'
|
||||
|
||||
def tearDown(self):
|
||||
if os.path.exists(self.export_file):
|
||||
os.remove(self.export_file)
|
||||
|
||||
def test_export_template(self):
|
||||
out = StringIO()
|
||||
call_command('export_template', self.export_file, stdout=out)
|
||||
|
||||
self.assertIn("Template exported successfully", out.getvalue())
|
||||
|
||||
with open(self.export_file, 'r') as f:
|
||||
content = f.read()
|
||||
self.assertEqual(content.strip(), self.template_content.strip())
|
||||
@@ -0,0 +1,31 @@
|
||||
import os
|
||||
from io import StringIO
|
||||
from django.core.management import call_command
|
||||
from django.test import TestCase
|
||||
from backend.core.models import DocumentTemplate
|
||||
|
||||
|
||||
class DocumentTemplateImportCommandTest(TestCase):
|
||||
def setUp(self):
|
||||
self.yaml_content = """
|
||||
- segment_type: "title"
|
||||
content: "Document Title"
|
||||
- segment_type: "subtitle"
|
||||
content: "Document Subtitle"
|
||||
"""
|
||||
self.yaml_file = 'test_template.yaml'
|
||||
with open(self.yaml_file, 'w') as f:
|
||||
f.write(self.yaml_content)
|
||||
|
||||
def tearDown(self):
|
||||
if os.path.exists(self.yaml_file):
|
||||
os.remove(self.yaml_file)
|
||||
|
||||
def test_import_template(self):
|
||||
out = StringIO()
|
||||
call_command('import_template', self.yaml_file, stdout=out)
|
||||
|
||||
self.assertIn("Template imported successfully", out.getvalue())
|
||||
|
||||
template = DocumentTemplate.objects.get(name="Default Template")
|
||||
self.assertEqual(template.content.strip(), self.yaml_content.strip())
|
||||
Reference in New Issue
Block a user