819 lines
31 KiB
Python
819 lines
31 KiB
Python
"""Skuld: the HTTP server.
|
|
|
|
Every response is JSON or a file from static/. The session cookie is HttpOnly
|
|
and SameSite=Strict; the CSRF secret sits in a script-readable cookie that
|
|
static/session.js copies into the X-CSRF-Token header of every mutating call.
|
|
"""
|
|
|
|
import hmac
|
|
import json
|
|
import os
|
|
import re
|
|
import traceback
|
|
import uuid
|
|
from datetime import date, datetime, timezone
|
|
from http.cookies import CookieError, SimpleCookie
|
|
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
|
|
from pathlib import Path
|
|
from urllib.parse import parse_qs, urlparse
|
|
|
|
import auth
|
|
import database
|
|
import export
|
|
import ledger
|
|
import rates
|
|
import tax
|
|
import validation
|
|
|
|
STATIC_DIR = Path(__file__).resolve().parent / "static"
|
|
DEFAULT_HOST = "0.0.0.0"
|
|
DEFAULT_PORT = 8080
|
|
|
|
CONTENT_TYPES = {
|
|
".html": "text/html; charset=utf-8",
|
|
".css": "text/css; charset=utf-8",
|
|
".js": "text/javascript; charset=utf-8",
|
|
".svg": "image/svg+xml",
|
|
".ico": "image/x-icon",
|
|
".csv": "text/csv; charset=utf-8",
|
|
}
|
|
|
|
PAGES = {
|
|
"/": "index.html",
|
|
"/login": "login.html",
|
|
"/transaksjonar": "transactions.html",
|
|
"/transaksjon": "transaction.html",
|
|
"/marknader": "markets.html",
|
|
"/rapport": "report.html",
|
|
"/innstillingar": "settings.html",
|
|
}
|
|
|
|
ASSETS = (
|
|
"app.js",
|
|
"session.js",
|
|
"style.css",
|
|
"icon.svg",
|
|
"login.js",
|
|
"index.js",
|
|
"transactions.js",
|
|
"transaction.js",
|
|
"markets.js",
|
|
"report.js",
|
|
"settings.js",
|
|
)
|
|
|
|
# (method, path pattern, handler name). The handler is a method of the request
|
|
# handler and returns the JSON payload.
|
|
ROUTES = (
|
|
("GET", r"^/api/auth/state$", "auth_state"),
|
|
("POST", r"^/api/auth/setup$", "auth_setup"),
|
|
("POST", r"^/api/auth/login$", "auth_login"),
|
|
("POST", r"^/api/auth/logout$", "auth_logout"),
|
|
("POST", r"^/api/auth/password$", "auth_password"),
|
|
("GET", r"^/api/markets$", "markets_list"),
|
|
("POST", r"^/api/markets$", "markets_create"),
|
|
("PATCH", r"^/api/markets/(?P<id>\d+)$", "markets_update"),
|
|
("DELETE", r"^/api/markets/(?P<id>\d+)$", "markets_delete"),
|
|
("GET", r"^/api/transactions$", "transactions_list"),
|
|
("POST", r"^/api/transactions$", "transactions_create"),
|
|
("GET", r"^/api/transactions/(?P<id>\d+)$", "transactions_get"),
|
|
("PATCH", r"^/api/transactions/(?P<id>\d+)$", "transactions_update"),
|
|
("DELETE", r"^/api/transactions/(?P<id>\d+)$", "transactions_delete"),
|
|
("GET", r"^/api/lots$", "lots_list"),
|
|
("GET", r"^/api/prices$", "prices_list"),
|
|
("PUT", r"^/api/prices/(?P<date>\d{4}-\d{2}-\d{2})$", "prices_put"),
|
|
("GET", r"^/api/settings$", "settings_get"),
|
|
("PATCH", r"^/api/settings$", "settings_patch"),
|
|
("GET", r"^/api/report$", "report_get"),
|
|
("GET", r"^/api/report/export\.csv$", "report_export"),
|
|
)
|
|
|
|
# Routes that answer without a session. The rest need one, and every mutating
|
|
# call needs the CSRF header on top of it.
|
|
OPEN_ROUTES = ("auth_state", "auth_setup", "auth_login", "auth_logout")
|
|
|
|
|
|
class ApiError(Exception):
|
|
"""An error that is sent as {"error": message, "field": field}."""
|
|
|
|
def __init__(self, status, message, field=None):
|
|
super().__init__(message)
|
|
self.status = status
|
|
self.message = message
|
|
self.field = field
|
|
|
|
|
|
def _cookie(name, value, http_only, max_age=auth.SESSION_TTL_SECONDS):
|
|
parts = ["%s=%s" % (name, value), "Path=/", "Max-Age=%d" % max_age,
|
|
"SameSite=Strict"]
|
|
if http_only:
|
|
parts.append("HttpOnly")
|
|
return "; ".join(parts)
|
|
|
|
|
|
def _parse_year(value):
|
|
text = str(value or "").strip()
|
|
if not re.fullmatch(r"\d{4}", text):
|
|
raise ApiError(400, "Året må vere eit firetal, t.d. 2025.", "year")
|
|
return int(text)
|
|
|
|
|
|
def _as_int(value, field, message="Verdien må vere eit heiltal."):
|
|
try:
|
|
return int(str(value).strip())
|
|
except (TypeError, ValueError):
|
|
raise ApiError(400, message, field)
|
|
|
|
|
|
def _now_stamp():
|
|
return datetime.now(timezone.utc).isoformat(timespec="seconds")
|
|
|
|
|
|
def _today():
|
|
return date.today().isoformat()
|
|
|
|
|
|
def _raise_errors(errors):
|
|
"""Send the first validation error, with the field it belongs to."""
|
|
field = next(iter(errors))
|
|
raise ApiError(400, errors[field], field)
|
|
|
|
|
|
def _market_rows(conn):
|
|
rows = conn.execute(
|
|
"SELECT id, name, kind, taxable, custody_market_id, note, sort_order, archived"
|
|
" FROM markets ORDER BY sort_order, name"
|
|
).fetchall()
|
|
return [dict(row) for row in rows]
|
|
|
|
|
|
def _load_markets(conn):
|
|
rows = conn.execute("SELECT * FROM markets").fetchall()
|
|
return {row["id"]: dict(row) for row in rows}
|
|
|
|
|
|
def _bank_market_id(conn):
|
|
"""The account that pays the costs (docs/spec.md section 3.11)."""
|
|
row = conn.execute(
|
|
"SELECT value FROM settings WHERE key = 'bank_market_id'"
|
|
).fetchone()
|
|
if row is None:
|
|
raise ApiError(500, "Bankkontoen manglar i databasen.")
|
|
return int(row["value"])
|
|
|
|
|
|
def _with_price(transaction):
|
|
"""Add the derived bitcoin price of docs/spec.md section 7.3."""
|
|
transaction["btc_price_ore"] = tax.btc_price_ore(transaction)
|
|
return transaction
|
|
|
|
|
|
def _load_transaction(conn, tx_id):
|
|
row = conn.execute(
|
|
"SELECT t.*, m.name AS market_name, d.name AS to_market_name,"
|
|
" COALESCE((SELECT SUM(c.cost_ore) FROM lot_consumptions c WHERE c.tx_id = t.id), 0)"
|
|
" AS cost_ore,"
|
|
" COALESCE((SELECT SUM(c.sats) FROM lot_consumptions c WHERE c.tx_id = t.id), 0)"
|
|
" AS consumed_sats,"
|
|
" COALESCE((SELECT SUM(l.cost_ore_original) FROM lots l"
|
|
" WHERE l.origin_tx_id = t.id), 0) AS lot_cost_ore,"
|
|
" COALESCE((SELECT SUM(l.sats_original) FROM lots l"
|
|
" WHERE l.origin_tx_id = t.id), 0) AS lot_sats"
|
|
" FROM transactions t"
|
|
" LEFT JOIN markets m ON m.id = t.market_id"
|
|
" LEFT JOIN markets d ON d.id = t.to_market_id WHERE t.id = ?",
|
|
(tx_id,),
|
|
).fetchone()
|
|
if row is None:
|
|
raise ApiError(404, "Fann ikkje transaksjonen.")
|
|
return _with_price(dict(row))
|
|
|
|
|
|
def _drop_ledger(conn):
|
|
"""Throw the derived ledger away; rebuild() puts it back from the transactions."""
|
|
conn.execute("DELETE FROM lot_consumptions")
|
|
conn.execute("DELETE FROM lots")
|
|
|
|
|
|
def _may_name_bank(tx, bank_id):
|
|
"""Say whether a transaction may name the bank (docs/spec.md section 3.11)."""
|
|
if tx["type"] != "transfer_out" or tx["leg_out_ore"] is None:
|
|
return False
|
|
sides = (tx["market_id"] == bank_id, tx["to_market_id"] == bank_id)
|
|
return sides[0] != sides[1]
|
|
|
|
|
|
def _write_transaction(conn, tx_id, tx, picks):
|
|
"""Insert or update one row, filling in what the server derives."""
|
|
now = _now_stamp()
|
|
if tx["type"] == "admin_cost":
|
|
# The cost is paid from the bank and the kroner leave it (section 3.10).
|
|
tx = dict(tx)
|
|
tx["market_id"] = _bank_market_id(conn)
|
|
if tx_id is None:
|
|
cursor = conn.execute(
|
|
"INSERT INTO transactions (date, type, market_id, taxable, note, leg_in_sats,"
|
|
" leg_in_ore, leg_out_sats, leg_out_ore, value_ore, fee_sats, fee_ore,"
|
|
" to_market_id, created_at, updated_at) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",
|
|
(tx["date"], tx["type"], tx["market_id"], tx["taxable"], tx["note"],
|
|
tx["leg_in_sats"], tx["leg_in_ore"], tx["leg_out_sats"], tx["leg_out_ore"],
|
|
tx["value_ore"], tx["fee_sats"], tx["fee_ore"], tx["to_market_id"], now, now),
|
|
)
|
|
tx_id = cursor.lastrowid
|
|
else:
|
|
conn.execute(
|
|
"UPDATE transactions SET date=?, type=?, market_id=?, taxable=?, note=?,"
|
|
" leg_in_sats=?, leg_in_ore=?, leg_out_sats=?, leg_out_ore=?, value_ore=?,"
|
|
" fee_sats=?, fee_ore=?, to_market_id=?, updated_at=? WHERE id=?",
|
|
(tx["date"], tx["type"], tx["market_id"], tx["taxable"], tx["note"],
|
|
tx["leg_in_sats"], tx["leg_in_ore"], tx["leg_out_sats"], tx["leg_out_ore"],
|
|
tx["value_ore"], tx["fee_sats"], tx["fee_ore"], tx["to_market_id"], now, tx_id),
|
|
)
|
|
conn.execute("DELETE FROM lot_picks WHERE tx_id = ?", (tx_id,))
|
|
for pick in picks:
|
|
conn.execute(
|
|
"INSERT INTO lot_picks (tx_id, origin_tx_id, origin_seq, sats) VALUES (?,?,?,?)",
|
|
(tx_id, pick["origin_tx_id"], pick["origin_seq"], pick["sats"]),
|
|
)
|
|
return tx_id
|
|
|
|
|
|
def _sync_transfer_pair(conn, tx_id, previous, tx):
|
|
"""Keep the generated transfer_in in step with its transfer_out."""
|
|
group = previous["transfer_group"] if previous else None
|
|
if tx["type"] == "transfer_out" and tx["to_market_id"]:
|
|
if not group:
|
|
group = uuid.uuid4().hex
|
|
conn.execute(
|
|
"UPDATE transactions SET transfer_group = ? WHERE id = ?", (group, tx_id)
|
|
)
|
|
now = _now_stamp()
|
|
row = conn.execute(
|
|
"SELECT id FROM transactions WHERE type = 'transfer_in' AND transfer_group = ?",
|
|
(group,),
|
|
).fetchone()
|
|
if row is None:
|
|
conn.execute(
|
|
"INSERT INTO transactions (date, type, market_id, taxable, note, leg_in_sats,"
|
|
" leg_in_ore, fee_sats, fee_ore, to_market_id, transfer_group, generated,"
|
|
" created_at, updated_at)"
|
|
" VALUES (?, 'transfer_in', ?, ?, '', ?, ?, 0, 0, ?, ?, 1, ?, ?)",
|
|
(tx["date"], tx["to_market_id"], tx["taxable"], tx["leg_out_sats"],
|
|
tx["leg_out_ore"], tx["market_id"], group, now, now),
|
|
)
|
|
else:
|
|
conn.execute(
|
|
"UPDATE transactions SET date = ?, market_id = ?, taxable = ?,"
|
|
" leg_in_sats = ?, leg_in_ore = ?, fee_sats = 0, fee_ore = 0,"
|
|
" to_market_id = ?, generated = 1, updated_at = ? WHERE id = ?",
|
|
(tx["date"], tx["to_market_id"], tx["taxable"], tx["leg_out_sats"],
|
|
tx["leg_out_ore"], tx["market_id"], now, row["id"]),
|
|
)
|
|
elif group:
|
|
# The transaction is no longer a transfer with a destination: an empty
|
|
# `Til` is a withdrawal, and it leaves no pair behind (section 3.11).
|
|
_drop_ledger(conn)
|
|
conn.execute(
|
|
"DELETE FROM transactions WHERE type = 'transfer_in' AND transfer_group = ?",
|
|
(group,),
|
|
)
|
|
conn.execute(
|
|
"UPDATE transactions SET transfer_group = NULL WHERE id = ?", (tx_id,)
|
|
)
|
|
|
|
|
|
class SkuldHandler(BaseHTTPRequestHandler):
|
|
"""One HTTP request."""
|
|
|
|
server_version = "Skuld"
|
|
protocol_version = "HTTP/1.1"
|
|
|
|
def do_GET(self):
|
|
self._dispatch("GET")
|
|
|
|
def do_POST(self):
|
|
self._dispatch("POST")
|
|
|
|
def do_PATCH(self):
|
|
self._dispatch("PATCH")
|
|
|
|
def do_PUT(self):
|
|
self._dispatch("PUT")
|
|
|
|
def do_DELETE(self):
|
|
self._dispatch("DELETE")
|
|
|
|
# -- plumbing ---------------------------------------------------------
|
|
|
|
def _dispatch(self, method):
|
|
self._pending_cookies = []
|
|
self._response_sent = False
|
|
self._raw_body = self._read_body()
|
|
parsed = urlparse(self.path)
|
|
path = parsed.path
|
|
query = parse_qs(parsed.query)
|
|
try:
|
|
if path == "/health":
|
|
return self._send_json({"status": "ok"})
|
|
if path.startswith("/api/"):
|
|
return self._api(method, path, query)
|
|
return self._static(path)
|
|
except ApiError as error:
|
|
return self._send_json(
|
|
{"error": error.message, "field": error.field}, status=error.status
|
|
)
|
|
except Exception: # one bad request must not take the server down
|
|
traceback.print_exc()
|
|
return self._send_json(
|
|
{"error": "Intern feil i Skuld.", "field": None}, status=500
|
|
)
|
|
|
|
def _api(self, method, path, query):
|
|
for route_method, pattern, name in ROUTES:
|
|
match = re.match(pattern, path)
|
|
if not match or route_method != method:
|
|
continue
|
|
token = self._session_token()
|
|
if name not in OPEN_ROUTES:
|
|
if not auth.session_valid(token):
|
|
raise ApiError(401, "Du er ikkje innlogga.")
|
|
if method != "GET":
|
|
self._check_csrf(token)
|
|
body = self._json_body() if method in ("POST", "PATCH", "PUT") else {}
|
|
payload = getattr(self, name)(match.groupdict(), query, body)
|
|
if self._response_sent:
|
|
return None
|
|
return self._send_json(payload, cookies=self._pending_cookies)
|
|
raise ApiError(404, "Fann ikkje det du spurde etter.")
|
|
|
|
def _read_body(self):
|
|
"""Read the request body once, before any answer is written."""
|
|
length = int(self.headers.get("Content-Length") or 0)
|
|
return self.rfile.read(length) if length > 0 else b""
|
|
|
|
def _json_body(self):
|
|
if not self._raw_body:
|
|
return {}
|
|
try:
|
|
data = json.loads(self._raw_body.decode("utf-8"))
|
|
except (UnicodeDecodeError, ValueError):
|
|
raise ApiError(400, "Ugyldig JSON i førespurnaden.")
|
|
if not isinstance(data, dict):
|
|
raise ApiError(400, "Ugyldig JSON i førespurnaden.")
|
|
return data
|
|
|
|
def _session_token(self):
|
|
raw = self.headers.get("Cookie")
|
|
if not raw:
|
|
return None
|
|
jar = SimpleCookie()
|
|
try:
|
|
jar.load(raw)
|
|
except CookieError:
|
|
return None
|
|
morsel = jar.get(auth.SESSION_COOKIE)
|
|
return morsel.value if morsel else None
|
|
|
|
def _check_csrf(self, token):
|
|
sent = self.headers.get("X-CSRF-Token") or ""
|
|
if not sent or not hmac.compare_digest(sent, auth.csrf_secret(token or "")):
|
|
raise ApiError(403, "Manglande eller feil CSRF-token.")
|
|
|
|
def _send_json(self, payload, status=200, cookies=None, headers=None):
|
|
data = json.dumps(payload, ensure_ascii=False).encode("utf-8")
|
|
self._send_bytes(data, "application/json; charset=utf-8", status, cookies,
|
|
headers)
|
|
|
|
def _send_bytes(self, data, content_type, status=200, cookies=None, headers=None):
|
|
self.send_response(status)
|
|
self.send_header("Content-Type", content_type)
|
|
self.send_header("Content-Length", str(len(data)))
|
|
self.send_header("Cache-Control", "no-store")
|
|
for cookie in cookies or ():
|
|
self.send_header("Set-Cookie", cookie)
|
|
for name, value in (headers or {}).items():
|
|
self.send_header(name, value)
|
|
self.end_headers()
|
|
if self.command != "HEAD":
|
|
self.wfile.write(data)
|
|
self._response_sent = True
|
|
|
|
def _redirect(self, location):
|
|
self.send_response(302)
|
|
self.send_header("Location", location)
|
|
self.send_header("Content-Length", "0")
|
|
self.send_header("Cache-Control", "no-store")
|
|
self.end_headers()
|
|
|
|
# -- static pages -----------------------------------------------------
|
|
|
|
def _static(self, path):
|
|
page = PAGES.get(path)
|
|
if page is not None:
|
|
if path != "/login" and not auth.session_valid(self._session_token()):
|
|
return self._redirect("/login")
|
|
return self._send_file(STATIC_DIR / page)
|
|
name = path.lstrip("/")
|
|
if name in ASSETS:
|
|
return self._send_file(STATIC_DIR / name)
|
|
raise ApiError(404, "Fann ikkje sida.")
|
|
|
|
def _send_file(self, target):
|
|
if not target.is_file():
|
|
raise ApiError(404, "Fann ikkje sida.")
|
|
content_type = CONTENT_TYPES.get(target.suffix.lower()) or "application/octet-stream"
|
|
return self._send_bytes(target.read_bytes(), content_type)
|
|
|
|
# -- auth -------------------------------------------------------------
|
|
|
|
def _admin_username(self):
|
|
conn = database.connect()
|
|
try:
|
|
row = conn.execute("SELECT username FROM admin WHERE id = 1").fetchone()
|
|
finally:
|
|
conn.close()
|
|
return row["username"] if row else None
|
|
|
|
def _start_session(self, payload):
|
|
token = auth.create_session()
|
|
self._pending_cookies.append(_cookie(auth.SESSION_COOKIE, token, True))
|
|
self._pending_cookies.append(
|
|
_cookie(auth.CSRF_COOKIE, auth.csrf_secret(token), False)
|
|
)
|
|
return payload
|
|
|
|
def auth_state(self, params, query, body):
|
|
authenticated = auth.session_valid(self._session_token())
|
|
return {
|
|
"admin_exists": auth.admin_exists(),
|
|
"authenticated": authenticated,
|
|
"username": self._admin_username() if authenticated else None,
|
|
}
|
|
|
|
def auth_setup(self, params, query, body):
|
|
if auth.admin_exists():
|
|
raise ApiError(400, "Administratoren finst alt.")
|
|
username, error = validation.validate_username(body.get("username"))
|
|
if error:
|
|
raise ApiError(400, error, "username")
|
|
password, error = validation.validate_password(body.get("password"))
|
|
if error:
|
|
raise ApiError(400, error, "password")
|
|
auth.create_admin(username, password)
|
|
return self._start_session({"username": username})
|
|
|
|
def auth_login(self, params, query, body):
|
|
if not auth.verify_login(body.get("username"), body.get("password")):
|
|
raise ApiError(401, "Feil brukarnamn eller passord.")
|
|
return self._start_session({"username": self._admin_username()})
|
|
|
|
def auth_logout(self, params, query, body):
|
|
auth.delete_session(self._session_token())
|
|
self._pending_cookies.append(_cookie(auth.SESSION_COOKIE, "", True, max_age=0))
|
|
self._pending_cookies.append(_cookie(auth.CSRF_COOKIE, "", False, max_age=0))
|
|
return {"status": "ok"}
|
|
|
|
def auth_password(self, params, query, body):
|
|
token = self._session_token()
|
|
if not auth.session_valid(token):
|
|
raise ApiError(401, "Du er ikkje innlogga.")
|
|
self._check_csrf(token)
|
|
new_password, error = validation.validate_password(body.get("new_password"))
|
|
if error:
|
|
raise ApiError(400, error, "new_password")
|
|
if not auth.change_password(body.get("current_password"), new_password):
|
|
raise ApiError(400, "Passordet stemmer ikkje.", "current_password")
|
|
return {"status": "ok"}
|
|
|
|
|
|
# -- accounts ---------------------------------------------------------
|
|
|
|
def markets_list(self, params, query, body):
|
|
conn = database.connect()
|
|
try:
|
|
bank_id = _bank_market_id(conn)
|
|
markets = [row for row in _market_rows(conn) if row["id"] != bank_id]
|
|
on_hand = ledger.sats_on_hand(conn)
|
|
today = _today()
|
|
kroner = {
|
|
market["id"]: ledger.kroner_in_market(market["id"], today, conn)
|
|
for market in markets
|
|
}
|
|
finally:
|
|
conn.close()
|
|
for market in markets:
|
|
market["sats"] = on_hand.get(market["id"], 0)
|
|
market["kroner_ore"] = kroner[market["id"]]
|
|
return markets
|
|
|
|
def markets_create(self, params, query, body):
|
|
conn = database.connect()
|
|
try:
|
|
market, errors = validation.validate_market(body, _load_markets(conn))
|
|
if errors:
|
|
_raise_errors(errors)
|
|
cursor = conn.execute(
|
|
"INSERT INTO markets (name, kind, taxable, custody_market_id, note,"
|
|
" sort_order, archived) VALUES (?, ?, ?, ?, ?, ?, ?)",
|
|
(market["name"], market["kind"], market["taxable"],
|
|
market["custody_market_id"], market["note"], market["sort_order"],
|
|
market["archived"]),
|
|
)
|
|
conn.commit()
|
|
market["id"] = cursor.lastrowid
|
|
market["sats"] = 0
|
|
finally:
|
|
conn.close()
|
|
return market
|
|
|
|
def markets_update(self, params, query, body):
|
|
market_id = _as_int(params["id"], "id")
|
|
conn = database.connect()
|
|
try:
|
|
markets = _load_markets(conn)
|
|
if market_id not in markets:
|
|
raise ApiError(404, "Fann ikkje kontoen.")
|
|
merged = dict(markets[market_id])
|
|
merged.update({key: body[key] for key in body if key in merged})
|
|
market, errors = validation.validate_market(merged, markets, market_id)
|
|
if errors:
|
|
_raise_errors(errors)
|
|
taxable_changed = market["taxable"] != markets[market_id]["taxable"]
|
|
conn.execute(
|
|
"UPDATE markets SET name = ?, kind = ?, taxable = ?, custody_market_id = ?,"
|
|
" note = ?, sort_order = ?, archived = ? WHERE id = ?",
|
|
(market["name"], market["kind"], market["taxable"],
|
|
market["custody_market_id"], market["note"], market["sort_order"],
|
|
market["archived"], market_id),
|
|
)
|
|
if taxable_changed:
|
|
ledger.rebuild(conn)
|
|
conn.commit()
|
|
finally:
|
|
conn.close()
|
|
return market
|
|
|
|
def markets_delete(self, params, query, body):
|
|
market_id = _as_int(params["id"], "id")
|
|
conn = database.connect()
|
|
try:
|
|
row = conn.execute(
|
|
"SELECT 1 FROM markets WHERE id = ?", (market_id,)
|
|
).fetchone()
|
|
if row is None:
|
|
raise ApiError(404, "Fann ikkje kontoen.")
|
|
used = conn.execute(
|
|
"SELECT 1 FROM transactions WHERE market_id = ? OR to_market_id = ? LIMIT 1",
|
|
(market_id, market_id),
|
|
).fetchone()
|
|
if used:
|
|
raise ApiError(400, "Kontoen har transaksjonar og kan ikkje slettast.")
|
|
custodian = conn.execute(
|
|
"SELECT 1 FROM markets WHERE custody_market_id = ? LIMIT 1", (market_id,)
|
|
).fetchone()
|
|
if custodian:
|
|
raise ApiError(
|
|
400, "Kontoen er oppbevarar for andre kontoar og kan ikkje slettast."
|
|
)
|
|
conn.execute("DELETE FROM markets WHERE id = ?", (market_id,))
|
|
conn.commit()
|
|
finally:
|
|
conn.close()
|
|
return {"status": "ok"}
|
|
|
|
# -- transactions -----------------------------------------------------
|
|
|
|
def transactions_list(self, params, query, body):
|
|
where = []
|
|
values = []
|
|
if query.get("year", [""])[0]:
|
|
year = _parse_year(query["year"][0])
|
|
where.append("t.date >= ? AND t.date <= ?")
|
|
values += ["%04d-01-01" % year, "%04d-12-31" % year]
|
|
if query.get("market_id", [""])[0]:
|
|
where.append("t.market_id = ?")
|
|
values.append(_as_int(query["market_id"][0], "market_id"))
|
|
if query.get("type", [""])[0]:
|
|
where.append("t.type = ?")
|
|
values.append(query["type"][0])
|
|
sql = (
|
|
"SELECT t.*, m.name AS market_name, d.name AS to_market_name,"
|
|
" COALESCE(SUM(c.cost_ore), 0) AS cost_ore,"
|
|
" COALESCE(SUM(c.sats), 0) AS consumed_sats,"
|
|
" COALESCE((SELECT SUM(l.cost_ore_original) FROM lots l"
|
|
" WHERE l.origin_tx_id = t.id), 0) AS lot_cost_ore,"
|
|
" COALESCE((SELECT SUM(l.sats_original) FROM lots l"
|
|
" WHERE l.origin_tx_id = t.id), 0) AS lot_sats"
|
|
" FROM transactions t LEFT JOIN markets m ON m.id = t.market_id"
|
|
" LEFT JOIN markets d ON d.id = t.to_market_id"
|
|
" LEFT JOIN lot_consumptions c ON c.tx_id = t.id"
|
|
)
|
|
if where:
|
|
sql += " WHERE " + " AND ".join(where)
|
|
sql += " GROUP BY t.id ORDER BY t.date DESC, t.id DESC"
|
|
conn = database.connect()
|
|
try:
|
|
rows = conn.execute(sql, values).fetchall()
|
|
finally:
|
|
conn.close()
|
|
return [_with_price(dict(row)) for row in rows]
|
|
|
|
def transactions_get(self, params, query, body):
|
|
tx_id = _as_int(params["id"], "id")
|
|
conn = database.connect()
|
|
try:
|
|
transaction = _load_transaction(conn, tx_id)
|
|
consumptions = [dict(row) for row in conn.execute(
|
|
"SELECT c.id, c.lot_id, c.sats, c.cost_ore, c.auto, l.acquired_date"
|
|
" FROM lot_consumptions c JOIN lots l ON l.id = c.lot_id"
|
|
" WHERE c.tx_id = ? ORDER BY c.id",
|
|
(tx_id,),
|
|
).fetchall()]
|
|
picks = [dict(row) for row in conn.execute(
|
|
"SELECT origin_tx_id, origin_seq, sats FROM lot_picks WHERE tx_id = ?"
|
|
" ORDER BY id",
|
|
(tx_id,),
|
|
).fetchall()]
|
|
candidates = (
|
|
ledger.candidate_lots(transaction["market_id"], transaction["date"], conn)
|
|
if transaction["type"] in ledger.CONSUMING_TYPES
|
|
else []
|
|
)
|
|
finally:
|
|
conn.close()
|
|
return {
|
|
"transaction": transaction,
|
|
"consumptions": consumptions,
|
|
"picks": picks,
|
|
"candidate_lots": candidates,
|
|
}
|
|
|
|
def transactions_create(self, params, query, body):
|
|
return self._transaction_payload(self._save_transaction(None, body))
|
|
|
|
def transactions_update(self, params, query, body):
|
|
tx_id = _as_int(params["id"], "id")
|
|
return self._transaction_payload(self._save_transaction(tx_id, body))
|
|
|
|
def transactions_delete(self, params, query, body):
|
|
tx_id = _as_int(params["id"], "id")
|
|
conn = database.connect()
|
|
try:
|
|
row = conn.execute(
|
|
"SELECT id, type, transfer_group FROM transactions WHERE id = ?", (tx_id,)
|
|
).fetchone()
|
|
if row is None:
|
|
raise ApiError(404, "Fann ikkje transaksjonen.")
|
|
if row["type"] == "transfer_in":
|
|
raise ApiError(
|
|
400, "Overføring inn kan berre slettast saman med overføringa ut."
|
|
)
|
|
with conn:
|
|
_drop_ledger(conn)
|
|
if row["transfer_group"]:
|
|
conn.execute(
|
|
"DELETE FROM transactions WHERE transfer_group = ?",
|
|
(row["transfer_group"],),
|
|
)
|
|
else:
|
|
conn.execute("DELETE FROM transactions WHERE id = ?", (tx_id,))
|
|
ledger.rebuild(conn)
|
|
finally:
|
|
conn.close()
|
|
return {"status": "ok"}
|
|
|
|
def _save_transaction(self, tx_id, body):
|
|
picks, error = validation.validate_lot_picks(body.get("lot_picks"))
|
|
if error:
|
|
raise ApiError(400, error, "lot_picks")
|
|
conn = database.connect()
|
|
try:
|
|
tx, errors = validation.validate_transaction(
|
|
body, _load_markets(conn), _bank_market_id(conn)
|
|
)
|
|
if errors:
|
|
_raise_errors(errors)
|
|
if tx["type"] == "admin_cost" and picks:
|
|
# docs/spec.md section 8: admin_cost changes no holdings.
|
|
raise ApiError(
|
|
400, "Forvaltningskostnad rører ingen parti.", "lot_picks"
|
|
)
|
|
if tx["type"] != "admin_cost" and not _may_name_bank(tx, _bank_market_id(conn)):
|
|
# The bank is the counterparty of a cost, and a side of a kroner
|
|
# transfer, and nothing else (docs/spec.md section 3.11).
|
|
raise ApiError(
|
|
400,
|
|
"Bankkontoen kan berre brukast av forvaltningskostnad og av ei "
|
|
"overføring i kroner.",
|
|
"market_id",
|
|
)
|
|
previous = None
|
|
if tx_id is not None:
|
|
row = conn.execute(
|
|
"SELECT id, type, transfer_group FROM transactions WHERE id = ?",
|
|
(tx_id,),
|
|
).fetchone()
|
|
if row is None:
|
|
raise ApiError(404, "Fann ikkje transaksjonen.")
|
|
if row["type"] == "transfer_in":
|
|
raise ApiError(
|
|
400,
|
|
"Overføring inn kan berre sjåast og slettast saman med overføringa ut.",
|
|
)
|
|
previous = dict(row)
|
|
try:
|
|
with conn:
|
|
tx_id = _write_transaction(conn, tx_id, tx, picks)
|
|
_sync_transfer_pair(conn, tx_id, previous, tx)
|
|
ledger.rebuild(conn)
|
|
except ledger.LedgerError as ledger_error:
|
|
raise ApiError(400, str(ledger_error), "leg_out_sats")
|
|
return tx_id
|
|
finally:
|
|
conn.close()
|
|
|
|
def _transaction_payload(self, tx_id):
|
|
conn = database.connect()
|
|
try:
|
|
return _load_transaction(conn, tx_id)
|
|
finally:
|
|
conn.close()
|
|
|
|
# -- lots, prices and settings ----------------------------------------
|
|
|
|
def lots_list(self, params, query, body):
|
|
market_id = _as_int(query.get("market_id", [""])[0], "market_id")
|
|
wanted = query.get("date", [""])[0] or _today()
|
|
return ledger.candidate_lots(market_id, wanted)
|
|
|
|
def prices_list(self, params, query, body):
|
|
return rates.list_rates()
|
|
|
|
def prices_put(self, params, query, body):
|
|
if str(body.get("nok_per_btc") or "").strip():
|
|
ore, error = validation.validate_ore(body["nok_per_btc"])
|
|
if error:
|
|
raise ApiError(400, error, "nok_per_btc")
|
|
elif body.get("nok_per_btc_ore") is not None:
|
|
ore = _as_int(body["nok_per_btc_ore"], "nok_per_btc_ore",
|
|
"Kursen må vere eit heiltal i øre.")
|
|
if ore <= 0:
|
|
raise ApiError(400, "Kursen må vere større enn null.", "nok_per_btc_ore")
|
|
else:
|
|
raise ApiError(400, "Fyll ut kursen.", "nok_per_btc")
|
|
source = body.get("source")
|
|
rates.set_rate(
|
|
params["date"], ore, "" if source is None else str(source).strip()
|
|
)
|
|
return rates.list_rates()
|
|
|
|
def settings_get(self, params, query, body):
|
|
return database.all_settings()
|
|
|
|
def settings_patch(self, params, query, body):
|
|
settings, errors = validation.validate_settings(body)
|
|
if errors:
|
|
_raise_errors(errors)
|
|
before = database.get_setting("lot_method")
|
|
for key, value in settings.items():
|
|
database.set_setting(key, value)
|
|
if settings.get("lot_method") and settings["lot_method"] != before:
|
|
ledger.rebuild()
|
|
return database.all_settings()
|
|
|
|
|
|
# -- the year report --------------------------------------------------
|
|
|
|
def report_get(self, params, query, body):
|
|
return tax.report(_parse_year(query.get("year", [""])[0]))
|
|
|
|
def report_export(self, params, query, body):
|
|
year = _parse_year(query.get("year", [""])[0])
|
|
data = (export.report_csv(year) + export.transactions_csv(year)).encode("utf-8")
|
|
return self._send_bytes(
|
|
data,
|
|
"text/csv; charset=utf-8",
|
|
headers={
|
|
"Content-Disposition":
|
|
'attachment; filename="skuld-%d.csv"' % year
|
|
},
|
|
)
|
|
|
|
|
|
def main():
|
|
"""Serve until the process is stopped."""
|
|
port = int(os.environ.get("PORT") or DEFAULT_PORT)
|
|
database.connect().close()
|
|
server = ThreadingHTTPServer((DEFAULT_HOST, port), SkuldHandler)
|
|
print("Skuld listening on %s:%d" % (DEFAULT_HOST, port), flush=True)
|
|
try:
|
|
server.serve_forever()
|
|
except KeyboardInterrupt:
|
|
pass
|
|
finally:
|
|
server.server_close()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|