-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Modularize DB Date Fields' Logic and Denormalize
Item
Models (#11)
* feat: modularize date fields and update logic for db models * refactor: denormalize ItemPrice model into Item model - fix inheritance flow for User models and schema - add necessary UserSession schema field * fix: setup valid user response schema - add UserBaseResponse that has datetime fields - replace UserBase in api responses with UserBaseResponse
- Loading branch information
1 parent
4f1f360
commit c4dca9b
Showing
9 changed files
with
66 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import datetime as dt | ||
|
||
from beanie import Document, after_event, Replace, SaveChanges, Update, ValidateOnSave | ||
from pydantic import Field | ||
|
||
|
||
class DateMetadataDocument(Document): | ||
"""DateMetadataDocument provides created and updated time fields, and sets the correct `updated_time` each time the | ||
model instance is modified.""" | ||
|
||
created_time: dt.datetime = Field(default_factory=dt.datetime.now) | ||
updated_time: dt.datetime = Field(default_factory=dt.datetime.now) | ||
|
||
@after_event(Update, Replace, SaveChanges, ValidateOnSave) | ||
def update_document_time(self) -> None: | ||
self.updated_time = dt.datetime.now() | ||
|
||
class Settings: | ||
is_root = True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,82 +1,46 @@ | ||
import datetime as dt | ||
from decimal import Decimal | ||
from enum import Enum | ||
|
||
from beanie import Document, Link, Replace, SaveChanges, Update, ValidateOnSave, after_event | ||
from beanie import Link | ||
from pydantic import Field | ||
|
||
from src.schemas.poe import Currency | ||
from src.models.common import DateMetadataDocument | ||
from src.schemas.poe import ItemPrice | ||
|
||
|
||
class ItemIdType(str, Enum): | ||
pay = "pay" | ||
receive = "receive" | ||
|
||
|
||
# TODO: see if we can modularize the created-updated time and update-time aspects | ||
class ItemCategory(Document): | ||
class ItemCategory(DateMetadataDocument): | ||
"""ItemCategory represents a major category that items belong to. A category is the primary form of classifying | ||
items. Each category belongs to a category group (model not defined for category groups).""" | ||
|
||
name: str | ||
internal_name: str | ||
group: str | ||
created_time: dt.datetime = Field(default_factory=dt.datetime.now) | ||
updated_time: dt.datetime = Field(default_factory=dt.datetime.now) | ||
|
||
@after_event(Replace, SaveChanges, Update, ValidateOnSave) | ||
def update_time(self): | ||
self.updated_time = dt.datetime.now() | ||
|
||
class Settings: | ||
"""Defines the settings for the collection.""" | ||
|
||
name = "poe_item_categories" | ||
|
||
|
||
class Item(Document): | ||
"""Item represents a Path of Exile in-game item. Each item belongs to a category.""" | ||
class Item(DateMetadataDocument): | ||
"""Item represents a Path of Exile in-game item. Each item belongs to a category. It contains information such as | ||
item type and the current, past and predicted pricing, encapsulated in the `ItemPrice` schema.""" | ||
|
||
poe_ninja_id: int | ||
id_type: ItemIdType | None = None | ||
name: str | ||
category: Link[ItemCategory] | ||
price: ItemPrice | None | ||
type_: str | None = Field(None, serialization_alias="type") | ||
variant: str | None = None | ||
icon_url: str | None = None | ||
enabled: bool = True | ||
created_time: dt.datetime = Field(default_factory=dt.datetime.now) | ||
updated_time: dt.datetime = Field(default_factory=dt.datetime.now) | ||
|
||
@after_event(Replace, SaveChanges, Update, ValidateOnSave) | ||
def update_time(self): | ||
self.updated_time = dt.datetime.now() | ||
|
||
class Settings: | ||
"""Defines the settings for the collection.""" | ||
|
||
name = "poe_items" | ||
|
||
|
||
class ItemPrice(Document): | ||
"""ItemPrice holds information regarding the current, past and future price of an item. | ||
It stores the recent and predicted prices in a dictionary, with the date as the key.""" | ||
|
||
item: Link[Item] | ||
price: Decimal | ||
currency: Currency | ||
price_history: dict[dt.datetime, Decimal] | ||
price_history_currency: Currency | ||
price_prediction: dict[dt.datetime, Decimal] | ||
price_prediction_currency: Currency | ||
created_time: dt.datetime = Field(default_factory=dt.datetime.now) | ||
updated_time: dt.datetime = Field(default_factory=dt.datetime.now) | ||
|
||
@after_event(Replace, SaveChanges, Update, ValidateOnSave) | ||
def update_time(self): | ||
self.updated_time = dt.datetime.now() | ||
|
||
class Settings: | ||
"""Defines the settings for the collection.""" | ||
|
||
name = "poe_item_prices" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,22 @@ | ||
import datetime as dt | ||
from decimal import Decimal | ||
from enum import Enum | ||
|
||
from pydantic import BaseModel | ||
|
||
|
||
class Currency(str, Enum): | ||
divines = "divines" | ||
chaos = "chaos" | ||
divines = "divines" | ||
|
||
|
||
class ItemPrice(BaseModel): | ||
"""ItemPrice holds information regarding the current, past and future price of an item. | ||
It stores the recent and predicted prices in a dictionary, with the date as the key.""" | ||
|
||
price: Decimal | ||
currency: Currency | ||
price_history: dict[dt.datetime, Decimal] | ||
price_history_currency: Currency | ||
price_prediction: dict[dt.datetime, Decimal] | ||
price_prediction_currency: Currency |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters