add fiber and new pydantic

This commit is contained in:
doman 2023-07-30 20:18:42 +02:00
parent 73b481ad82
commit d11478df01
13 changed files with 49 additions and 8 deletions

1
.dockerignore Normal file
View file

@ -0,0 +1 @@
!negotiator/

View file

@ -17,6 +17,7 @@ class CreateProduct(AuthorizedController):
content.carb, content.carb,
content.protein, content.protein,
content.fat, content.fat,
content.fiber,
) )
return Product.from_orm(product) return Product.from_orm(product)
except AssertionError as e: except AssertionError as e:

View file

@ -52,6 +52,14 @@ class Diary(Base, CommonMixin):
""" """
return sum(meal.fat for meal in self.meals) return sum(meal.fat for meal in self.meals)
@property
def fiber(self) -> float:
"""fiber.
:rtype: float
"""
return sum(meal.fat for meal in self.meals)
@classmethod @classmethod
def query(cls, user_id: int) -> Select: def query(cls, user_id: int) -> Select:
"""get_all.""" """get_all."""

View file

@ -61,6 +61,14 @@ class Entry(Base, CommonMixin):
""" """
return self.amount * self.product.fat return self.amount * self.product.fat
@property
def fiber(self) -> float:
"""fiber.
:rtype: float
"""
return self.amount * self.product.fiber
@classmethod @classmethod
async def create( async def create(
cls, session: AsyncSession, meal_id: int, product_id: int, grams: float cls, session: AsyncSession, meal_id: int, product_id: int, grams: float

View file

@ -51,6 +51,14 @@ class Meal(Base, CommonMixin):
""" """
return sum(entry.fat for entry in self.entries) return sum(entry.fat for entry in self.entries)
@property
def fiber(self) -> float:
"""fiber.
:rtype: float
"""
return sum(entry.fiber for entry in self.entries)
@classmethod @classmethod
async def create( async def create(
cls, cls,

View file

@ -14,6 +14,7 @@ class Product(Base, CommonMixin):
protein: Mapped[float] protein: Mapped[float]
carb: Mapped[float] carb: Mapped[float]
fat: Mapped[float] fat: Mapped[float]
fiber: Mapped[float]
@property @property
def calories(self) -> float: def calories(self) -> float:
@ -21,7 +22,7 @@ class Product(Base, CommonMixin):
:rtype: float :rtype: float
""" """
return self.protein * 4 + self.carb * 4 + self.fat * 9 return self.protein * 4 + self.carb * 4 + self.fat * 9 + self.fiber * 2
@classmethod @classmethod
async def list_all( async def list_all(
@ -39,15 +40,23 @@ class Product(Base, CommonMixin):
@classmethod @classmethod
async def create( async def create(
cls, session: AsyncSession, name: str, carb: float, protein: float, fat: float cls,
session: AsyncSession,
name: str,
carb: float,
protein: float,
fat: float,
fiber: float,
) -> "Product": ) -> "Product":
# validation here # validation here
assert carb <= 100, "carb must be less than 100" assert carb <= 100, "carb must be less than 100"
assert protein <= 100, "protein must be less than 100" assert protein <= 100, "protein must be less than 100"
assert fat <= 100, "fat must be less than 100" assert fat <= 100, "fat must be less than 100"
assert fiber <= 100, "fiber must be less than 100"
assert carb >= 0, "carb must be greater than 0" assert carb >= 0, "carb must be greater than 0"
assert protein >= 0, "protein must be greater than 0" assert protein >= 0, "protein must be greater than 0"
assert fat >= 0, "fat must be greater than 0" assert fat >= 0, "fat must be greater than 0"
assert fiber >= 0, "fiber must be greater than 0"
assert carb + protein + fat <= 100, "total must be less than 100" assert carb + protein + fat <= 100, "total must be less than 100"
# to avoid duplicates in the database keep name as lower # to avoid duplicates in the database keep name as lower

View file

@ -15,6 +15,7 @@ class Diary(BaseModel):
protein: float protein: float
carb: float carb: float
fat: float fat: float
fiber: float
class Config: class Config:
orm_mode = True from_attributes = True

View file

@ -15,9 +15,10 @@ class Entry(BaseModel):
protein: float protein: float
carb: float carb: float
fat: float fat: float
fiber: float
class Config: class Config:
orm_mode = True from_attributes = True
class CreateEntryPayload(BaseModel): class CreateEntryPayload(BaseModel):

View file

@ -13,11 +13,12 @@ class Meal(BaseModel):
protein: float protein: float
carb: float carb: float
fat: float fat: float
fiber: float
entries: List[Entry] entries: List[Entry]
diary_id: int diary_id: int
class Config: class Config:
orm_mode = True from_attributes = True
class CreateMealPayload(BaseModel): class CreateMealPayload(BaseModel):

View file

@ -11,9 +11,10 @@ class Product(BaseModel):
protein: float protein: float
carb: float carb: float
fat: float fat: float
fiber: float
class Config: class Config:
orm_mode = True from_attributes = True
class CreateProductPayload(BaseModel): class CreateProductPayload(BaseModel):
@ -23,6 +24,7 @@ class CreateProductPayload(BaseModel):
protein: float protein: float
carb: float carb: float
fat: float fat: float
fiber: float
class ListProductPayload(BaseModel): class ListProductPayload(BaseModel):

View file

@ -5,7 +5,7 @@ class User(BaseModel):
username: str username: str
class Config: class Config:
orm_mode = True from_attributes = True
class CreateUserPayload(BaseModel): class CreateUserPayload(BaseModel):

View file

@ -1,4 +1,4 @@
from pydantic import BaseSettings from pydantic_settings import BaseSettings
from typing import List from typing import List

View file

@ -1,5 +1,6 @@
fastapi fastapi
pydantic pydantic
pydantic_settings
sqlalchemy[postgresql_asyncpg] sqlalchemy[postgresql_asyncpg]
uvicorn[standard] uvicorn[standard]
asyncpg asyncpg