-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
148 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
"""Other output models.""" | ||
|
||
from pydantic import BaseModel | ||
|
||
|
||
class JustUrl(BaseModel): | ||
"""Just returns a URL.""" | ||
|
||
url: str |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
"""Admin routes.""" | ||
|
||
from fastapi import APIRouter | ||
from account.config import CONFIG | ||
from . import stripe, token | ||
|
||
router = APIRouter(prefix=f"/{CONFIG.admin_root}", include_in_schema=False) | ||
|
||
# router.include_router(stripe.router) | ||
router.include_router(token.router) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
"""Billing admin routes.""" | ||
|
||
from fastapi import APIRouter, Depends, HTTPException | ||
|
||
from account.models.user import User | ||
from account.models.util import JustUrl | ||
from account.util.current_user import admin_user, embedded_user | ||
from account.util.stripe import get_portal_session | ||
|
||
router = APIRouter(prefix="/stripe") | ||
|
||
|
||
@router.get("/portal", dependencies=[Depends(admin_user)]) | ||
async def get_billing_url(user: User = Depends(embedded_user)) -> JustUrl: | ||
"""Return the Stripe account portal for another user.""" | ||
if not (user.stripe and user.stripe.customer_id): | ||
raise HTTPException(400, "No stripe fields available") | ||
session = get_portal_session(user) | ||
return JustUrl(url=session.url) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
"""Token admin routes.""" | ||
|
||
from fastapi import APIRouter, Depends | ||
|
||
from account.models.token import AllTokenUsageOut | ||
from account.models.user import User | ||
from account.util.current_user import admin_user, embedded_user | ||
from account.util.token import token_usage_for | ||
|
||
router = APIRouter(prefix="/token") | ||
|
||
|
||
@router.post("/history", dependencies=[Depends(admin_user)]) | ||
async def get_all_history( | ||
days: int = 30, user: User = Depends(embedded_user) | ||
) -> list[AllTokenUsageOut]: | ||
"""Return all recent token history for another user.""" | ||
return await token_usage_for(user, days) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
"""Shared token utilities.""" | ||
|
||
from datetime import datetime, timedelta, UTC | ||
|
||
from bson.objectid import ObjectId | ||
|
||
from account.models.token import AllTokenUsageOut, TokenUsage | ||
from account.models.user import User | ||
|
||
|
||
async def token_usage_for(user: User, days: int) -> list[AllTokenUsageOut]: | ||
"""Get recent token history for a user.""" | ||
days_since = datetime.now(tz=UTC) - timedelta(days=days) | ||
data = ( | ||
await TokenUsage.find( | ||
TokenUsage.user_id == ObjectId(user.id), | ||
TokenUsage.date >= days_since, | ||
) | ||
.aggregate( | ||
[ | ||
{"$project": {"_id": 0, "date": 1, "count": 1, "token_id": 1}}, | ||
{ | ||
"$group": { | ||
"_id": "$token_id", | ||
"days": {"$push": {"date": "$date", "count": "$count"}}, | ||
} | ||
}, | ||
] | ||
) | ||
.to_list() | ||
) | ||
for i, item in enumerate(data): | ||
data[i]["token_id"] = item["_id"] | ||
del data[i]["_id"] | ||
return [AllTokenUsageOut.model_validate(d) for d in data] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters