commit 893b229cc6b35c09181a84050f34fb79024e41c2 Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 22:14:08 2025 -0500 j commit 06f539aea2f4fff9da7038d43d0de553c4423796 Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 21:55:30 2025 -0500 fk commit d0c2960ec9f334448d2eb3573b9d7817482abf46 Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 21:50:53 2025 -0500 frick commit 6b1362c166fc5f51c3bcf316a99116f0d11074a5 Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 21:49:40 2025 -0500 database commit 8cadc6df4c817d9d05503807e56287fd00e5e939 Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 21:38:09 2025 -0500 asdf commit 1ca6f9868452e34143b8df4a412be35e6902a31e Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 21:32:50 2025 -0500 fffff commit 8bb337a9c35e830ef9ce3dac0a0f2df3fe9bc5a0 Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 21:31:13 2025 -0500 ffff commit 65aba280c55fa09c6a37f688f485efab1f70792b Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 21:26:16 2025 -0500 aa commit 59ef03a59ee4a15c30e080a1aef7c31c0214a2e3 Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 21:24:21 2025 -0500 asdf commit f44d5740fc9315ccb0792ecac3e8ec9f28f171be Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 21:23:32 2025 -0500 aaa commit 13c96b164316b4908d9d01e454cbdc9103157558 Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 21:18:54 2025 -0500 sdf commit 949c795fd13d93c9618613740fb093f6bb7b7710 Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 21:17:53 2025 -0500 asdf commit 8c3cd423fe228e8aff112a050170246a5fc9f8bd Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 20:56:01 2025 -0500 app2 commit 78eafc739ebb7f100f657964b3ad8f4937a4046b Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 20:54:55 2025 -0500 app commit dc47eced143e77ebec415bdfbe209d9466b7bcf1 Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 20:43:15 2025 -0500 asdfasdfasdf commit e24bcae88cf8c14ea543f49b639b2976c627d201 Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 20:39:44 2025 -0500 a commit c894451bfe790c97ac0e01085615d7c7288a39da Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 20:38:20 2025 -0500 req commit 3d09869562a96b5adc7c4be279bc8c003bbb37b2 Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 20:33:27 2025 -0500 wrong number = code dont work lol i love computers commit 4c93a1271b8aea159cf53f8d7879b00513886d6f Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 20:29:39 2025 -0500 q commit 1f5361da88fe3903a1e92a345fa56bb390f69d92 Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 18:27:20 2025 -0500 same as original code now -5 days of my life commit 511b070cbbcd29b4e784e9a09d58481e50e6e82f Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 13:52:28 2025 -0500 pricey worky commit 964fdd641b63530c59e038ebc7d1e01e9570d75c Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 11:37:29 2025 -0500 prep for pricing service work commit a78c3bcba303c2605b6277c1db33b155abe4db1b Author: zman <joshua.k.rzemien@gmail.com> Date: Wed Feb 5 21:51:22 2025 -0500 more stuff yay commit bd9cfca7a95c89b2140eec57bf52bc84432b9a4e Author: zman <joshua.k.rzemien@gmail.com> Date: Tue Feb 4 22:30:33 2025 -0500 GIGA FIXED EVERYTHING OMG commit 85510a46713e0ac660e70c7befb4e94ccf11912e Author: zman <joshua.k.rzemien@gmail.com> Date: Tue Feb 4 00:01:34 2025 -0500 data model change and some new services
184 lines
6.3 KiB
Python
184 lines
6.3 KiB
Python
from logging import getLogger
|
|
from uuid import uuid4
|
|
from pandas import DataFrame
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.db.utils import db_transaction
|
|
from app.db.models import CardManabox, CardTCGPlayer, StagedFileProduct, TCGPlayerGroups
|
|
from app.services.util._dataframe import ManaboxRow, DataframeUtil
|
|
from app.services.file import FileService
|
|
from app.services.tcgplayer import TCGPlayerService
|
|
from app.services.storage import StorageService
|
|
|
|
logger = getLogger(__name__)
|
|
|
|
|
|
class ProductService:
|
|
def __init__(
|
|
self,
|
|
db: Session,
|
|
file_service: FileService,
|
|
tcgplayer_service: TCGPlayerService,
|
|
storage_service: StorageService,
|
|
):
|
|
self.db = db
|
|
self.file_service = file_service
|
|
self.tcgplayer_service = tcgplayer_service
|
|
self.storage_service = storage_service
|
|
self.df_util = DataframeUtil()
|
|
|
|
def create_staged_file_product(
|
|
self, file_id: str, card_manabox: CardManabox, row: ManaboxRow
|
|
) -> StagedFileProduct:
|
|
"""Create a staged file product entry.
|
|
|
|
Args:
|
|
file_id: The ID of the file being processed
|
|
card_manabox: The Manabox card details
|
|
row: The row data from the Manabox file
|
|
|
|
Returns:
|
|
The created staged file product
|
|
"""
|
|
staged_product = StagedFileProduct(
|
|
id=str(uuid4()),
|
|
file_id=file_id,
|
|
product_id=card_manabox.product_id,
|
|
quantity=row.quantity,
|
|
)
|
|
with db_transaction(self.db):
|
|
self.db.add(staged_product)
|
|
return staged_product
|
|
|
|
def create_card_manabox(
|
|
self, manabox_row: ManaboxRow, card_tcgplayer: CardTCGPlayer
|
|
) -> CardManabox:
|
|
"""Create a Manabox card entry.
|
|
|
|
Args:
|
|
manabox_row: The row data from the Manabox file
|
|
card_tcgplayer: The TCGPlayer card details
|
|
|
|
Returns:
|
|
The created Manabox card
|
|
"""
|
|
if not card_tcgplayer:
|
|
group = (
|
|
self.db.query(TCGPlayerGroups)
|
|
.filter(TCGPlayerGroups.abbreviation == manabox_row.set_code)
|
|
.first()
|
|
)
|
|
card_tcgplayer = self.tcgplayer_service.get_card_tcgplayer_from_manabox_row(
|
|
manabox_row, group.group_id
|
|
)
|
|
|
|
card_manabox = CardManabox(
|
|
product_id=card_tcgplayer.product_id,
|
|
name=manabox_row.name,
|
|
set_code=manabox_row.set_code,
|
|
set_name=manabox_row.set_name,
|
|
collector_number=manabox_row.collector_number,
|
|
foil=manabox_row.foil,
|
|
rarity=manabox_row.rarity,
|
|
manabox_id=manabox_row.manabox_id,
|
|
scryfall_id=manabox_row.scryfall_id,
|
|
condition=manabox_row.condition,
|
|
language=manabox_row.language,
|
|
)
|
|
|
|
with db_transaction(self.db):
|
|
self.db.add(card_manabox)
|
|
return card_manabox
|
|
|
|
def card_manabox_lookup_create_if_not_exist(
|
|
self, manabox_row: ManaboxRow
|
|
) -> CardManabox:
|
|
"""Lookup a Manabox card or create it if it doesn't exist.
|
|
|
|
Args:
|
|
manabox_row: The row data from the Manabox file
|
|
|
|
Returns:
|
|
The existing or newly created Manabox card
|
|
"""
|
|
card_manabox = (
|
|
self.db.query(CardManabox)
|
|
.filter(
|
|
CardManabox.name == manabox_row.name,
|
|
CardManabox.set_code == manabox_row.set_code,
|
|
CardManabox.set_name == manabox_row.set_name,
|
|
CardManabox.collector_number == manabox_row.collector_number,
|
|
CardManabox.foil == manabox_row.foil,
|
|
CardManabox.rarity == manabox_row.rarity,
|
|
CardManabox.manabox_id == manabox_row.manabox_id,
|
|
CardManabox.scryfall_id == manabox_row.scryfall_id,
|
|
CardManabox.condition == manabox_row.condition,
|
|
CardManabox.language == manabox_row.language,
|
|
)
|
|
.first()
|
|
)
|
|
|
|
if not card_manabox:
|
|
logger.debug(f"card_manabox not found for row: {manabox_row.__dict__}")
|
|
group = (
|
|
self.db.query(TCGPlayerGroups)
|
|
.filter(TCGPlayerGroups.abbreviation == manabox_row.set_code)
|
|
.first()
|
|
)
|
|
if not group:
|
|
logger.error(f"Group not found for set code: {manabox_row.set_code}")
|
|
logger.error(f"Row data: {manabox_row.__dict__}")
|
|
return None
|
|
|
|
card_tcgplayer = self.tcgplayer_service.get_card_tcgplayer_from_manabox_row(
|
|
manabox_row, group.group_id
|
|
)
|
|
if not card_tcgplayer:
|
|
logger.error(f"Card not found for row: {manabox_row.__dict__}")
|
|
return None
|
|
card_manabox = self.create_card_manabox(manabox_row, card_tcgplayer)
|
|
|
|
return card_manabox
|
|
|
|
def process_manabox_df(self, df: DataFrame, file_id: str) -> None:
|
|
"""Process a Manabox dataframe.
|
|
|
|
Args:
|
|
df: The Manabox dataframe to process
|
|
file_id: The ID of the file being processed
|
|
"""
|
|
for _, row in df.iterrows():
|
|
manabox_row = ManaboxRow(row)
|
|
card_manabox = self.card_manabox_lookup_create_if_not_exist(manabox_row)
|
|
if not card_manabox:
|
|
continue
|
|
self.create_staged_file_product(file_id, card_manabox, row)
|
|
|
|
def bg_process_manabox_file(self, file_id: str) -> None:
|
|
"""Process a Manabox file in the background.
|
|
|
|
Args:
|
|
file_id: The ID of the file to process
|
|
|
|
Raises:
|
|
Exception: If there's an error during processing
|
|
"""
|
|
try:
|
|
manabox_file = self.file_service.get_file(file_id)
|
|
manabox_df = self.df_util.file_to_df(manabox_file)
|
|
self.process_manabox_df(manabox_df, file_id)
|
|
|
|
with db_transaction(self.db):
|
|
manabox_file.status = "completed"
|
|
|
|
except Exception as e:
|
|
with db_transaction(self.db):
|
|
manabox_file.status = "error"
|
|
raise e
|
|
|
|
try:
|
|
self.storage_service.store_staged_products_for_file(file_id)
|
|
except Exception as e:
|
|
logger.error(f"Error creating storage records: {str(e)}")
|
|
raise e
|