Skip to content

Commit

Permalink
server/license_key: hide deactivated activations from API output
Browse files Browse the repository at this point in the history
  • Loading branch information
frankie567 committed Dec 18, 2024
1 parent 52a062b commit 7846dfb
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
14 changes: 2 additions & 12 deletions server/polar/license_key/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from uuid import UUID

import structlog
from sqlalchemy import Select, and_, func, select
from sqlalchemy import Select, func, select
from sqlalchemy.orm import contains_eager, joinedload

from polar.auth.models import AuthSubject, is_customer, is_organization, is_user
Expand Down Expand Up @@ -71,19 +71,9 @@ async def get_loaded(
) -> LicenseKey | None:
query = (
self._get_select_base()
.join(
LicenseKeyActivation,
onclause=(
and_(
LicenseKeyActivation.license_key_id == LicenseKey.id,
LicenseKeyActivation.deleted_at.is_(None),
)
),
isouter=True,
)
.join(Benefit, onclause=LicenseKey.benefit_id == Benefit.id)
.options(
contains_eager(LicenseKey.activations),
joinedload(LicenseKey.activations),
contains_eager(LicenseKey.benefit),
)
.where(LicenseKey.id == id)
Expand Down
17 changes: 16 additions & 1 deletion server/polar/models/license_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,26 @@ def benefit(cls) -> Mapped[BenefitLicenseKeys]:
limit_activations: Mapped[int | None] = mapped_column(Integer, nullable=True)

@declared_attr
def activations(cls) -> Mapped[list["LicenseKeyActivation"]]:
def all_activations(cls) -> Mapped[list["LicenseKeyActivation"]]:
return relationship(
"LicenseKeyActivation", lazy="raise", back_populates="license_key"
)

@declared_attr
def activations(cls) -> Mapped[list["LicenseKeyActivation"]]:
# Prices are almost always needed, so eager loading makes sense
return relationship(
"LicenseKeyActivation",
lazy="raise",
primaryjoin=(
"and_("
"LicenseKeyActivation.license_key_id == LicenseKey.id, "
"LicenseKeyActivation.deleted_at.is_(None)"
")"
),
viewonly=True,
)

usage: Mapped[int] = mapped_column(Integer, nullable=False, default=0)

limit_usage: Mapped[int | None] = mapped_column(Integer, nullable=True)
Expand Down

0 comments on commit 7846dfb

Please sign in to comment.