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):
|
class BarcodeTemplateAttribute(BaseModel):
|
||||||
__tablename__ = 'barcode_template_attributes'
|
__tablename__ = 'barcode_template_attributes'
|
||||||
id = Column(Integer, autoincrement=True, primary_key=True, index=True)
|
id = Column(Integer, autoincrement=True, primary_key=True, index=True)
|
||||||
name = Column(String, nullable=False, index=True, comment='Название атрибута')
|
key = Column(String, nullable=False, index=True, comment='Ключ атрибута')
|
||||||
label = Column(String, nullable=False, index=True, comment='Метка атрибута')
|
name = Column(String, nullable=False, index=True, comment='Метка атрибута')
|
||||||
|
|
||||||
|
|
||||||
class BarcodeTemplate(BaseModel):
|
class BarcodeTemplate(BaseModel):
|
||||||
@@ -20,3 +20,7 @@ class BarcodeTemplate(BaseModel):
|
|||||||
cascade="all, delete-orphan",
|
cascade="all, delete-orphan",
|
||||||
lazy='joined')
|
lazy='joined')
|
||||||
is_default = Column(Boolean, nullable=False, default=False, comment='По умолчанию')
|
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,
|
BarcodeTemplateCreateResponse,
|
||||||
BarcodeTemplateCreateRequest, GetAllBarcodeTemplateAttributesResponse,
|
BarcodeTemplateCreateRequest, GetAllBarcodeTemplateAttributesResponse,
|
||||||
CreateBarcodeTemplateAttributeResponse, CreateBarcodeTemplateAttributeRequest,
|
CreateBarcodeTemplateAttributeResponse, CreateBarcodeTemplateAttributeRequest,
|
||||||
BarcodeTemplateUpdateResponse, BarcodeTemplateUpdateRequest)
|
BarcodeTemplateUpdateResponse, BarcodeTemplateUpdateRequest,
|
||||||
|
GetAllBarcodeTemplatesResponse)
|
||||||
from services.barcode import BarcodeService
|
from services.barcode import BarcodeService
|
||||||
|
|
||||||
barcode_router = APIRouter(
|
barcode_router = APIRouter(
|
||||||
@@ -31,6 +32,17 @@ async def get_barcode_template_by_id(
|
|||||||
return await BarcodeService(session).get_barcode_template_by_id(request)
|
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(
|
@barcode_router.post(
|
||||||
'/template/create',
|
'/template/create',
|
||||||
response_model=BarcodeTemplateCreateResponse,
|
response_model=BarcodeTemplateCreateResponse,
|
||||||
|
|||||||
@@ -2,17 +2,22 @@ from schemas.base import CustomModelCamel, OkMessageSchema
|
|||||||
|
|
||||||
|
|
||||||
# region Entities
|
# region Entities
|
||||||
class BarcodeTemplateAttribute(CustomModelCamel):
|
class BarcodeTemplateAttributeSchema(CustomModelCamel):
|
||||||
id: int
|
id: int
|
||||||
label: str
|
key: str
|
||||||
name: str
|
name: str
|
||||||
|
|
||||||
|
|
||||||
class BarcodeTemplate(CustomModelCamel):
|
class BaseBarcodeTemplateSchema(CustomModelCamel):
|
||||||
id: int
|
|
||||||
name: str
|
name: str
|
||||||
is_default: bool
|
is_default: bool
|
||||||
attributes: list[BarcodeTemplateAttribute]
|
width: int
|
||||||
|
height: int
|
||||||
|
|
||||||
|
|
||||||
|
class BarcodeTemplateSchema(BaseBarcodeTemplateSchema):
|
||||||
|
id: int
|
||||||
|
attributes: list[BarcodeTemplateAttributeSchema]
|
||||||
|
|
||||||
|
|
||||||
# endregion
|
# endregion
|
||||||
@@ -30,10 +35,6 @@ class BarcodeTemplateUpdateResponse(OkMessageSchema):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
class GetAllBarcodeTemplateAttributesResponse(CustomModelCamel):
|
|
||||||
attributes: list[BarcodeTemplateAttribute]
|
|
||||||
|
|
||||||
|
|
||||||
class CreateBarcodeTemplateAttributeRequest(CustomModelCamel):
|
class CreateBarcodeTemplateAttributeRequest(CustomModelCamel):
|
||||||
name: str
|
name: str
|
||||||
label: str
|
label: str
|
||||||
@@ -43,22 +44,26 @@ class CreateBarcodeTemplateAttributeRequest(CustomModelCamel):
|
|||||||
|
|
||||||
# region Responses
|
# region Responses
|
||||||
class GetBarcodeTemplateByIdResponse(CustomModelCamel):
|
class GetBarcodeTemplateByIdResponse(CustomModelCamel):
|
||||||
barcode_template: BarcodeTemplate
|
barcode_template: BarcodeTemplateSchema
|
||||||
|
|
||||||
|
|
||||||
class BarcodeTemplateCreateRequest(CustomModelCamel):
|
class BarcodeTemplateCreateRequest(BaseBarcodeTemplateSchema):
|
||||||
name: str
|
|
||||||
attribute_ids: list[int]
|
attribute_ids: list[int]
|
||||||
is_default: bool
|
|
||||||
|
|
||||||
|
|
||||||
class BarcodeTemplateUpdateRequest(CustomModelCamel):
|
class BarcodeTemplateUpdateRequest(BaseBarcodeTemplateSchema):
|
||||||
id: int
|
id: int
|
||||||
name: str
|
|
||||||
is_default: bool
|
|
||||||
attribute_ids: list[int]
|
attribute_ids: list[int]
|
||||||
|
|
||||||
|
|
||||||
class CreateBarcodeTemplateAttributeResponse(OkMessageSchema):
|
class CreateBarcodeTemplateAttributeResponse(OkMessageSchema):
|
||||||
id: int
|
id: int
|
||||||
|
|
||||||
|
|
||||||
|
class GetAllBarcodeTemplatesResponse(CustomModelCamel):
|
||||||
|
templates: list[BarcodeTemplateSchema]
|
||||||
|
|
||||||
|
|
||||||
|
class GetAllBarcodeTemplateAttributesResponse(CustomModelCamel):
|
||||||
|
attributes: list[BarcodeTemplateAttributeSchema]
|
||||||
# endregion
|
# endregion
|
||||||
|
|||||||
@@ -5,7 +5,9 @@ from schemas.barcode import (GetBarcodeTemplateByIdRequest,
|
|||||||
GetBarcodeTemplateByIdResponse,
|
GetBarcodeTemplateByIdResponse,
|
||||||
BarcodeTemplateCreateResponse,
|
BarcodeTemplateCreateResponse,
|
||||||
BarcodeTemplateCreateRequest, CreateBarcodeTemplateAttributeRequest,
|
BarcodeTemplateCreateRequest, CreateBarcodeTemplateAttributeRequest,
|
||||||
BarcodeTemplateUpdateResponse, BarcodeTemplateUpdateRequest)
|
BarcodeTemplateUpdateResponse, BarcodeTemplateUpdateRequest,
|
||||||
|
BarcodeTemplateAttributeSchema, GetAllBarcodeTemplateAttributesResponse,
|
||||||
|
GetAllBarcodeTemplatesResponse)
|
||||||
from services.base import BaseService
|
from services.base import BaseService
|
||||||
|
|
||||||
|
|
||||||
@@ -20,6 +22,12 @@ class BarcodeService(BaseService):
|
|||||||
)
|
)
|
||||||
return query.scalar()
|
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:
|
async def create_barcode_template(self, request: BarcodeTemplateCreateRequest) -> BarcodeTemplateCreateResponse:
|
||||||
try:
|
try:
|
||||||
if request.is_default:
|
if request.is_default:
|
||||||
@@ -120,10 +128,11 @@ class BarcodeService(BaseService):
|
|||||||
# endregion
|
# endregion
|
||||||
|
|
||||||
# region Template attributes
|
# region Template attributes
|
||||||
async def get_all_barcode_template_attributes(self):
|
async def get_all_barcode_template_attributes(self) -> GetAllBarcodeTemplateAttributesResponse:
|
||||||
stmt = select(BarcodeTemplateAttribute)
|
stmt = select(BarcodeTemplateAttribute).order_by(BarcodeTemplateAttribute.id)
|
||||||
query = await self.session.execute(stmt)
|
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):
|
async def create_barcode_template_attribute(self, request: CreateBarcodeTemplateAttributeRequest):
|
||||||
try:
|
try:
|
||||||
|
|||||||
Reference in New Issue
Block a user