|
| 1 | +from datetime import datetime, timezone |
| 2 | + |
| 3 | +from pydantic import BaseConfig, validator |
| 4 | +from sqlmodel import Field, SQLModel |
| 5 | + |
| 6 | +from src.models.base import BaseModel |
| 7 | + |
| 8 | + |
| 9 | +class MemeBase(SQLModel): |
| 10 | + submission_id: str = Field(...) |
| 11 | + submission_url: str = Field(...) |
| 12 | + submission_title: str = Field(...) |
| 13 | + permalink: str = Field(...) |
| 14 | + author: str = Field(...) |
| 15 | + timestamp: datetime = Field(...) |
| 16 | + |
| 17 | + class Config(BaseConfig): |
| 18 | + json_encoder = { |
| 19 | + datetime: lambda dt: dt.replace(tzinfo=timezone.utc).isoformat(), |
| 20 | + } |
| 21 | + schema_extra = { |
| 22 | + "example": { |
| 23 | + "id": "1234-43143-3134-13423", |
| 24 | + "submission_id": "nny218", |
| 25 | + "submission_title": "This community is so nice. Helps me hodl.", |
| 26 | + "submission_url": "https://i.redd.it/gdv6tbamkb271.jpg", |
| 27 | + "permalink": "/r/dogecoin/comments/nnvakd/still_holding_keep_the_faith/", |
| 28 | + "author": "42points", |
| 29 | + "timestamp": "2004-09-16T23:59:58.75", |
| 30 | + "created_at": "2004-09-16T23:59:58.75", |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + |
| 35 | +class Meme(BaseModel, MemeBase, table=True): |
| 36 | + @validator("created_at", pre=True, always=True) |
| 37 | + def set_created_at_now(cls, v): |
| 38 | + return v or datetime.now() |
0 commit comments