Merge branch 'worktree-agent-a8e1a9fb17fbab9d8'
This commit is contained in:
commit
15057ea230
13 files changed
+1216
-192
No files matched your search
+76
-18
@@ -9,8 +9,7 @@ This is roledesk's own API, over the same store. Every refusal is
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import timedelta
|
||||
from typing import Annotated, Any
|
||||
from typing import Annotated, Any, Literal
|
||||
|
||||
from fastapi import APIRouter, HTTPException, Query, Response
|
||||
from pydantic import BaseModel, Field
|
||||
@@ -21,7 +20,8 @@ from desk.tools import agent, scrape
|
||||
from pipeline import Job
|
||||
from pipeline.web import QueueDep
|
||||
from app.models import (
|
||||
AFFINITY, Agent, Application, Company, Gate, ScoredRole, Status, Subscription, Weights,
|
||||
AFFINITY, Agent, Application, Company, Gate, Role, ScoredRole, Status, Subscription,
|
||||
Weights,
|
||||
)
|
||||
from app.storage import Store
|
||||
from app.web.deps import ConfigDep, StoreDep
|
||||
@@ -31,6 +31,45 @@ router = APIRouter(prefix="/api", tags=["roledesk"])
|
||||
|
||||
# ------------------------------------------------------------------- roles
|
||||
|
||||
class VoteBody(BaseModel):
|
||||
value: Literal[-1, 0, 1]
|
||||
|
||||
|
||||
@router.get("/roles/{uid}")
|
||||
def get_role(uid: str, store: StoreDep) -> Role:
|
||||
"""One role, open or closed, for the read page."""
|
||||
role = store.role(uid)
|
||||
if role is None:
|
||||
raise HTTPException(404, f"no such role: {uid}")
|
||||
return role
|
||||
|
||||
|
||||
@router.post("/roles/{uid}/vote")
|
||||
def vote_role(uid: str, body: VoteBody, store: StoreDep) -> Role:
|
||||
"""1 like, 0 neither way, -1 pass. The role stays listed either way; the
|
||||
vote moves it in a ranking, see `Store.roles`."""
|
||||
try:
|
||||
return store.set_vote(uid, body.value)
|
||||
except KeyError:
|
||||
raise HTTPException(404, f"no such role: {uid}") from None
|
||||
|
||||
|
||||
@router.delete("/roles/{uid}/vote")
|
||||
def unvote_role(uid: str, store: StoreDep) -> Role:
|
||||
"""Unjudged again: what pressing the stance a role already has does."""
|
||||
try:
|
||||
return store.set_vote(uid, None)
|
||||
except KeyError:
|
||||
raise HTTPException(404, f"no such role: {uid}") from None
|
||||
|
||||
|
||||
class RolesPage(BaseModel):
|
||||
"""One page of roles, and how many the same filters match in all."""
|
||||
|
||||
items: list[ScoredRole]
|
||||
total: int
|
||||
|
||||
|
||||
@router.get("/roles")
|
||||
def list_roles(
|
||||
store: StoreDep,
|
||||
@@ -43,19 +82,26 @@ def list_roles(
|
||||
show_muted: bool = False,
|
||||
agent: int | None = None,
|
||||
gated: bool = False,
|
||||
limit: Annotated[int, Query(ge=1, le=1000)] = 200,
|
||||
) -> list[ScoredRole]:
|
||||
"""Open roles, newest first. See `Store.roles` for what each filter does.
|
||||
judged: Literal["", "liked", "unjudged", "passed"] = "",
|
||||
offset: Annotated[int, Query(ge=0)] = 0,
|
||||
limit: Annotated[int, Query(ge=1, le=1000)] = 60,
|
||||
) -> RolesPage:
|
||||
"""Open roles, newest first, `limit` of them from `offset`, with the
|
||||
total. See `Store.roles` for what each filter does.
|
||||
|
||||
A muted company's roles are left out unless `show_muted` is set. With
|
||||
`agent`, ordered by that agent's score and carrying `score` and
|
||||
`gated_reason`; a gated role only with `gated=1`."""
|
||||
`agent`, ordered by that agent's score moved by the role's vote, and
|
||||
carrying `score` and `gated_reason`; a gated role only with `gated=1`.
|
||||
`judged` keeps the liked, the unjudged or the passed."""
|
||||
if agent is not None and store.agent(agent) is None:
|
||||
raise HTTPException(404, f"no such agent: {agent}")
|
||||
return store.roles(limit=limit, q=q or None, company=company or None,
|
||||
place=place or None, function=function or None,
|
||||
remote=remote or None, days=days, hide_muted=not show_muted,
|
||||
agent=agent, gated=gated)
|
||||
filters: dict[str, Any] = {
|
||||
"q": q or None, "company": company or None, "place": place or None,
|
||||
"function": function or None, "remote": remote or None, "days": days,
|
||||
"hide_muted": not show_muted, "agent": agent, "gated": gated,
|
||||
"judged": judged or None}
|
||||
return RolesPage(items=store.roles(limit=limit, offset=offset, **filters),
|
||||
total=store.count_roles(**filters))
|
||||
|
||||
|
||||
# ------------------------------------------------- roles: text and summary
|
||||
@@ -329,23 +375,35 @@ class SaveBody(BaseModel):
|
||||
|
||||
@router.post("/applications", status_code=201)
|
||||
def save_role(body: SaveBody, store: StoreDep) -> Application:
|
||||
"""Track one role. Pressing Save twice does not double-track it: the
|
||||
existing row comes back rather than a second one starting at `saved`
|
||||
over whatever status it has since moved to."""
|
||||
"""Track one role: the ★ on a role. Pressing it twice does not
|
||||
double-track it: the existing row comes back rather than a second one
|
||||
starting at `saved` over whatever status it has since moved to."""
|
||||
existing = next((a for a in store.applications() if a.role_uid == body.role_uid), None)
|
||||
if existing is not None:
|
||||
return existing
|
||||
role = store.role(body.role_uid)
|
||||
if role is None:
|
||||
raise HTTPException(404, f"no such role: {body.role_uid}")
|
||||
next_date = (store.clock() + timedelta(days=2)).date().isoformat()
|
||||
application = Application(
|
||||
role_uid=role.uid, company_key=role.company_key, role_title=role.title,
|
||||
url=role.original_url, status=body.status, source=role.source,
|
||||
next_action="Read the posting and decide", next_date=next_date)
|
||||
url=role.original_url, status=body.status, source=role.source)
|
||||
return store.save_application(application)
|
||||
|
||||
|
||||
@router.delete("/applications/{app_id}", status_code=204)
|
||||
def unsave_role(app_id: str, store: StoreDep) -> Response:
|
||||
"""The ★ pressed again. Only while the application is still `saved`: once
|
||||
it has moved on, it is history, and withdrawn is the status for that."""
|
||||
application = store.application(app_id)
|
||||
if application is None:
|
||||
raise HTTPException(404, f"no such application: {app_id}")
|
||||
if application.status != "saved":
|
||||
raise HTTPException(409, f"This application is at {application.status} already, "
|
||||
"so it stays. Set it to withdrawn instead.")
|
||||
store.delete_application(app_id)
|
||||
return Response(status_code=204)
|
||||
|
||||
|
||||
@router.get("/applications")
|
||||
def list_applications(store: StoreDep) -> list[Application]:
|
||||
"""Every tracked application, with the role title and company key it was
|
||||
|
||||
+12
-1
@@ -31,11 +31,12 @@ from collections.abc import Callable
|
||||
from dataclasses import dataclass
|
||||
from pathlib import Path
|
||||
|
||||
from fastapi import APIRouter, Request
|
||||
from fastapi import APIRouter, HTTPException, Request
|
||||
from fastapi.responses import HTMLResponse, RedirectResponse
|
||||
from fastapi.templating import Jinja2Templates
|
||||
|
||||
from app.models import REMOTE, STATUSES
|
||||
from app.web.deps import StoreDep
|
||||
|
||||
_ROOT = Path(__file__).resolve().parents[2]
|
||||
if str(_ROOT) not in sys.path:
|
||||
@@ -121,6 +122,16 @@ def roles(request: Request) -> HTMLResponse:
|
||||
remote_options=REMOTE_OPTIONS)
|
||||
|
||||
|
||||
@router.get("/roles/{uid}", response_class=HTMLResponse)
|
||||
def role(request: Request, uid: str, store: StoreDep) -> HTMLResponse:
|
||||
"""The read page of one role. A shell like the others, filled by
|
||||
`role.js`; the lookup here only makes an unknown uid a 404 page rather
|
||||
than an empty one."""
|
||||
if store.role(uid) is None:
|
||||
raise HTTPException(404, f"no such role: {uid}")
|
||||
return page(request, "role.html", uid=uid)
|
||||
|
||||
|
||||
@router.get("/applications", response_class=HTMLResponse)
|
||||
def applications(request: Request) -> HTMLResponse:
|
||||
return page(request, "applications.html", status_options=STATUS_OPTIONS)
|
||||
|
||||
Reference in new issue
Block a user