refactoring of deal documents
This commit is contained in:
@@ -7,18 +7,18 @@ from sqlalchemy.orm import selectinload, joinedload
|
||||
from weasyprint import HTML, CSS
|
||||
|
||||
from constants import DEAL_STATUS_STR, ENV, APP_PATH
|
||||
from generators.deal_pdf_generator.deal_data import DocumentDealProductData
|
||||
from generators.deal_pdf_generator.deal_data import DealTechSpecProductData, DealTechSpecData
|
||||
from models import Deal, DealProduct, DealService as DealServiceModel, Product
|
||||
from utils.images_fetcher import fetch_images
|
||||
|
||||
|
||||
class DealPdfGenerator:
|
||||
class DealTechSpecPdfGenerator:
|
||||
def __init__(self, session: AsyncSession):
|
||||
self._session = session
|
||||
|
||||
@staticmethod
|
||||
async def _group_deal_products_by_products(deal_products: List[DealProduct]) -> Dict[str, DocumentDealProductData]:
|
||||
products: Dict[str, DocumentDealProductData] = {}
|
||||
async def _group_deal_products_by_products(deal_products: List[DealProduct]) -> Dict[str, DealTechSpecProductData]:
|
||||
products: Dict[str, DealTechSpecProductData] = {}
|
||||
additional_info: Optional[str]
|
||||
|
||||
for deal_product in deal_products:
|
||||
@@ -41,7 +41,7 @@ class DealPdfGenerator:
|
||||
|
||||
return products
|
||||
|
||||
async def _create_detailed_deal_document_html(self, deal_id: int):
|
||||
async def _get_deal_by_id(self, deal_id: int) -> Optional[Deal]:
|
||||
deal: Deal | None = await self._session.scalar(
|
||||
select(Deal)
|
||||
.where(Deal.id == deal_id)
|
||||
@@ -54,6 +54,10 @@ class DealPdfGenerator:
|
||||
joinedload(Deal.shipping_warehouse),
|
||||
)
|
||||
)
|
||||
return deal
|
||||
|
||||
async def _create_deal_tech_spec_document_html(self, deal_id: int):
|
||||
deal = await self._get_deal_by_id(deal_id)
|
||||
|
||||
if not deal:
|
||||
return ""
|
||||
@@ -68,7 +72,7 @@ class DealPdfGenerator:
|
||||
product_urls.append(None)
|
||||
product_images = await fetch_images(product_urls)
|
||||
|
||||
document_deal_data = {
|
||||
document_deal_data: DealTechSpecData = {
|
||||
"deal": deal,
|
||||
"products": products,
|
||||
"current_status_str": DEAL_STATUS_STR[deal.current_status],
|
||||
@@ -76,13 +80,13 @@ class DealPdfGenerator:
|
||||
"product_images": product_images,
|
||||
}
|
||||
|
||||
template = ENV.get_template("deal/deal.html")
|
||||
template = ENV.get_template("deal/deal-tech-spec.html")
|
||||
|
||||
result = template.render({"data": document_deal_data, "sign_place_text": "_" * 22})
|
||||
return result
|
||||
|
||||
async def create_detailed_deal_document_pdf(self, deal_id) -> BytesIO:
|
||||
doc = await self._create_detailed_deal_document_html(deal_id)
|
||||
async def create_deal_tech_spec_pdf(self, deal_id) -> BytesIO:
|
||||
doc = await self._create_deal_tech_spec_document_html(deal_id)
|
||||
pdf_file = BytesIO()
|
||||
HTML(string=doc).write_pdf(pdf_file, stylesheets=[CSS(APP_PATH + '/static/css/deal.css')])
|
||||
HTML(string=doc).write_pdf(pdf_file, stylesheets=[CSS(APP_PATH + '/static/css/deal-tech-spec.css')])
|
||||
return pdf_file
|
||||
|
||||
Reference in New Issue
Block a user