Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: simple Location model #35

Merged
merged 4 commits into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
"""Create Location table

Revision ID: b3b951e016d0
Revises: 20145023aad0
Create Date: 2023-11-16 14:32:39.734937

"""
from typing import Sequence, Union

import sqlalchemy as sa

from alembic import op

# revision identifiers, used by Alembic.
revision: str = "b3b951e016d0"
down_revision: Union[str, None] = "20145023aad0"
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.create_table(
"locations",
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("osm_id", sa.BigInteger(), nullable=True),
sa.Column("osm_type", sa.String(length=255), nullable=True),
sa.Column("osm_name", sa.String(), nullable=True),
sa.Column("osm_display_name", sa.String(), nullable=True),
sa.Column("osm_address_postcode", sa.String(), nullable=True),
sa.Column("osm_address_city", sa.String(), nullable=True),
sa.Column("osm_address_country", sa.String(), nullable=True),
sa.Column("osm_lat", sa.Numeric(precision=11, scale=7), nullable=True),
sa.Column("osm_lon", sa.Numeric(precision=11, scale=7), nullable=True),
sa.Column(
"created",
sa.DateTime(timezone=True),
server_default=sa.text("now()"),
nullable=True,
),
sa.Column("updated", sa.DateTime(timezone=True), nullable=True),
sa.PrimaryKeyConstraint("id"),
)
op.create_index(op.f("ix_locations_id"), "locations", ["id"], unique=False)
# ### end Alembic commands ###


def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.drop_index(op.f("ix_locations_id"), table_name="locations")
op.drop_table("locations")
# ### end Alembic commands ###
2 changes: 1 addition & 1 deletion app/enums.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from enum import Enum


class PriceLocationOSMType(Enum):
class LocationOSMType(Enum):
NODE = "NODE"
WAY = "WAY"
RELATION = "RELATION"
37 changes: 32 additions & 5 deletions app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from sqlalchemy_utils.types.currency import CurrencyType

from app.db import Base
from app.enums import PriceLocationOSMType
from app.enums import LocationOSMType

force_auto_coercion()

Expand All @@ -31,32 +31,59 @@ class User(Base):
__tablename__ = "users"


class Location(Base):
id = Column(Integer, primary_key=True, index=True)

osm_id = Column(BigInteger)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why don't we use osm_id as a primary key directly, as it's an INT?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather not rely 100% on OSM. There's probably future locations that will not/never be in OSM. Like online stores, or temporary markets, or even closed locations 🤔...

osm_type = Column(ChoiceType(LocationOSMType))

osm_name = Column(String)
osm_display_name = Column(String)
osm_address_postcode = Column(String)
osm_address_city = Column(String)
osm_address_country = Column(String)
osm_lat = Column(Numeric(precision=11, scale=7))
osm_lon = Column(Numeric(precision=11, scale=7))

created = Column(DateTime(timezone=True), server_default=func.now())
updated = Column(DateTime(timezone=True), onupdate=func.now())

__tablename__ = "locations"


class Proof(Base):
id = Column(Integer, primary_key=True, index=True)

file_path = Column(String, nullable=False)
mimetype = Column(String, index=True)

prices: Mapped[list["Price"]] = relationship(back_populates="proof")

owner = Column(String, index=True)

created = Column(DateTime(timezone=True), server_default=func.now(), index=True)
prices: Mapped[list["Price"]] = relationship(back_populates="proof")

__tablename__ = "proofs"


class Price(Base):
id = Column(Integer, primary_key=True, index=True)

product_code = Column(String, index=True)

price = Column(Numeric(precision=10, scale=2))
currency = Column(CurrencyType)

location_osm_id = Column(BigInteger, index=True)
location_osm_type = Column(ChoiceType(PriceLocationOSMType))
location_osm_type = Column(ChoiceType(LocationOSMType))

date = Column(Date)
owner = Column(String)

created = Column(DateTime(timezone=True), server_default=func.now())
proof_id: Mapped[int] = mapped_column(ForeignKey("proofs.id"), nullable=True)
proof: Mapped[Proof] = relationship(back_populates="prices")

owner = Column(String)

created = Column(DateTime(timezone=True), server_default=func.now())

__tablename__ = "prices"
26 changes: 23 additions & 3 deletions app/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from pydantic import BaseModel, ConfigDict, Field, field_serializer, field_validator
from sqlalchemy_utils import Currency

from app.enums import PriceLocationOSMType
from app.enums import LocationOSMType
from app.models import Price


Expand All @@ -16,14 +16,34 @@ class UserBase(BaseModel):
token: str


class LocationCreate(BaseModel):
model_config = ConfigDict(from_attributes=True, arbitrary_types_allowed=True)

osm_id: int = Field(gt=0)
osm_type: LocationOSMType


class LocationBase(LocationCreate):
id: int
osm_name: str | None
raphodn marked this conversation as resolved.
Show resolved Hide resolved
osm_display_name: str | None
osm_address_postcode: str | None
osm_address_city: str | None
osm_address_country: str | None
osm_lat: float | None
osm_lon: float | None
created: datetime
updated: datetime | None


class PriceCreate(BaseModel):
model_config = ConfigDict(from_attributes=True, arbitrary_types_allowed=True)

product_code: str = Field(min_length=1, pattern="^[0-9]+$")
price: float
currency: str | Currency
location_osm_id: int = Field(gt=0)
location_osm_type: PriceLocationOSMType
location_osm_type: LocationOSMType
date: date
proof_id: int | None = None

Expand Down Expand Up @@ -62,7 +82,7 @@ class ProofBase(ProofCreate):
class PriceFilter(Filter):
product_code: Optional[str] | None = None
location_osm_id: Optional[int] | None = None
location_osm_type: Optional[PriceLocationOSMType] | None = None
location_osm_type: Optional[LocationOSMType] | None = None
price: Optional[int] | None = None
currency: Optional[str] | None = None
price__gt: Optional[int] | None = None
Expand Down
Loading