Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Mini CRM
A small CRM web application written with the Python 3 standard library only
(http.server, sqlite3, json, unittest, urllib). No third-party
packages, no network access required.
Run
python3 server.py # http://127.0.0.1:8000
PORT=8080 CRM_DB=crm.db python3 server.py
| Variable | Default | Meaning |
|---|---|---|
PORT |
8000 |
TCP port, always on 127.0.0.1 |
CRM_DB |
crm.db |
SQLite database file |
Tables are created on first start. The single page app is served at GET /.
Test
python3 -m unittest discover -s tests
The suite runs offline. Most tests drive the real request handler in process;
tests/test_live_server.py additionally starts a listening server and is
skipped automatically when the environment forbids opening a socket.
Data model
| Table | Columns |
|---|---|
| companies | id, name (required), website, industry |
| contacts | id, name (required), email, phone, company_id → companies |
| deals | id, title (required), amount (number ≥ 0, default 0), stage (default lead), company_id → companies, contact_id → contacts, created_at |
| notes | id, deal_id → deals, body (required), created_at |
| activity | id, created_at, action (create/update/delete), entity, entity_id, deal_id, summary |
Every create, update and delete appends one activity row in the same SQLite
transaction as the change itself, so the log can never drift from the data.
Deal stages are exactly lead, qualified, proposal, won, lost.
Foreign keys use ON DELETE SET NULL, so deleting a company or contact keeps
the deals and only clears the reference; deleting a deal removes its notes.
API
All bodies and responses are JSON (Content-Type: application/json).
Unknown ids answer 404 {"error": "..."}, invalid input answers
400 {"error": "..."}, and a known route used with the wrong verb answers
405 {"error": "..."}. An oversized body answers 413 {"error": "..."} and a
database conflict answers 409 {"error": "..."}; unexpected failures return a
generic 500 and are logged with a traceback on the server side.
For each resource companies, contacts, deals:
| Method & path | Behaviour |
|---|---|
GET /api/<r> |
list all; ?q=text case-insensitive substring match on name/title/email, deals also accept ?stage=<stage> |
POST /api/<r> |
create, 201 + the record including its integer id |
GET /api/<r>/{id} |
fetch one record |
PUT /api/<r>/{id} |
replace the editable fields, 200 + the record |
DELETE /api/<r>/{id} |
delete, 204 with no body |
Every list endpoint also accepts ?limit=N&offset=M (no limit and offset=0
by default) and always answers with an X-Total-Count header holding the
number of records matching the filters, ignoring the page.
Notes:
| Method & path | Behaviour |
|---|---|
GET /api/deals/{id}/notes |
list the notes of a deal, oldest first |
POST /api/deals/{id}/notes |
create a note, body {"body": "text"} is required |
Pipeline:
| Method & path | Behaviour |
|---|---|
GET /api/pipeline |
{"lead": {"count": n, "amount": total}, ...} for every stage |
CSV export (same q, stage, limit and offset parameters, header row,
RFC 4180 quoting, Content-Disposition: attachment):
| Method & path | Behaviour |
|---|---|
GET /api/companies.csv |
companies as text/csv |
GET /api/contacts.csv |
contacts as text/csv |
GET /api/deals.csv |
deals as text/csv |
Activity log:
| Method & path | Behaviour |
|---|---|
GET /api/activity |
newest entries first; ?limit=N, ?offset=M, ?entity=<r>, ?deal_id=<id>, plus X-Total-Count |
Examples
curl -s -X POST localhost:8000/api/companies \
-H 'Content-Type: application/json' \
-d '{"name":"Acme","industry":"tools"}'
curl -s -X POST localhost:8000/api/deals \
-H 'Content-Type: application/json' \
-d '{"title":"Rollout","amount":4200,"stage":"qualified","company_id":1}'
curl -s 'localhost:8000/api/deals?stage=qualified&q=roll'
curl -s -X POST localhost:8000/api/deals/1/notes -d '{"body":"kickoff call booked"}'
curl -s localhost:8000/api/pipeline
curl -s -D - -o /dev/null 'localhost:8000/api/companies?limit=2' # X-Total-Count
curl -s localhost:8000/api/deals.csv
curl -s 'localhost:8000/api/activity?deal_id=1&limit=5'
Front-end
static/index.html is a dependency free single page app: companies list,
contacts list, a deals board with one column per stage, a search box that
filters every list through the API, create/edit/delete forms for the three
resources, and a deal detail panel showing the notes (with an add-note form)
and the activity of that deal. Keyboard shortcuts: / focuses the search box,
Esc closes the deal panel.
Layout
server.py entry point: env config, server class, re-exports
router.py routes → responses, pagination, CSV, static files
handlers.py http.server adapter (bytes in, bytes out)
db.py SQLite schema and access, atomic activity logging
models.py resource registry, stages, validators per resource
validation.py shared input validation helpers
responses.py Response/FileResponse and the error → status mapping
errors.py the exception types behind 400/404/405/413
static/index.html single page front-end
tests/ unittest suite (one module per area of the API)
CODE_REVIEW.md phase 2 review: weaknesses found and the fixes applied
python3 server.py is still the only entry point; the modules above are
imported by it and import server still exposes them.