initial doc generator works now
rodzic
cabc144810
commit
0cfba0602b
Plik binarny nie jest wyświetlany.
|
@ -90,7 +90,6 @@ WSGI_APPLICATION = "artel.wsgi.application"
|
|||
# https://docs.djangoproject.com/en/4.1/ref/settings/#databases
|
||||
import dj_database_url as db_url
|
||||
|
||||
|
||||
DATABASES = {
|
||||
"default": db_url.parse(
|
||||
os.environ.get("DATABASE_URL", "postgres://comfy:password@db/comfy_shop")
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
from django.contrib import admin
|
||||
|
||||
# Register your models here.
|
|
@ -0,0 +1,6 @@
|
|||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class DocgeneratorConfig(AppConfig):
|
||||
default_auto_field = 'django.db.models.BigAutoField'
|
||||
name = 'docgenerator'
|
|
@ -0,0 +1,44 @@
|
|||
from abc import (
|
||||
ABC,
|
||||
abstractmethod
|
||||
)
|
||||
from typing import (
|
||||
Dict,
|
||||
Any
|
||||
)
|
||||
|
||||
from django.db.models import Model
|
||||
from docxtpl import DocxTemplate
|
||||
|
||||
|
||||
class DocumentGeneratorInterface(ABC):
|
||||
@abstractmethod
|
||||
def load_template(self, path: str):
|
||||
...
|
||||
|
||||
@abstractmethod
|
||||
def get_extra_context(self) -> Dict[str, Any]:
|
||||
...
|
||||
|
||||
@abstractmethod
|
||||
def generate_file(self, context: Dict[str, Any] = None):
|
||||
...
|
||||
|
||||
|
||||
class BaseDocumentGenerator(DocumentGeneratorInterface):
|
||||
|
||||
def __init__(self, instance: Model) -> None:
|
||||
super().__init__()
|
||||
self.instance = instance
|
||||
|
||||
def load_template(self, path: str):
|
||||
return DocxTemplate(path)
|
||||
|
||||
def get_extra_context(self):
|
||||
return {}
|
||||
|
||||
|
||||
class PdfFromDocGenerator(BaseDocumentGenerator):
|
||||
def generate_file(self, context: Dict[str, Any] = None):
|
||||
template = self.load_template()
|
||||
context.update(self.get_extra_context())
|
|
@ -0,0 +1,10 @@
|
|||
from django.db import models
|
||||
from django.core.files.storage import Storage
|
||||
|
||||
|
||||
class DocumentTemplate(models.Model):
|
||||
name = models.CharField(max_length=255)
|
||||
file = models.FileField(upload_to="doc_templates/", )
|
||||
|
||||
def __str__(self) -> str:
|
||||
return self.name
|
|
@ -0,0 +1,5 @@
|
|||
from django.test import TestCase
|
||||
|
||||
|
||||
class PdfFromDocGeneratorTestCase(TestCase):
|
||||
...
|
|
@ -0,0 +1,3 @@
|
|||
from django.shortcuts import render
|
||||
|
||||
# Create your views here.
|
Ładowanie…
Reference in New Issue