Update App/routers/stocks/models.py
Browse files- App/routers/stocks/models.py +52 -52
App/routers/stocks/models.py
CHANGED
@@ -1,20 +1,27 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
from tortoise.contrib.pydantic.creator import pydantic_queryset_creator, pydantic_model_creator
|
4 |
from tortoise.models import Model
|
5 |
from tortoise import fields
|
|
|
6 |
from tortoise.queryset import QuerySet
|
7 |
|
8 |
-
# ===============================================
|
9 |
-
# Step 1: Define all your ORM Models first
|
10 |
-
# ===============================================
|
11 |
|
12 |
class Stock(Model):
|
13 |
id = fields.IntField(primary_key=True)
|
14 |
symbol = fields.CharField(max_length=10, unique=True)
|
15 |
name = fields.CharField(max_length=200)
|
16 |
created_at = fields.DatetimeField(auto_now_add=True)
|
17 |
-
updated_at = fields.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
class Meta:
|
20 |
table = "stocks"
|
@@ -22,66 +29,39 @@ class Stock(Model):
|
|
22 |
|
23 |
class StockPriceData(Model):
|
24 |
id = fields.IntField(primary_key=True)
|
25 |
-
stock
|
26 |
date = fields.DateField()
|
27 |
opening_price = fields.DecimalField(max_digits=15, decimal_places=2)
|
28 |
closing_price = fields.DecimalField(max_digits=15, decimal_places=2)
|
29 |
high = fields.DecimalField(max_digits=15, decimal_places=2)
|
30 |
low = fields.DecimalField(max_digits=15, decimal_places=2)
|
31 |
-
volume = fields.BigIntField()
|
32 |
-
turnover = fields.BigIntField()
|
33 |
-
shares_in_issue = fields.BigIntField()
|
34 |
-
market_cap = fields.BigIntField()
|
35 |
created_at = fields.DatetimeField(auto_now_add=True)
|
36 |
|
37 |
@staticmethod
|
38 |
-
async def get_list(data
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
"""
|
43 |
-
# --- Pydantic model definitions are now safely inside the method ---
|
44 |
-
|
45 |
-
# 1. Define a slim Pydantic model for the nested Stock object
|
46 |
-
StockPydantic_Slim = pydantic_model_creator(
|
47 |
-
Stock,
|
48 |
-
name="StockSlim",
|
49 |
-
exclude=("dividends", "portfolio_holdings", "price_data", "watching")
|
50 |
-
)
|
51 |
-
|
52 |
-
# 2. Define a base Pydantic model for StockPriceData, excluding the default 'stock' field
|
53 |
-
StockPriceData_Pydantic_Base = pydantic_model_creator(
|
54 |
-
StockPriceData,
|
55 |
-
name="StockPriceDataBase",
|
56 |
-
exclude=("stock",)
|
57 |
-
)
|
58 |
-
|
59 |
-
# 3. Create the final Pydantic model by combining the base and the slim stock model
|
60 |
-
class StockPriceData_Pydantic_Final(StockPriceData_Pydantic_Base):
|
61 |
-
stock: StockPydantic_Slim
|
62 |
-
|
63 |
-
# 4. Create the serializer for a list of these final models
|
64 |
-
PriceDataList_Serializer = pydantic_queryset_creator(
|
65 |
-
tortoise_model=StockPriceData,
|
66 |
-
pydantic_model=StockPriceData_Pydantic_Final,
|
67 |
-
name="PriceDataList"
|
68 |
-
)
|
69 |
-
|
70 |
-
# Finally, serialize the data using the model we just constructed
|
71 |
-
return await PriceDataList_Serializer.from_queryset(data)
|
72 |
|
73 |
async def to_dict(self):
|
74 |
-
|
75 |
-
|
|
|
76 |
|
77 |
class Meta:
|
78 |
table = "stock_price_data"
|
79 |
-
unique_together = (
|
|
|
|
|
80 |
|
81 |
|
82 |
class Dividend(Model):
|
83 |
id = fields.IntField(primary_key=True)
|
84 |
-
stock
|
85 |
ex_dividend_date = fields.DateField()
|
86 |
dividend_amount = fields.DecimalField(max_digits=15, decimal_places=4)
|
87 |
dividend_type = fields.CharField(max_length=50, null=True, blank=True)
|
@@ -90,8 +70,28 @@ class Dividend(Model):
|
|
90 |
max_digits=10, decimal_places=4, null=True, blank=True
|
91 |
)
|
92 |
created_at = fields.DatetimeField(auto_now_add=True)
|
93 |
-
updated_at = fields.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
94 |
|
95 |
class Meta:
|
96 |
table = "dividends"
|
97 |
-
ordering = [
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from tortoise.contrib.pydantic.creator import pydantic_queryset_creator
|
|
|
|
|
2 |
from tortoise.models import Model
|
3 |
from tortoise import fields
|
4 |
+
from tortoise.contrib.pydantic import pydantic_model_creator
|
5 |
from tortoise.queryset import QuerySet
|
6 |
|
|
|
|
|
|
|
7 |
|
8 |
class Stock(Model):
|
9 |
id = fields.IntField(primary_key=True)
|
10 |
symbol = fields.CharField(max_length=10, unique=True)
|
11 |
name = fields.CharField(max_length=200)
|
12 |
created_at = fields.DatetimeField(auto_now_add=True)
|
13 |
+
updated_at = fields.DatetimeField(auto_now=True)
|
14 |
+
|
15 |
+
@staticmethod
|
16 |
+
async def get_list(data):
|
17 |
+
if type(data) == list(Stock):
|
18 |
+
parser = pydantic_queryset_creator(Stock)
|
19 |
+
return await parser.from_queryset(data)
|
20 |
+
|
21 |
+
async def to_dict(self):
|
22 |
+
if type(self) == Stock:
|
23 |
+
parser = pydantic_model_creator(Stock)
|
24 |
+
return await parser.from_tortoise_orm(self)
|
25 |
|
26 |
class Meta:
|
27 |
table = "stocks"
|
|
|
29 |
|
30 |
class StockPriceData(Model):
|
31 |
id = fields.IntField(primary_key=True)
|
32 |
+
stock = fields.ForeignKeyField("models.Stock", related_name="price_data")
|
33 |
date = fields.DateField()
|
34 |
opening_price = fields.DecimalField(max_digits=15, decimal_places=2)
|
35 |
closing_price = fields.DecimalField(max_digits=15, decimal_places=2)
|
36 |
high = fields.DecimalField(max_digits=15, decimal_places=2)
|
37 |
low = fields.DecimalField(max_digits=15, decimal_places=2)
|
38 |
+
volume = fields.BigIntField() # Use BigIntField for large numbers
|
39 |
+
turnover = fields.BigIntField() # Use BigIntField instead of IntField
|
40 |
+
shares_in_issue = fields.BigIntField() # Use BigIntField for large numbers
|
41 |
+
market_cap = fields.BigIntField() # Use BigIntField for large numbers
|
42 |
created_at = fields.DatetimeField(auto_now_add=True)
|
43 |
|
44 |
@staticmethod
|
45 |
+
async def get_list(data):
|
46 |
+
if type(data) == QuerySet:
|
47 |
+
parser = pydantic_queryset_creator(StockPriceData)
|
48 |
+
return await parser.from_queryset(data)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
|
50 |
async def to_dict(self):
|
51 |
+
if type(self) == Model:
|
52 |
+
parser = pydantic_model_creator(StockPriceData)
|
53 |
+
return await parser.from_tortoise_orm(self)
|
54 |
|
55 |
class Meta:
|
56 |
table = "stock_price_data"
|
57 |
+
unique_together = (
|
58 |
+
("stock", "date"),
|
59 |
+
) # Prevent duplicate entries for same stock/date
|
60 |
|
61 |
|
62 |
class Dividend(Model):
|
63 |
id = fields.IntField(primary_key=True)
|
64 |
+
stock = fields.ForeignKeyField("models.Stock", related_name="dividends")
|
65 |
ex_dividend_date = fields.DateField()
|
66 |
dividend_amount = fields.DecimalField(max_digits=15, decimal_places=4)
|
67 |
dividend_type = fields.CharField(max_length=50, null=True, blank=True)
|
|
|
70 |
max_digits=10, decimal_places=4, null=True, blank=True
|
71 |
)
|
72 |
created_at = fields.DatetimeField(auto_now_add=True)
|
73 |
+
updated_at = fields.DatetimeField(auto_now=True)
|
74 |
+
|
75 |
+
@staticmethod
|
76 |
+
async def get_list(data):
|
77 |
+
if type(data) == QuerySet:
|
78 |
+
parser = pydantic_queryset_creator(Dividend)
|
79 |
+
return await parser.from_queryset(data)
|
80 |
+
|
81 |
+
async def to_dict(self):
|
82 |
+
if type(self) == Model:
|
83 |
+
parser = pydantic_model_creator(Dividend)
|
84 |
+
return await parser.from_tortoise_orm(self)
|
85 |
+
if type(self) == QuerySet:
|
86 |
+
parser = pydantic_queryset_creator(Dividend)
|
87 |
+
return await parser.from_queryset(self)
|
88 |
|
89 |
class Meta:
|
90 |
table = "dividends"
|
91 |
+
ordering = [
|
92 |
+
"-ex_dividend_date",
|
93 |
+
"-payment_date",
|
94 |
+
] # Order by dates descending by default
|
95 |
+
|
96 |
+
def __str__(self):
|
97 |
+
return f"{self.stock.symbol} Dividend: {self.dividend_amount} on {self.ex_dividend_date}"
|