refactoring of deal documents

This commit is contained in:
2024-11-09 17:09:17 +04:00
parent b1034d437e
commit 6890d6b79c
5 changed files with 25 additions and 22 deletions

View File

@@ -3,17 +3,16 @@ from typing import TypedDict, List, Dict, Tuple, Optional
from models import DealProduct, Deal, DealStatusHistory from models import DealProduct, Deal, DealStatusHistory
class DocumentDealProductData(TypedDict): class DealTechSpecProductData(TypedDict):
deal_products: List[DealProduct] deal_products: List[DealProduct]
total_one_product: int total_one_product: int
quantity: int quantity: int
additional_info: Optional[str] additional_info: Optional[str]
class DocumentDealData(TypedDict): class DealTechSpecData(TypedDict):
deal: Deal deal: Deal
general_services_total: int products: Dict[str, DealTechSpecProductData]
products: Dict[str, DocumentDealProductData]
current_status_str: str current_status_str: str
last_status: DealStatusHistory last_status: DealStatusHistory
product_images: Tuple[str] product_images: Tuple[str]

View File

@@ -7,18 +7,18 @@ from sqlalchemy.orm import selectinload, joinedload
from weasyprint import HTML, CSS from weasyprint import HTML, CSS
from constants import DEAL_STATUS_STR, ENV, APP_PATH 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 models import Deal, DealProduct, DealService as DealServiceModel, Product
from utils.images_fetcher import fetch_images from utils.images_fetcher import fetch_images
class DealPdfGenerator: class DealTechSpecPdfGenerator:
def __init__(self, session: AsyncSession): def __init__(self, session: AsyncSession):
self._session = session self._session = session
@staticmethod @staticmethod
async def _group_deal_products_by_products(deal_products: List[DealProduct]) -> Dict[str, DocumentDealProductData]: async def _group_deal_products_by_products(deal_products: List[DealProduct]) -> Dict[str, DealTechSpecProductData]:
products: Dict[str, DocumentDealProductData] = {} products: Dict[str, DealTechSpecProductData] = {}
additional_info: Optional[str] additional_info: Optional[str]
for deal_product in deal_products: for deal_product in deal_products:
@@ -41,7 +41,7 @@ class DealPdfGenerator:
return products 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( deal: Deal | None = await self._session.scalar(
select(Deal) select(Deal)
.where(Deal.id == deal_id) .where(Deal.id == deal_id)
@@ -54,6 +54,10 @@ class DealPdfGenerator:
joinedload(Deal.shipping_warehouse), 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: if not deal:
return "" return ""
@@ -68,7 +72,7 @@ class DealPdfGenerator:
product_urls.append(None) product_urls.append(None)
product_images = await fetch_images(product_urls) product_images = await fetch_images(product_urls)
document_deal_data = { document_deal_data: DealTechSpecData = {
"deal": deal, "deal": deal,
"products": products, "products": products,
"current_status_str": DEAL_STATUS_STR[deal.current_status], "current_status_str": DEAL_STATUS_STR[deal.current_status],
@@ -76,13 +80,13 @@ class DealPdfGenerator:
"product_images": product_images, "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}) result = template.render({"data": document_deal_data, "sign_place_text": "_" * 22})
return result return result
async def create_detailed_deal_document_pdf(self, deal_id) -> BytesIO: async def create_deal_tech_spec_pdf(self, deal_id) -> BytesIO:
doc = await self._create_detailed_deal_document_html(deal_id) doc = await self._create_deal_tech_spec_document_html(deal_id)
pdf_file = BytesIO() 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 return pdf_file

View File

@@ -7,7 +7,7 @@ from sqlalchemy.ext.asyncio import AsyncSession
from backend.dependecies import SessionDependency, CurrentUserDependency from backend.dependecies import SessionDependency, CurrentUserDependency
from backend.session import get_session from backend.session import get_session
from generators.deal_pdf_generator.generator import DealPdfGenerator from generators.deal_pdf_generator.generator import DealTechSpecPdfGenerator
from models import User from models import User
from schemas.barcode import GetDealProductsBarcodesPdfRequest, GetDealProductsBarcodesPdfResponse from schemas.barcode import GetDealProductsBarcodesPdfRequest, GetDealProductsBarcodesPdfResponse
from schemas.deal import * from schemas.deal import *
@@ -183,11 +183,11 @@ async def create_guest_url(
@deal_router.get( @deal_router.get(
'/document/{deal_id}', '/billing-document/{deal_id}',
operation_id='get_deal_document', operation_id='get_billing_document',
# dependencies=[Depends(authorized_user)], # dependencies=[Depends(authorized_user)],
) )
async def get_deal_document( async def get_billing_document(
deal_id: int, deal_id: int,
session: Annotated[AsyncSession, Depends(get_session)], session: Annotated[AsyncSession, Depends(get_session)],
): ):
@@ -196,15 +196,15 @@ async def get_deal_document(
@deal_router.get( @deal_router.get(
'/detailedDocument/{deal_id}', '/tech-spec/{deal_id}',
operation_id='get_deal_document_detailed', operation_id='get_deal_tech_spec',
# dependencies=[Depends(authorized_user)], # dependencies=[Depends(authorized_user)],
) )
async def get_detailed_deal_document( async def get_deal_tech_spec(
deal_id: int, deal_id: int,
session: Annotated[AsyncSession, Depends(get_session)], session: Annotated[AsyncSession, Depends(get_session)],
): ):
pdf_file: BytesIO = await DealPdfGenerator(session).create_detailed_deal_document_pdf(deal_id) pdf_file: BytesIO = await DealTechSpecPdfGenerator(session).create_deal_tech_spec_pdf(deal_id)
return Response(pdf_file.getvalue(), media_type='application/pdf') return Response(pdf_file.getvalue(), media_type='application/pdf')