From 34d548a0412f8a666f21a567fc55a1691fee24cf Mon Sep 17 00:00:00 2001 From: 14Kgun Date: Tue, 2 Jan 2024 12:16:14 +0900 Subject: [PATCH 1/3] fix: sqlmodel --- src/models/__init__.py | 3 ++- src/models/food.py | 4 ++-- src/models/{review.py => history.py} | 14 +++++++------- src/models/post.py | 20 ++++++++++++++++++++ src/models/tag.py | 22 ++++++++++------------ src/models/user.py | 19 +++++++++++++++---- src/services/food.py | 4 ++-- src/services/review.py | 19 ++++++++++--------- 8 files changed, 68 insertions(+), 37 deletions(-) rename src/models/{review.py => history.py} (63%) create mode 100644 src/models/post.py diff --git a/src/models/__init__.py b/src/models/__init__.py index 23ca596..78a5389 100644 --- a/src/models/__init__.py +++ b/src/models/__init__.py @@ -3,8 +3,9 @@ from src.settings import settings from .food import * +from .history import * +from .post import * from .report import * -from .review import * from .tag import * from .user import * diff --git a/src/models/food.py b/src/models/food.py index 847c957..726ef67 100644 --- a/src/models/food.py +++ b/src/models/food.py @@ -8,8 +8,8 @@ class FoodModel(SQLModel, table=True): if TYPE_CHECKING: + from .history import HistoryModel from .report import ReportModel - from .review import ReviewModel id: int = Field(primary_key=True, default=None, index=True) name: str @@ -31,4 +31,4 @@ class FoodModel(SQLModel, table=True): created_at: datetime = Field(default_factory=datetime.utcnow, nullable=False) reports: List["ReportModel"] = Relationship(back_populates="food") - reviews: List["ReviewModel"] = Relationship(back_populates="food") + histories: List["HistoryModel"] = Relationship(back_populates="food") diff --git a/src/models/review.py b/src/models/history.py similarity index 63% rename from src/models/review.py rename to src/models/history.py index 203a52e..f95c98c 100644 --- a/src/models/review.py +++ b/src/models/history.py @@ -4,10 +4,11 @@ from sqlmodel import Field, Relationship, SQLModel -class ReviewModel(SQLModel, table=True): +class HistoryModel(SQLModel, table=True): if TYPE_CHECKING: from .food import FoodModel - from .tag import ReviewTagModel + from .post import PostModel + from .tag import PostTagModel from .user import UserModel id: int = Field(primary_key=True, default=None, index=True) @@ -18,14 +19,13 @@ class ReviewModel(SQLModel, table=True): energy: Optional[float] created_at: datetime = Field(default_factory=datetime.utcnow, nullable=False) - content: Optional[str] - food_id: Optional[int] = Field( default=None, foreign_key="foodmodel.id", nullable=True ) - food: "FoodModel" = Relationship(back_populates="reviews") + food: Optional["FoodModel"] = Relationship(back_populates="histories") user_id: int = Field(foreign_key="usermodel.id") - user: "UserModel" = Relationship(back_populates="reviews") + user: "UserModel" = Relationship(back_populates="histories") - review_tags: List["ReviewTagModel"] = Relationship(back_populates="review") + post_id: Optional[int] = Field(foreign_key="postmodel.id") + post: Optional["PostModel"] = Relationship(back_populates="history") diff --git a/src/models/post.py b/src/models/post.py new file mode 100644 index 0000000..4e5c903 --- /dev/null +++ b/src/models/post.py @@ -0,0 +1,20 @@ +from datetime import datetime +from typing import TYPE_CHECKING, List, Optional + +from sqlmodel import Field, Relationship, SQLModel + + +class PostModel(SQLModel, table=True): + if TYPE_CHECKING: + from .history import HistoryModel + from .tag import PostTagModel + + id: int = Field(primary_key=True, default=None, index=True) + content: str + created_at: datetime = Field(default_factory=datetime.utcnow, nullable=False) + + history_id: int = Field(foreign_key="historymodel.id") + history: "HistoryModel" = Relationship(back_populates="history") + + content: Optional[str] + post_tags: List["PostTagModel"] = Relationship(back_populates="post") diff --git a/src/models/tag.py b/src/models/tag.py index 4b8d782..51958ce 100644 --- a/src/models/tag.py +++ b/src/models/tag.py @@ -4,24 +4,22 @@ class TagModel(SQLModel, table=True): - id: int = Field(primary_key=True, default=None, index=True) - name: str = Field(index=True, unique=True) - color_background: Optional[str] - color_border: Optional[str] + id: str = Field(primary_key=True, index=True, unique=True) + name: str = Field(unique=True) - review_tags: List["ReviewTagModel"] = Relationship(back_populates="tag") + post_tags: List["PostTagModel"] = Relationship(back_populates="tag") -class ReviewTagModel(SQLModel, table=True): +class PostTagModel(SQLModel, table=True): if TYPE_CHECKING: - from .review import ReviewModel + from .post import PostModel id: int = Field(primary_key=True, default=None, index=True) - review_id: int = Field(foreign_key="reviewmodel.id") - review: "ReviewModel" = Relationship(back_populates="review_tags") + post_id: int = Field(foreign_key="postmodel.id") + post: "PostModel" = Relationship(back_populates="post_tags") - tag_id: int = Field(foreign_key="tagmodel.id") - tag: "TagModel" = Relationship(back_populates="review_tags") + tag_id: str = Field(foreign_key="tagmodel.id") + tag: "TagModel" = Relationship(back_populates="post_tags") - __table_args__ = (UniqueConstraint("review_id", "tag_id"),) + __table_args__ = (UniqueConstraint("post_id", "tag_id"),) diff --git a/src/models/user.py b/src/models/user.py index 77e0ae5..9c8c032 100644 --- a/src/models/user.py +++ b/src/models/user.py @@ -7,11 +7,11 @@ class UserModel(SQLModel, table=True): if TYPE_CHECKING: + from .history import HistoryModel from .report import UserReportModel - from .review import ReviewModel - id: int = Field(default=None, primary_key=True) - username: str + id: int = Field(default=None, index=True, primary_key=True) + nickname: str email: str = Field(unique=True) password: str height: Optional[float] @@ -20,4 +20,15 @@ class UserModel(SQLModel, table=True): gender: Optional[GenderEnum] user_reports: List["UserReportModel"] = Relationship(back_populates="user") - reviews: List["ReviewModel"] = Relationship(back_populates="user") + histories: List["HistoryModel"] = Relationship(back_populates="user") + devices: List["DeviceModel"] = Relationship(back_populates="user") + + +class DeviceModel(SQLModel, table=True): + token: str = Field(primary_key=True, index=True) + notification_after_one_hour: bool = Field(default=False) + notification_after_two_hours: bool = Field(default=False) + notification_after_four_hours: bool = Field(default=False) + + user_id: int = Field(foreign_key="usermodel.id") + user: "UserModel" = Relationship(back_populates="devices") diff --git a/src/services/food.py b/src/services/food.py index 13bb829..98b6187 100644 --- a/src/services/food.py +++ b/src/services/food.py @@ -2,7 +2,7 @@ from sqlalchemy.exc import IntegrityError from sqlmodel import Session, select -from src.models import FoodModel, ReportModel, ReviewModel, engine +from src.models import FoodModel, HistoryModel, ReportModel, engine from src.schemas import ( DistributionFoodContent, FoodCreateBody, @@ -133,7 +133,7 @@ def get_food_detail(food_id: int) -> FoodDetail: def get_tags_by_food_id(id: int, session: Session) -> list[Tag]: - post = session.exec(select(ReviewModel).where(ReviewModel.food_id == id)).first() + post = session.exec(select(HistoryModel).where(HistoryModel.food_id == id)).first() tag_list = [] if post is not None: tags = post.review_tags diff --git a/src/services/review.py b/src/services/review.py index 537cdb2..644f575 100644 --- a/src/services/review.py +++ b/src/services/review.py @@ -5,13 +5,13 @@ from sqlalchemy.orm import joinedload from sqlmodel import Session, select -from src.models import ReviewModel, ReviewTagModel, engine +from src.models import HistoryModel, PostTagModel, engine from src.schemas import CurrentUser, Nutrients, Review, ReviewCreateBody, Tag from src.utils.time import kst def create_review(body: ReviewCreateBody, current_user: CurrentUser) -> int: - review = ReviewModel( + review = HistoryModel( food_id=body.food_id, carbohydrate=body.nutrients.carbohydrate, protein=body.nutrients.protein, @@ -33,7 +33,7 @@ def create_review(body: ReviewCreateBody, current_user: CurrentUser) -> int: try: with Session(engine) as session: for tag_id in body.tag_ids: - post_tag = ReviewTagModel(review_id=review.id, tag_id=tag_id) + post_tag = PostTagModel(review_id=review.id, tag_id=tag_id) session.add(post_tag) session.commit() @@ -47,10 +47,10 @@ def search_review(*queries) -> list[Review]: with Session(engine) as session: posts = ( session.exec( - select(ReviewModel) + select(HistoryModel) .where(*queries) .options( - joinedload(ReviewModel.review_tags).joinedload(ReviewTagModel.tag) + joinedload(HistoryModel.review_tags).joinedload(PostTagModel.tag) ) ) .unique() @@ -87,13 +87,14 @@ def search_review(*queries) -> list[Review]: def search_review_by_food_id(food_id: int) -> list[Review]: - return search_review(ReviewModel.food_id == food_id) + return search_review(HistoryModel.food_id == food_id) def search_review_by_day(datestr: str, current_user: CurrentUser) -> list[Review]: + print(datestr) date = datetime.strptime(datestr, "%Y%m%d").astimezone(kst) return search_review( - ReviewModel.user_id == current_user.id, - ReviewModel.created_at >= date - timedelta(hours=9), - ReviewModel.created_at < date + timedelta(days=1) - timedelta(hours=9), + HistoryModel.user_id == current_user.id, + HistoryModel.created_at >= date - timedelta(hours=9), + HistoryModel.created_at < date + timedelta(days=1) - timedelta(hours=9), ) From 36f2a6d24d7e486c0d44ad377b88360d673e85e3 Mon Sep 17 00:00:00 2001 From: 14Kgun Date: Wed, 3 Jan 2024 19:28:32 +0900 Subject: [PATCH 2/3] fix: sqlmodel --- src/models/__init__.py | 1 - src/models/food.py | 2 +- src/models/history.py | 10 ++++++---- src/models/post.py | 20 -------------------- src/models/tag.py | 14 +++++++------- src/services/review.py | 6 +++--- 6 files changed, 17 insertions(+), 36 deletions(-) delete mode 100644 src/models/post.py diff --git a/src/models/__init__.py b/src/models/__init__.py index 78a5389..16300dc 100644 --- a/src/models/__init__.py +++ b/src/models/__init__.py @@ -4,7 +4,6 @@ from .food import * from .history import * -from .post import * from .report import * from .tag import * from .user import * diff --git a/src/models/food.py b/src/models/food.py index 726ef67..47308c6 100644 --- a/src/models/food.py +++ b/src/models/food.py @@ -25,7 +25,7 @@ class FoodModel(SQLModel, table=True): sugar: Optional[float] energy: Optional[float] type: FoodType - image_src: Optional[str] + image_src: Optional[str] = Field(max_length=500) barcode_number: Optional[str] = Field(index=True, unique=True) product_db_id: Optional[int] = Field(index=True) created_at: datetime = Field(default_factory=datetime.utcnow, nullable=False) diff --git a/src/models/history.py b/src/models/history.py index f95c98c..a08c08e 100644 --- a/src/models/history.py +++ b/src/models/history.py @@ -7,8 +7,7 @@ class HistoryModel(SQLModel, table=True): if TYPE_CHECKING: from .food import FoodModel - from .post import PostModel - from .tag import PostTagModel + from .tag import HistoryTagModel from .user import UserModel id: int = Field(primary_key=True, default=None, index=True) @@ -27,5 +26,8 @@ class HistoryModel(SQLModel, table=True): user_id: int = Field(foreign_key="usermodel.id") user: "UserModel" = Relationship(back_populates="histories") - post_id: Optional[int] = Field(foreign_key="postmodel.id") - post: Optional["PostModel"] = Relationship(back_populates="history") + post_content: Optional[str] + post_created_at: Optional[datetime] = Field( + default_factory=datetime.utcnow, nullable=False + ) + post_tags: List["HistoryTagModel"] = Relationship(back_populates="history") diff --git a/src/models/post.py b/src/models/post.py deleted file mode 100644 index 4e5c903..0000000 --- a/src/models/post.py +++ /dev/null @@ -1,20 +0,0 @@ -from datetime import datetime -from typing import TYPE_CHECKING, List, Optional - -from sqlmodel import Field, Relationship, SQLModel - - -class PostModel(SQLModel, table=True): - if TYPE_CHECKING: - from .history import HistoryModel - from .tag import PostTagModel - - id: int = Field(primary_key=True, default=None, index=True) - content: str - created_at: datetime = Field(default_factory=datetime.utcnow, nullable=False) - - history_id: int = Field(foreign_key="historymodel.id") - history: "HistoryModel" = Relationship(back_populates="history") - - content: Optional[str] - post_tags: List["PostTagModel"] = Relationship(back_populates="post") diff --git a/src/models/tag.py b/src/models/tag.py index 51958ce..d3e8308 100644 --- a/src/models/tag.py +++ b/src/models/tag.py @@ -1,4 +1,4 @@ -from typing import TYPE_CHECKING, List, Optional +from typing import TYPE_CHECKING, List from sqlmodel import Field, Relationship, SQLModel, UniqueConstraint @@ -7,19 +7,19 @@ class TagModel(SQLModel, table=True): id: str = Field(primary_key=True, index=True, unique=True) name: str = Field(unique=True) - post_tags: List["PostTagModel"] = Relationship(back_populates="tag") + post_tags: List["HistoryTagModel"] = Relationship(back_populates="tag") -class PostTagModel(SQLModel, table=True): +class HistoryTagModel(SQLModel, table=True): if TYPE_CHECKING: - from .post import PostModel + from .history import HistoryModel id: int = Field(primary_key=True, default=None, index=True) - post_id: int = Field(foreign_key="postmodel.id") - post: "PostModel" = Relationship(back_populates="post_tags") + history_id: int = Field(foreign_key="historymodel.id") + history: "HistoryModel" = Relationship(back_populates="post_tags") tag_id: str = Field(foreign_key="tagmodel.id") tag: "TagModel" = Relationship(back_populates="post_tags") - __table_args__ = (UniqueConstraint("post_id", "tag_id"),) + __table_args__ = (UniqueConstraint("history_id", "tag_id"),) diff --git a/src/services/review.py b/src/services/review.py index 644f575..88c2f1e 100644 --- a/src/services/review.py +++ b/src/services/review.py @@ -5,7 +5,7 @@ from sqlalchemy.orm import joinedload from sqlmodel import Session, select -from src.models import HistoryModel, PostTagModel, engine +from src.models import HistoryModel, HistoryTagModel, engine from src.schemas import CurrentUser, Nutrients, Review, ReviewCreateBody, Tag from src.utils.time import kst @@ -33,7 +33,7 @@ def create_review(body: ReviewCreateBody, current_user: CurrentUser) -> int: try: with Session(engine) as session: for tag_id in body.tag_ids: - post_tag = PostTagModel(review_id=review.id, tag_id=tag_id) + post_tag = HistoryTagModel(review_id=review.id, tag_id=tag_id) session.add(post_tag) session.commit() @@ -50,7 +50,7 @@ def search_review(*queries) -> list[Review]: select(HistoryModel) .where(*queries) .options( - joinedload(HistoryModel.review_tags).joinedload(PostTagModel.tag) + joinedload(HistoryModel.review_tags).joinedload(HistoryTagModel.tag) ) ) .unique() From 8fd2371e37ca8cccf6a6adfc6a9c22902309241f Mon Sep 17 00:00:00 2001 From: 14Kgun Date: Wed, 10 Jan 2024 19:11:33 +0900 Subject: [PATCH 3/3] fix: sqlmodel --- src/models/tag.py | 3 ++- src/models/user.py | 4 +++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/models/tag.py b/src/models/tag.py index d3e8308..0122808 100644 --- a/src/models/tag.py +++ b/src/models/tag.py @@ -1,4 +1,4 @@ -from typing import TYPE_CHECKING, List +from typing import TYPE_CHECKING, List, Optional from sqlmodel import Field, Relationship, SQLModel, UniqueConstraint @@ -6,6 +6,7 @@ class TagModel(SQLModel, table=True): id: str = Field(primary_key=True, index=True, unique=True) name: str = Field(unique=True) + color: Optional[str] post_tags: List["HistoryTagModel"] = Relationship(back_populates="tag") diff --git a/src/models/user.py b/src/models/user.py index 9c8c032..ad811a5 100644 --- a/src/models/user.py +++ b/src/models/user.py @@ -1,3 +1,4 @@ +from datetime import datetime from typing import TYPE_CHECKING, List, Optional from sqlmodel import Field, Relationship, SQLModel @@ -16,8 +17,9 @@ class UserModel(SQLModel, table=True): password: str height: Optional[float] weight: Optional[float] - birth: Optional[str] + birth: Optional[datetime] gender: Optional[GenderEnum] + signup_at: datetime = Field(default_factory=datetime.utcnow, nullable=False) user_reports: List["UserReportModel"] = Relationship(back_populates="user") histories: List["HistoryModel"] = Relationship(back_populates="user")