106 lines
2.9 KiB
Python
106 lines
2.9 KiB
Python
from decimal import Decimal
|
|
|
|
from app.domain.enums import (
|
|
Availability,
|
|
NormalizationStatus,
|
|
OfferCondition,
|
|
ValueType,
|
|
)
|
|
from app.domain.models import (
|
|
AttributeDefinition,
|
|
AttributeStatement,
|
|
Offer,
|
|
PriceObservation,
|
|
Product,
|
|
ProductVariant,
|
|
)
|
|
from app.persistence import models as orm
|
|
|
|
|
|
def decimal_or_none(value) -> Decimal | None:
|
|
return None if value is None else Decimal(str(value))
|
|
|
|
|
|
def to_definition(row: orm.AttributeDefinitionRecord) -> AttributeDefinition:
|
|
return AttributeDefinition(
|
|
key=row.key,
|
|
value_type=ValueType(row.value_type),
|
|
canonical_unit=row.canonical_unit,
|
|
minimum=decimal_or_none(row.minimum),
|
|
maximum=decimal_or_none(row.maximum),
|
|
description=row.description,
|
|
)
|
|
|
|
|
|
def to_product(row: orm.ProductRecord) -> Product:
|
|
return Product(
|
|
id=row.id,
|
|
category=row.category,
|
|
brand=row.brand,
|
|
name=row.name,
|
|
model=row.model,
|
|
created_at=row.created_at,
|
|
)
|
|
|
|
|
|
def to_variant(row: orm.ProductVariantRecord) -> ProductVariant:
|
|
return ProductVariant(
|
|
id=row.id,
|
|
product_id=row.product_id,
|
|
name=row.name,
|
|
manufacturer_sku=row.manufacturer_sku,
|
|
identifiers=row.identifiers or {},
|
|
created_at=row.created_at,
|
|
)
|
|
|
|
|
|
def to_attribute(row: orm.VariantAttributeRecord) -> AttributeStatement:
|
|
return AttributeStatement(
|
|
id=row.id,
|
|
variant_id=row.variant_id,
|
|
attribute=row.attribute_key,
|
|
raw_value=row.raw_value,
|
|
normalized_value=row.normalized_value,
|
|
unit=row.unit,
|
|
normalization_status=NormalizationStatus(row.normalization_status),
|
|
normalizer_version=row.normalizer_version,
|
|
confidence=Decimal(str(row.confidence)),
|
|
source_id=row.source_id,
|
|
source_reference=row.source_reference,
|
|
observed_at=row.observed_at,
|
|
ingested_at=row.ingested_at,
|
|
normalization_error=row.normalization_error,
|
|
)
|
|
|
|
|
|
def to_offer(row: orm.OfferRecord) -> Offer:
|
|
return Offer(
|
|
id=row.id,
|
|
variant_id=row.variant_id,
|
|
source_id=row.source_id,
|
|
external_id=row.external_id,
|
|
seller=row.seller,
|
|
url=row.url,
|
|
market=row.market,
|
|
currency=row.currency,
|
|
condition=OfferCondition(row.condition),
|
|
created_at=row.created_at,
|
|
)
|
|
|
|
|
|
def to_observation(row: orm.PriceObservationRecord) -> PriceObservation:
|
|
return PriceObservation(
|
|
id=row.id,
|
|
offer_id=row.offer_id,
|
|
source_id=row.source_id,
|
|
external_observation_id=row.external_observation_id,
|
|
observed_at=row.observed_at,
|
|
ingested_at=row.ingested_at,
|
|
item_price=Decimal(str(row.item_price)),
|
|
shipping_price=decimal_or_none(row.shipping_price),
|
|
currency=row.currency,
|
|
availability=Availability(row.availability),
|
|
raw_payload=row.raw_payload,
|
|
confidence=Decimal(str(row.confidence)),
|
|
)
|