-
Notifications
You must be signed in to change notification settings - Fork 2
Add model display names to hide real model IDs in public shares #1097
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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
charlesyhuang
wants to merge
7
commits into
staging
Choose a base branch
from
feat/model-presentation-names
base: staging
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2d1bc9a
feat(admin): map model ids to presentation names on published shares
charlesyhuang d7cd8da
refactor: simplify the model display-name pass
charlesyhuang bd13a55
fix(migration): chain model_display_names onto the current head
charlesyhuang 62666cb
fix: keep share pages up pre-migration, canonicalize alias keys
charlesyhuang 28e4c53
Merge branch 'staging' into feat/model-presentation-names
charlesyhuang 29f4eff
Merge branch 'staging' into feat/model-presentation-names
charlesyhuang 33e65aa
fix(migration): rechain modeldisp01 onto trial_facets_001
charlesyhuang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,160 @@ | ||
| from typing import Annotated | ||
|
|
||
| from fastapi import APIRouter, Depends, HTTPException | ||
| from pydantic import BaseModel | ||
| from sqlalchemy import select | ||
| from sqlalchemy.exc import IntegrityError, ProgrammingError | ||
|
|
||
| from auth import AuthContext, can_manage_api_keys, require_admin | ||
| from auth.permissions import require_operator_org | ||
| from oddish.core.model_display_names import canonical_model_key | ||
| from oddish.db import ModelDisplayNameModel, get_session, utcnow | ||
| from pg_errors import is_undefined_table_error | ||
|
|
||
| router = APIRouter(prefix="/admin/model-display-names", tags=["Admin"]) | ||
|
|
||
|
|
||
| class ModelDisplayNameResponse(BaseModel): | ||
| id: str | ||
| model_name: str | ||
| display_name: str | ||
| created_by: str | None | ||
| created_at: str | ||
| updated_at: str | ||
|
|
||
|
|
||
| class ModelDisplayNameRequest(BaseModel): | ||
| model_name: str | ||
| display_name: str | ||
|
|
||
|
|
||
| class UpdateModelDisplayNameRequest(BaseModel): | ||
| display_name: str | ||
|
|
||
|
|
||
| def _response(row: ModelDisplayNameModel) -> ModelDisplayNameResponse: | ||
| return ModelDisplayNameResponse( | ||
| id=row.id, | ||
| model_name=row.model_name, | ||
| display_name=row.display_name, | ||
| created_by=row.created_by_user_id, | ||
| created_at=row.created_at.isoformat(), | ||
| updated_at=row.updated_at.isoformat(), | ||
| ) | ||
|
|
||
|
|
||
| def _require_manage(auth: AuthContext) -> None: | ||
| require_operator_org(auth) | ||
| if not can_manage_api_keys(auth): | ||
| raise HTTPException( | ||
| 403, "Only organization admins may edit model display names" | ||
| ) | ||
|
|
||
|
|
||
| def _unavailable(exc: ProgrammingError) -> HTTPException: | ||
| if is_undefined_table_error(exc): | ||
| return HTTPException( | ||
| 503, | ||
| "Model display names are not available yet (schema is still " | ||
| "migrating). Try again shortly.", | ||
| ) | ||
| raise exc | ||
|
|
||
|
|
||
| @router.get("", response_model=list[ModelDisplayNameResponse]) | ||
| async def list_model_display_names( | ||
| auth: Annotated[AuthContext, Depends(require_admin)], | ||
| ) -> list[ModelDisplayNameResponse]: | ||
| require_operator_org(auth) | ||
| try: | ||
| async with get_session() as session: | ||
| rows = await session.scalars( | ||
| select(ModelDisplayNameModel).order_by(ModelDisplayNameModel.model_name) | ||
| ) | ||
| return [_response(row) for row in rows] | ||
| except ProgrammingError as exc: | ||
| raise _unavailable(exc) from exc | ||
|
|
||
|
|
||
| @router.post("", response_model=ModelDisplayNameResponse) | ||
| async def set_model_display_name( | ||
| request: ModelDisplayNameRequest, | ||
| auth: Annotated[AuthContext, Depends(require_admin)], | ||
| ) -> ModelDisplayNameResponse: | ||
| _require_manage(auth) | ||
| # Store the canonical spelling: the live UNIQUE index is case-sensitive but | ||
| # the public lookup is not, so raw input would let "Spiffy-Balloon" and | ||
| # "spiffy-balloon" coexist as two live rows with only one of them applying. | ||
| model_name = canonical_model_key(request.model_name) | ||
| display_name = request.display_name.strip() | ||
| if not model_name: | ||
| raise HTTPException(400, "model_name must not be empty") | ||
| if not display_name: | ||
| raise HTTPException(400, "display_name must not be empty") | ||
|
|
||
| try: | ||
| async with get_session() as session: | ||
| existing = await session.scalar( | ||
| select(ModelDisplayNameModel).where( | ||
| ModelDisplayNameModel.model_name == model_name | ||
| ) | ||
| ) | ||
| if existing is not None: | ||
| existing.display_name = display_name | ||
| await session.commit() | ||
| return _response(existing) | ||
|
|
||
| row = ModelDisplayNameModel( | ||
| model_name=model_name, | ||
| display_name=display_name, | ||
| created_by_user_id=auth.user_id, | ||
| ) | ||
| session.add(row) | ||
| try: | ||
| await session.commit() | ||
| except IntegrityError: | ||
| raise HTTPException(409, "that model already has a display name") | ||
| return _response(row) | ||
| except ProgrammingError as exc: | ||
| raise _unavailable(exc) from exc | ||
|
|
||
|
|
||
| @router.put("/{name_id}", response_model=ModelDisplayNameResponse) | ||
| async def update_model_display_name( | ||
| name_id: str, | ||
| request: UpdateModelDisplayNameRequest, | ||
| auth: Annotated[AuthContext, Depends(require_admin)], | ||
| ) -> ModelDisplayNameResponse: | ||
| _require_manage(auth) | ||
| display_name = request.display_name.strip() | ||
| if not display_name: | ||
| raise HTTPException(400, "display_name must not be empty") | ||
|
|
||
| try: | ||
| async with get_session() as session: | ||
| row = await session.get(ModelDisplayNameModel, name_id) | ||
| if row is None: | ||
| raise HTTPException(404, "display name not found") | ||
| row.display_name = display_name | ||
| await session.commit() | ||
| return _response(row) | ||
| except ProgrammingError as exc: | ||
| raise _unavailable(exc) from exc | ||
|
|
||
|
|
||
| @router.delete("/{name_id}") | ||
| async def remove_model_display_name( | ||
| name_id: str, | ||
| auth: Annotated[AuthContext, Depends(require_admin)], | ||
| ) -> dict: | ||
| _require_manage(auth) | ||
| try: | ||
| async with get_session() as session: | ||
| row = await session.get(ModelDisplayNameModel, name_id) | ||
| if row is None: | ||
| raise HTTPException(404, "display name not found") | ||
| row.deleted_at = utcnow() | ||
| await session.commit() | ||
| except ProgrammingError as exc: | ||
| raise _unavailable(exc) from exc | ||
| return {"deleted": name_id} | ||
This file contains hidden or 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
21 changes: 21 additions & 0 deletions
21
frontend/src/app/api/admin/model-display-names/[id]/route.ts
This file contains hidden or 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,21 @@ | ||
| import { NextRequest } from "next/server"; | ||
| import { proxyBackendJson, proxyJsonRequest } from "@/lib/backend-response"; | ||
|
|
||
| const PATH = "admin/model-display-names"; | ||
|
|
||
| export async function PUT( | ||
| request: NextRequest, | ||
| { params }: { params: Promise<{ id: string }> } | ||
| ) { | ||
| const { id } = await params; | ||
| return proxyJsonRequest(request, `${PATH}/${encodeURIComponent(id)}`, "PUT"); | ||
| } | ||
|
|
||
| export async function DELETE( | ||
| _request: NextRequest, | ||
| { params }: { params: Promise<{ id: string }> } | ||
| ) { | ||
| const { id } = await params; | ||
| const encoded = encodeURIComponent(id); | ||
| return proxyBackendJson({ path: `${PATH}/${encoded}`, method: "DELETE" }); | ||
| } |
This file contains hidden or 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 @@ | ||
| import { NextRequest } from "next/server"; | ||
| import { proxyBackendJson, proxyJsonRequest } from "@/lib/backend-response"; | ||
|
|
||
| const PATH = "admin/model-display-names"; | ||
|
|
||
| export const GET = () => proxyBackendJson({ path: PATH }); | ||
|
|
||
| export const POST = (request: NextRequest) => | ||
| proxyJsonRequest(request, PATH, "POST"); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.