diff --git a/alembic/versions/20240525_2006_8a6892a20b5d_add_price_source_field.py b/alembic/versions/20240525_2006_8a6892a20b5d_add_price_source_field.py new file mode 100644 index 00000000..70033b7c --- /dev/null +++ b/alembic/versions/20240525_2006_8a6892a20b5d_add_price_source_field.py @@ -0,0 +1,30 @@ +"""Add Price source field + +Revision ID: 8a6892a20b5d +Revises: 2737db183adb +Create Date: 2024-05-25 20:06:18.409941 + +""" +from typing import Sequence, Union + +import sqlalchemy as sa + +from alembic import op + +# revision identifiers, used by Alembic. +revision: str = "8a6892a20b5d" +down_revision: Union[str, None] = "2737db183adb" +branch_labels: Union[str, Sequence[str], None] = None +depends_on: Union[str, Sequence[str], None] = None + + +def upgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + op.add_column("prices", sa.Column("source", sa.String(), nullable=True)) + # ### end Alembic commands ### + + +def downgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + op.drop_column("prices", "source") + # ### end Alembic commands ### diff --git a/app/crud.py b/app/crud.py index d9fac8c4..e8210479 100644 --- a/app/crud.py +++ b/app/crud.py @@ -299,8 +299,10 @@ def get_price_by_id(db: Session, id: int) -> Price | None: return db.query(Price).filter(Price.id == id).first() -def create_price(db: Session, price: PriceCreate, user: UserCreate) -> Price: - db_price = Price(**price.model_dump(), owner=user.user_id) +def create_price( + db: Session, price: PriceCreate, user: UserCreate, source: str = None +) -> Price: + db_price = Price(**price.model_dump(), owner=user.user_id, source=source) db.add(db_price) db.commit() db.refresh(db_price) diff --git a/app/models.py b/app/models.py index b454f7d2..38995f40 100644 --- a/app/models.py +++ b/app/models.py @@ -166,6 +166,8 @@ class Price(Base): owner: Mapped[str] = mapped_column(String) + source = mapped_column(String, nullable=True) + created = mapped_column(DateTime(timezone=True), server_default=func.now()) __tablename__ = "prices" diff --git a/app/routers/prices.py b/app/routers/prices.py index 072f0490..10967e2d 100644 --- a/app/routers/prices.py +++ b/app/routers/prices.py @@ -32,6 +32,7 @@ def create_price( price: schemas.PriceCreateWithValidation, background_tasks: BackgroundTasks, current_user: schemas.UserCreate = Depends(get_current_user), + app_name: str | None = None, db: Session = Depends(get_db), ) -> Price: """ @@ -58,7 +59,7 @@ def create_price( detail="Proof does not belong to current user", ) # create price - db_price = crud.create_price(db, price=price, user=current_user) + db_price = crud.create_price(db, price=price, user=current_user, source=app_name) # update counts background_tasks.add_task(tasks.create_price_product, db, price=db_price) background_tasks.add_task(tasks.create_price_location, db, price=db_price) diff --git a/app/schemas.py b/app/schemas.py index 5e0b1912..a9e8deb2 100644 --- a/app/schemas.py +++ b/app/schemas.py @@ -422,6 +422,11 @@ class PriceFull(PriceCreate): product_id: int | None location_id: int | None owner: str + # source: str | None = Field( + # description="Source (App name)", + # examples=["web app", "mobile app"], + # default=None, + # ) created: datetime.datetime diff --git a/tests/integration/test_api.py b/tests/integration/test_api.py index 63de5203..1e618680 100644 --- a/tests/integration/test_api.py +++ b/tests/integration/test_api.py @@ -287,6 +287,16 @@ def test_create_price(db_session, user_session: SessionModel, clean_prices): assert response.json()["product_code"] == PRICE_2.product_code assert len(crud.get_prices(db_session)) == 1 + 1 # assert db_prices[0]["owner"] == user.user_id + # with app_name + response = client.post( + "/api/v1/prices?app_name=test", + json=jsonable_encoder(PRICE_1), + headers={"Authorization": f"Bearer {user_session.token}"}, + ) + assert response.status_code == 201 + assert response.json()["product_code"] == PRICE_1.product_code + assert len(crud.get_prices(db_session)) == 2 + 1 + assert crud.get_prices(db_session)[0][0].source == "test" def test_create_price_moderator(db_session, user_session, user_session_1, clean_prices):