Skip to content

Commit

Permalink
feat: add proof.price_count to keep track of number of prices (#185)
Browse files Browse the repository at this point in the history
* New field Proof.price_count

* Set price_count on create. Update price_count on new price

* Update tests
  • Loading branch information
raphodn authored Feb 4, 2024
1 parent 2f039fe commit 1637f36
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
"""Add proof price_count field
Revision ID: 1cf89b834379
Revises: f3c157567667
Create Date: 2024-02-04 16:32:07.827619
"""
from typing import Sequence, Union

import sqlalchemy as sa

from alembic import op

# revision identifiers, used by Alembic.
revision: str = "1cf89b834379"
down_revision: Union[str, None] = "f3c157567667"
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(
"proofs",
sa.Column("price_count", sa.Integer(), server_default="0", nullable=False),
)
op.create_index(
op.f("ix_proofs_price_count"), "proofs", ["price_count"], unique=False
)
# Set the price_count to the number of prices for each proof
op.execute(
"""
UPDATE proofs
SET price_count = (
SELECT COUNT(*)
FROM prices
WHERE prices.proof_id = proofs.id
)
"""
)
# ### end Alembic commands ###


def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.drop_index(op.f("ix_proofs_price_count"), table_name="proofs")
op.drop_column("proofs", "price_count")
# ### end Alembic commands ###
2 changes: 2 additions & 0 deletions app/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,8 @@ def create_price(
background_tasks.add_task(tasks.create_price_product, db, price=db_price)
background_tasks.add_task(tasks.create_price_location, db, price=db_price)
background_tasks.add_task(tasks.increment_user_price_count, db, user=current_user)
if price.proof_id and proof:
background_tasks.add_task(tasks.increment_proof_price_count, db, proof=proof)
return db_price


Expand Down
14 changes: 14 additions & 0 deletions app/crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
ProductFilter,
ProductFull,
ProofFilter,
ProofFull,
UserCreate,
)

Expand Down Expand Up @@ -317,6 +318,7 @@ def create_proof(
type: ProofTypeEnum,
user: UserCreate,
is_public: bool = True,
price_count: int = 0,
):
"""Create a proof in the database.
Expand All @@ -333,6 +335,7 @@ def create_proof(
type=type,
owner=user.user_id,
is_public=is_public,
price_count=price_count,
)
db.add(db_proof)
db.commit()
Expand Down Expand Up @@ -386,6 +389,17 @@ def create_proof_file(file: UploadFile) -> tuple[str, str]:
return (file_path, mimetype)


def increment_proof_price_count(db: Session, proof: ProofFull):
"""Increment the price count of a proof.
This is used to keep track of the number of prices linked to a proof.
"""
proof.price_count += 1
db.commit()
db.refresh(proof)
return proof


# Locations
# ------------------------------------------------------------------------------
def get_locations_query(filters: LocationFilter | None = None):
Expand Down
1 change: 1 addition & 0 deletions app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ class Proof(Base):
is_public = Column(Boolean, nullable=False, server_default="true", index=True)

prices: Mapped[list["Price"]] = relationship(back_populates="proof")
price_count = Column(Integer, nullable=False, server_default="0", index=True)

owner = Column(String, index=True)

Expand Down
3 changes: 3 additions & 0 deletions app/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ class ProofFull(BaseModel):
"Set false only if the proof contains personal information.",
),
]
price_count: int = Field(
description="number of prices for this proof.", examples=[15], default=0
)
created: datetime.datetime


Expand Down
8 changes: 7 additions & 1 deletion app/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from app import crud
from app.models import Product
from app.schemas import LocationCreate, PriceFull, ProductCreate, UserCreate
from app.schemas import LocationCreate, PriceFull, ProductCreate, ProofFull, UserCreate
from app.utils import (
OFF_FIELDS,
fetch_location_openstreetmap_details,
Expand All @@ -27,6 +27,12 @@ def increment_user_price_count(db: Session, user: UserCreate):
crud.increment_user_price_count(db, user=user)


# Proofs
# ------------------------------------------------------------------------------
def increment_proof_price_count(db: Session, proof: ProofFull):
crud.increment_proof_price_count(db, proof=proof)


# Products
# ------------------------------------------------------------------------------
def create_price_product(db: Session, price: PriceFull):
Expand Down
2 changes: 2 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,7 @@ def test_get_proofs(user_session: SessionModel):
"owner",
"created",
"is_public",
"price_count",
}

for i, item in enumerate(data):
Expand All @@ -628,6 +629,7 @@ def test_get_proofs(user_session: SessionModel):
assert item["type"] == ("PRICE_TAG" if i == 0 else "RECEIPT")
assert item["owner"] == "user"
assert item["is_public"] == (True if i == 0 else False)
assert item["price_count"] == 0


def test_get_proofs_filters(db_session, user_session: SessionModel):
Expand Down

0 comments on commit 1637f36

Please sign in to comment.