feat(market): graph for fun

This commit is contained in:
2026-10-07 21:54:24 +02:00
parent 382ba01f3e
commit 8509be548e
11 changed files with 669 additions and 5 deletions
+31
View File
@@ -14,6 +14,7 @@ from market.models import (
get_event_points,
)
from market.schemas import (
BetHistoryPointSchema,
MarketListSchema,
ResolveMarketSchema,
UserBetCreateSchema,
@@ -61,6 +62,36 @@ def get_user_points(request, event_path: str):
return {"points": get_event_points(request.user, request.event)}
@router.get(f"{PREFIX}/{{market_uuid}}/history", response=List[BetHistoryPointSchema])
def bet_history(request, event_path: str, market_uuid: str):
"""Points added to each option over time (anonymous, one entry per bet placed/increased)."""
market = get_object_or_404(
Market.objects.exclude(status=Market.Status.DRAFT),
uuid=market_uuid,
event=request.event,
)
# A user can only bet on one option per market, so their option is unique
option_by_user = dict(
UserBet.objects.filter(option__market=market).values_list(
"user_id", "option__uuid"
)
)
changes = UserPointChange.objects.filter(
market=market, reason=UserPointChange.Reason.BET_PLACED
).order_by("created_at")
return [
{
"option_uuid": str(option_by_user[change.user_id]),
"created_at": change.created_at,
"amount": -change.amount,
}
for change in changes
if change.user_id in option_by_user
]
@router.post(f"{PREFIX}/{{market_uuid}}/actions/close")
def close_market(request, event_path: str, market_uuid: str):
"""Close a market. Admin only."""
+6
View File
@@ -39,6 +39,12 @@ class UserPointsSchema(Schema):
points: int
class BetHistoryPointSchema(Schema):
option_uuid: str
created_at: datetime
amount: int
class UserBetCreateSchema(Schema):
option_uuid: str
amount: int