added enums for status constants/fixed status not updating
This commit is contained in:
1 parent
b0319f6ada
commit
6b3bd33739
2 files changed
+14
-9
No files matched your search
@@ -6,6 +6,7 @@ and the expected error tuple used for known-failure handling.
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
from enum import Enum
|
||||
from json import JSONDecodeError
|
||||
|
||||
import aiohttp
|
||||
@@ -58,10 +59,6 @@ BUCKET_WEBHOOK_MAP = {
|
||||
90: "90-100",
|
||||
"price_drop": "40-49",
|
||||
}
|
||||
STATUS_PENDING = "pending"
|
||||
STATUS_SENT = "sent"
|
||||
STATUS_SKIPPED = "skipped"
|
||||
STATUS_FAILED = "failed"
|
||||
|
||||
EXPECTED_TASK_ERRORS = (
|
||||
RequestsError,
|
||||
@@ -72,3 +69,12 @@ EXPECTED_TASK_ERRORS = (
|
||||
PyMongoError,
|
||||
JSONDecodeError,
|
||||
)
|
||||
|
||||
|
||||
class ProductStatus(str, Enum):
|
||||
"""Lifecycle status for a monitored product document."""
|
||||
|
||||
PENDING = "pending"
|
||||
SENT = "sent"
|
||||
SKIPPED = "skipped"
|
||||
FAILED = "failed"
|
||||
@@ -14,7 +14,7 @@ from motor.motor_asyncio import AsyncIOMotorClient, AsyncIOMotorCollection
|
||||
from pymongo import UpdateOne
|
||||
from pymongo.errors import PyMongoError
|
||||
|
||||
from monitor_utils.constants import MONGO_DATABASE_NAME, MONGO_URL, STATUS_PENDING
|
||||
from monitor_utils.constants import ProductStatus, MONGO_DATABASE_NAME, MONGO_URL
|
||||
from monitor_utils.models import Product
|
||||
|
||||
LOGGER = logging.getLogger(__name__)
|
||||
@@ -106,10 +106,10 @@ async def add_products_to_database(
|
||||
|
||||
set_on_insert = {"discoveredDate": now}
|
||||
if price_dropped:
|
||||
document["status"] = STATUS_PENDING
|
||||
document["status"] = ProductStatus.PENDING
|
||||
document["priceBeforeDrop"] = old_price
|
||||
else:
|
||||
set_on_insert["status"] = STATUS_PENDING
|
||||
set_on_insert["status"] = ProductStatus.PENDING
|
||||
|
||||
update = {"$set": document, "$setOnInsert": set_on_insert}
|
||||
if new_price is not None and new_price != old_price:
|
||||
@@ -143,7 +143,7 @@ def build_doc_id(
|
||||
return f"{product_id}_{category_identifier}" if use_composite else product_id
|
||||
|
||||
|
||||
def convert_product_to_mongo(product: Product, category_identifier: str) -> dict:
|
||||
def convert_product_to_mongo(product: Product) -> dict:
|
||||
"""Flatten a Product into its MongoDB document representation.
|
||||
|
||||
Coerces string prices to floats and stamps lastUpdated with the current
|
||||
@@ -151,7 +151,6 @@ def convert_product_to_mongo(product: Product, category_identifier: str) -> dict
|
||||
|
||||
Args:
|
||||
product: Product to convert.
|
||||
category_identifier: Category suffix retained for signature parity.
|
||||
|
||||
Returns:
|
||||
The MongoDB document dict for the product.
|
||||
|
||||
Reference in new issue
Block a user