Add, edit, check and delete a company by hand through the API, and show the by-hand fields in the list
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
b3cb93d5aa
commit
28afc334c3
2 files changed
+259
-24
No files matched your search
+106
-16
@@ -10,6 +10,7 @@ This is roledesk's own API, over the same store. Every refusal is
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Annotated, Any, Literal
|
||||
from urllib.parse import urlsplit
|
||||
|
||||
from fastapi import APIRouter, HTTPException, Query, Response
|
||||
from pydantic import BaseModel, Field
|
||||
@@ -17,7 +18,7 @@ from pydantic import BaseModel, Field
|
||||
from app import jobs
|
||||
from app.models import (
|
||||
AFFINITY, Agent, Application, Company, Gate, Limits, Prompts, Role, RoleRow,
|
||||
ScoredRole, Status, Subscription, Weights, company_key,
|
||||
HandReason, ScoredRole, Status, Subscription, Weights, company_key,
|
||||
)
|
||||
from app.storage import Store
|
||||
from app.web.deps import ConfigDep, StoreDep
|
||||
@@ -303,11 +304,17 @@ class CompanyOut(BaseModel):
|
||||
the board has failed on every fetch and why it last did, both empty
|
||||
while it answers. `from_search` marks a row that exists only because a
|
||||
search listed its roles and somebody judged it (`Store.set_affinity`):
|
||||
no board, not even a detached one, and no `ats`, `careers_url` or
|
||||
`domain`, which every way of following a company writes one of."""
|
||||
no board, not even a detached one, and no `ats`, `careers_url`, `domain`
|
||||
or `hand_reason`, which every way of following a company or listing it by
|
||||
hand writes one of (`jobs.sources.known_from_search_only`). `by_hand`
|
||||
marks a company on the list checked by hand: no board shown and not
|
||||
`from_search`. `hand_reason` says why (`app.models.HAND_REASONS`) and
|
||||
`checked_at` when a person last looked at its careers page, "" for never."""
|
||||
|
||||
key: str
|
||||
name: str
|
||||
domain: str = ""
|
||||
notes: str = ""
|
||||
board: str
|
||||
subscription_id: int | None = None
|
||||
on: bool | None = None
|
||||
@@ -320,6 +327,9 @@ class CompanyOut(BaseModel):
|
||||
failing_since: str = ""
|
||||
failing_reason: str = ""
|
||||
from_search: bool = False
|
||||
hand_reason: str = ""
|
||||
checked_at: str = ""
|
||||
by_hand: bool = False
|
||||
|
||||
|
||||
def _company_boards(store: Store) -> dict[str, tuple[int, str, bool, str]]:
|
||||
@@ -337,29 +347,109 @@ def _company_boards(store: Store) -> dict[str, tuple[int, str, bool, str]]:
|
||||
return boards
|
||||
|
||||
|
||||
@router.get("/companies")
|
||||
def list_companies(store: StoreDep, agent: int | None = None) -> list[CompanyOut]:
|
||||
"""Every company, with `affinity` as `agent` (the default agent when
|
||||
absent) has it; 0 throughout when there is no agent."""
|
||||
def _company_outs(store: Store, agent: int | None) -> list[CompanyOut]:
|
||||
boards = _company_boards(store)
|
||||
ever_boarded = {company_key(s.company) for s in store.subscriptions(source="ats")}
|
||||
counts = store.open_role_counts()
|
||||
failing = jobs.pull.failing_boards(store)
|
||||
out = []
|
||||
for c in store.companies(_agent_id(store, agent)):
|
||||
for c in store.companies(agent):
|
||||
sub_id, host, on, target = boards.get(c.key, (None, "", None, ""))
|
||||
since, reason = failing.get(target, ("", ""))
|
||||
out.append(CompanyOut(key=c.key, name=c.name, board=host,
|
||||
subscription_id=sub_id, on=on, affinity=c.affinity,
|
||||
open_roles=counts.get(c.key, 0), ats=c.ats,
|
||||
ats_slug=c.ats_slug, careers_url=c.careers_url,
|
||||
country=c.country,
|
||||
failing_since=since, failing_reason=reason,
|
||||
from_search=not (c.key in ever_boarded or c.ats
|
||||
or c.careers_url or c.domain)))
|
||||
from_search = jobs.sources.known_from_search_only(c, ever_boarded)
|
||||
out.append(CompanyOut(key=c.key, name=c.name, domain=c.domain, notes=c.notes,
|
||||
board=host, subscription_id=sub_id, on=on,
|
||||
affinity=c.affinity, open_roles=counts.get(c.key, 0),
|
||||
ats=c.ats, ats_slug=c.ats_slug, careers_url=c.careers_url,
|
||||
country=c.country, failing_since=since, failing_reason=reason,
|
||||
from_search=from_search, hand_reason=c.hand_reason,
|
||||
checked_at=c.checked_at,
|
||||
by_hand=c.key not in boards and not from_search))
|
||||
return out
|
||||
|
||||
|
||||
def _one_company(store: Store, key: str) -> CompanyOut:
|
||||
"""`key` as the list shows it, with the default agent's affinity."""
|
||||
return next(c for c in _company_outs(store, _agent_id(store, None)) if c.key == key)
|
||||
|
||||
|
||||
@router.get("/companies")
|
||||
def list_companies(store: StoreDep, agent: int | None = None) -> list[CompanyOut]:
|
||||
"""Every company, with `affinity` as `agent` (the default agent when
|
||||
absent) has it; 0 throughout when there is no agent."""
|
||||
return _company_outs(store, _agent_id(store, agent))
|
||||
|
||||
|
||||
class NewCompanyBody(BaseModel):
|
||||
name: str
|
||||
careers_url: str = ""
|
||||
notes: str = ""
|
||||
hand_reason: HandReason = "added"
|
||||
|
||||
|
||||
@router.post("/companies", status_code=201)
|
||||
def add_company(body: NewCompanyBody, store: StoreDep) -> CompanyOut:
|
||||
"""A company put on the list checked by hand: no board, `hand_reason`
|
||||
`added` unless it says another. 409 when its key, from the name, is taken."""
|
||||
try:
|
||||
company = jobs.sources.add_by_hand(store, body.name, careers_url=body.careers_url,
|
||||
notes=body.notes, hand_reason=body.hand_reason)
|
||||
except jobs.sources.Conflict as exc:
|
||||
raise HTTPException(409, str(exc)) from exc
|
||||
except ValueError as exc:
|
||||
raise HTTPException(422, str(exc)) from exc
|
||||
return _one_company(store, company.key)
|
||||
|
||||
|
||||
class CompanyPatch(BaseModel):
|
||||
name: str | None = None
|
||||
careers_url: str | None = None
|
||||
notes: str | None = None
|
||||
hand_reason: HandReason | None = None
|
||||
|
||||
|
||||
@router.patch("/companies/{key}")
|
||||
def edit_company(key: str, body: CompanyPatch, store: StoreDep) -> CompanyOut:
|
||||
"""Change any of a company's name, careers link, notes and reason; a field
|
||||
left out stays. The key never changes, and `name` only for a company
|
||||
without a board: 409 for one with a subscription, shown or not (see
|
||||
`jobs.sources.edit_company`)."""
|
||||
fields = body.model_dump(exclude_unset=True)
|
||||
if any(v is None for v in fields.values()):
|
||||
raise HTTPException(422, "A field is left out, not null.")
|
||||
try:
|
||||
jobs.sources.edit_company(store, key, **fields)
|
||||
except KeyError:
|
||||
raise HTTPException(404, f"no such company: {key}") from None
|
||||
except jobs.sources.Conflict as exc:
|
||||
raise HTTPException(409, str(exc)) from exc
|
||||
except ValueError as exc:
|
||||
raise HTTPException(422, str(exc)) from exc
|
||||
return _one_company(store, key)
|
||||
|
||||
|
||||
@router.post("/companies/{key}/checked")
|
||||
def company_checked(key: str, store: StoreDep) -> CompanyOut:
|
||||
"""A person has just looked at the company's careers page."""
|
||||
if store.company(key) is None:
|
||||
raise HTTPException(404, f"no such company: {key}")
|
||||
store.update_company(key, checked_at=store.now())
|
||||
return _one_company(store, key)
|
||||
|
||||
|
||||
@router.delete("/companies/{key}", status_code=204)
|
||||
def remove_company(key: str, store: StoreDep) -> Response:
|
||||
"""Delete a company and every agent's stance on it. 409 for one with a
|
||||
board shown or with open roles; detach the board first."""
|
||||
try:
|
||||
jobs.sources.remove_company(store, key)
|
||||
except KeyError:
|
||||
raise HTTPException(404, f"no such company: {key}") from None
|
||||
except ValueError as exc:
|
||||
raise HTTPException(409, str(exc)) from exc
|
||||
return Response(status_code=204)
|
||||
|
||||
|
||||
class CompanyBoardBody(BaseModel):
|
||||
board: str
|
||||
|
||||
|
||||
Reference in new issue
Block a user