feat: temp barcode templates
This commit is contained in:
0
barcodes/__init__.py
Normal file
0
barcodes/__init__.py
Normal file
21
barcodes/attributes/__init__.py
Normal file
21
barcodes/attributes/__init__.py
Normal file
@@ -0,0 +1,21 @@
|
||||
from abc import abstractmethod, ABC
|
||||
|
||||
from .article_attribute_writer import ArticleAttributeWriter
|
||||
from .name_attribute_writer import NameAttributeWriter
|
||||
from ..barcode import Barcode
|
||||
|
||||
|
||||
class BaseAttributeWriter(ABC):
|
||||
@abstractmethod
|
||||
def write(self, barcode: Barcode):
|
||||
pass
|
||||
|
||||
|
||||
class AttributeWriterFactory:
|
||||
@staticmethod
|
||||
def get_writer(key: str):
|
||||
match key:
|
||||
case 'name':
|
||||
return NameAttributeWriter()
|
||||
case 'article':
|
||||
return ArticleAttributeWriter()
|
||||
7
barcodes/attributes/article_attribute_writer.py
Normal file
7
barcodes/attributes/article_attribute_writer.py
Normal file
@@ -0,0 +1,7 @@
|
||||
from barcodes.attributes import BaseAttributeWriter
|
||||
from barcodes.barcode import Barcode
|
||||
|
||||
|
||||
class ArticleAttributeWriter(BaseAttributeWriter):
|
||||
def write(self, barcode: Barcode):
|
||||
pass
|
||||
7
barcodes/attributes/name_attribute_writer.py
Normal file
7
barcodes/attributes/name_attribute_writer.py
Normal file
@@ -0,0 +1,7 @@
|
||||
from barcodes.attributes import BaseAttributeWriter
|
||||
from barcodes.barcode import Barcode
|
||||
|
||||
|
||||
class NameAttributeWriter(BaseAttributeWriter):
|
||||
def write(self, barcode: Barcode):
|
||||
pass
|
||||
15
barcodes/barcode.py
Normal file
15
barcodes/barcode.py
Normal file
@@ -0,0 +1,15 @@
|
||||
from .attributes import AttributeWriterFactory
|
||||
from models import ProductBarcode, BarcodeTemplate, BarcodeTemplateAttribute
|
||||
|
||||
|
||||
class Barcode:
|
||||
def __init__(self, session, barcode: ProductBarcode):
|
||||
self.session = session
|
||||
self.barcode = barcode
|
||||
|
||||
def render(self, template: BarcodeTemplate):
|
||||
for attribute in template.attributes:
|
||||
attribute: BarcodeTemplateAttribute
|
||||
writer = AttributeWriterFactory.get_writer(attribute.key)
|
||||
writer.write(self)
|
||||
|
||||
6
barcodes/generator.py
Normal file
6
barcodes/generator.py
Normal file
@@ -0,0 +1,6 @@
|
||||
from models import ProductBarcode
|
||||
|
||||
|
||||
class BarcodeGenerator:
|
||||
def __init__(self, barcode: ProductBarcode):
|
||||
self.barcode = barcode
|
||||
@@ -7,8 +7,8 @@ from models import BaseModel
|
||||
class BarcodeTemplateAttribute(BaseModel):
|
||||
__tablename__ = 'barcode_template_attributes'
|
||||
id = Column(Integer, autoincrement=True, primary_key=True, index=True)
|
||||
name = Column(String, nullable=False, index=True, comment='Название атрибута')
|
||||
label = Column(String, nullable=False, index=True, comment='Метка атрибута')
|
||||
key = Column(String, nullable=False, index=True, comment='Ключ атрибута')
|
||||
name = Column(String, nullable=False, index=True, comment='Метка атрибута')
|
||||
|
||||
|
||||
class BarcodeTemplate(BaseModel):
|
||||
@@ -20,3 +20,7 @@ class BarcodeTemplate(BaseModel):
|
||||
cascade="all, delete-orphan",
|
||||
lazy='joined')
|
||||
is_default = Column(Boolean, nullable=False, default=False, comment='По умолчанию')
|
||||
|
||||
# size
|
||||
width = Column(Integer, nullable=False, comment='Ширина в мм')
|
||||
height = Column(Integer, nullable=False, comment='Высота в мм')
|
||||
|
||||
@@ -9,7 +9,8 @@ from schemas.barcode import (GetBarcodeTemplateByIdResponse,
|
||||
BarcodeTemplateCreateResponse,
|
||||
BarcodeTemplateCreateRequest, GetAllBarcodeTemplateAttributesResponse,
|
||||
CreateBarcodeTemplateAttributeResponse, CreateBarcodeTemplateAttributeRequest,
|
||||
BarcodeTemplateUpdateResponse, BarcodeTemplateUpdateRequest)
|
||||
BarcodeTemplateUpdateResponse, BarcodeTemplateUpdateRequest,
|
||||
GetAllBarcodeTemplatesResponse)
|
||||
from services.barcode import BarcodeService
|
||||
|
||||
barcode_router = APIRouter(
|
||||
@@ -31,6 +32,17 @@ async def get_barcode_template_by_id(
|
||||
return await BarcodeService(session).get_barcode_template_by_id(request)
|
||||
|
||||
|
||||
@barcode_router.get(
|
||||
'/template/get-all',
|
||||
response_model=GetAllBarcodeTemplatesResponse,
|
||||
operation_id='get_all_barcode_templates'
|
||||
)
|
||||
async def get_all_barcode_templates(
|
||||
session: Annotated[AsyncSession, Depends(get_session)]
|
||||
):
|
||||
return await BarcodeService(session).get_all_barcode_templates()
|
||||
|
||||
|
||||
@barcode_router.post(
|
||||
'/template/create',
|
||||
response_model=BarcodeTemplateCreateResponse,
|
||||
|
||||
@@ -2,17 +2,22 @@ from schemas.base import CustomModelCamel, OkMessageSchema
|
||||
|
||||
|
||||
# region Entities
|
||||
class BarcodeTemplateAttribute(CustomModelCamel):
|
||||
class BarcodeTemplateAttributeSchema(CustomModelCamel):
|
||||
id: int
|
||||
label: str
|
||||
key: str
|
||||
name: str
|
||||
|
||||
|
||||
class BarcodeTemplate(CustomModelCamel):
|
||||
id: int
|
||||
class BaseBarcodeTemplateSchema(CustomModelCamel):
|
||||
name: str
|
||||
is_default: bool
|
||||
attributes: list[BarcodeTemplateAttribute]
|
||||
width: int
|
||||
height: int
|
||||
|
||||
|
||||
class BarcodeTemplateSchema(BaseBarcodeTemplateSchema):
|
||||
id: int
|
||||
attributes: list[BarcodeTemplateAttributeSchema]
|
||||
|
||||
|
||||
# endregion
|
||||
@@ -30,10 +35,6 @@ class BarcodeTemplateUpdateResponse(OkMessageSchema):
|
||||
pass
|
||||
|
||||
|
||||
class GetAllBarcodeTemplateAttributesResponse(CustomModelCamel):
|
||||
attributes: list[BarcodeTemplateAttribute]
|
||||
|
||||
|
||||
class CreateBarcodeTemplateAttributeRequest(CustomModelCamel):
|
||||
name: str
|
||||
label: str
|
||||
@@ -43,22 +44,26 @@ class CreateBarcodeTemplateAttributeRequest(CustomModelCamel):
|
||||
|
||||
# region Responses
|
||||
class GetBarcodeTemplateByIdResponse(CustomModelCamel):
|
||||
barcode_template: BarcodeTemplate
|
||||
barcode_template: BarcodeTemplateSchema
|
||||
|
||||
|
||||
class BarcodeTemplateCreateRequest(CustomModelCamel):
|
||||
name: str
|
||||
class BarcodeTemplateCreateRequest(BaseBarcodeTemplateSchema):
|
||||
attribute_ids: list[int]
|
||||
is_default: bool
|
||||
|
||||
|
||||
class BarcodeTemplateUpdateRequest(CustomModelCamel):
|
||||
class BarcodeTemplateUpdateRequest(BaseBarcodeTemplateSchema):
|
||||
id: int
|
||||
name: str
|
||||
is_default: bool
|
||||
attribute_ids: list[int]
|
||||
|
||||
|
||||
class CreateBarcodeTemplateAttributeResponse(OkMessageSchema):
|
||||
id: int
|
||||
|
||||
|
||||
class GetAllBarcodeTemplatesResponse(CustomModelCamel):
|
||||
templates: list[BarcodeTemplateSchema]
|
||||
|
||||
|
||||
class GetAllBarcodeTemplateAttributesResponse(CustomModelCamel):
|
||||
attributes: list[BarcodeTemplateAttributeSchema]
|
||||
# endregion
|
||||
|
||||
@@ -5,7 +5,9 @@ from schemas.barcode import (GetBarcodeTemplateByIdRequest,
|
||||
GetBarcodeTemplateByIdResponse,
|
||||
BarcodeTemplateCreateResponse,
|
||||
BarcodeTemplateCreateRequest, CreateBarcodeTemplateAttributeRequest,
|
||||
BarcodeTemplateUpdateResponse, BarcodeTemplateUpdateRequest)
|
||||
BarcodeTemplateUpdateResponse, BarcodeTemplateUpdateRequest,
|
||||
BarcodeTemplateAttributeSchema, GetAllBarcodeTemplateAttributesResponse,
|
||||
GetAllBarcodeTemplatesResponse)
|
||||
from services.base import BaseService
|
||||
|
||||
|
||||
@@ -20,6 +22,12 @@ class BarcodeService(BaseService):
|
||||
)
|
||||
return query.scalar()
|
||||
|
||||
async def get_all_barcode_templates(self) -> GetAllBarcodeTemplatesResponse:
|
||||
stmt = select(BarcodeTemplate).order_by(BarcodeTemplate.id)
|
||||
query = await self.session.execute(stmt)
|
||||
templates = query.scalars().all()
|
||||
return GetAllBarcodeTemplatesResponse(templates=templates)
|
||||
|
||||
async def create_barcode_template(self, request: BarcodeTemplateCreateRequest) -> BarcodeTemplateCreateResponse:
|
||||
try:
|
||||
if request.is_default:
|
||||
@@ -120,10 +128,11 @@ class BarcodeService(BaseService):
|
||||
# endregion
|
||||
|
||||
# region Template attributes
|
||||
async def get_all_barcode_template_attributes(self):
|
||||
stmt = select(BarcodeTemplateAttribute)
|
||||
async def get_all_barcode_template_attributes(self) -> GetAllBarcodeTemplateAttributesResponse:
|
||||
stmt = select(BarcodeTemplateAttribute).order_by(BarcodeTemplateAttribute.id)
|
||||
query = await self.session.execute(stmt)
|
||||
return query.scalars().all()
|
||||
attributes = query.scalars().all()
|
||||
return GetAllBarcodeTemplateAttributesResponse(attributes=attributes)
|
||||
|
||||
async def create_barcode_template_attribute(self, request: CreateBarcodeTemplateAttributeRequest):
|
||||
try:
|
||||
|
||||
Reference in New Issue
Block a user