migrate to table based tinydb

NOTE to migrate database install tinydb & run migrate.py

- added new functions to filter data with db queries
- in the storage module
This commit is contained in:
2025-08-27 20:39:04 +02:00
parent 0339da1668
commit 36f7f6d278
9 changed files with 2073 additions and 2335 deletions

View File

@@ -2,18 +2,14 @@
from __future__ import annotations
import time
from flask import Blueprint, render_template, redirect, url_for
from webapp.storage.orders import load_orders, update_order
from webapp.storage.orders import load_archived, mark_archived
bp = Blueprint("archive", __name__)
@bp.get("/archiv")
def archive_index():
"""Zeigt ausschließlich Aufträge mit status == 'archived'."""
archived = [o for o in load_orders() if o.get("status") == "archived"]
archived.sort(
key=lambda o: (o.get("archived_at") or o.get("done_at") or o.get("created_at") or ""),
reverse=True,
)
archived = load_archived()
return render_template("archive.html", archived_orders=archived)
@bp.post("/archiv/add/<order_id>")
@@ -23,5 +19,5 @@ def archive_add(order_id: str):
Danach BLEIBEN wir auf der Strukturen-Seite (kein Wechsel zur Archiv-Seite).
"""
now = time.strftime("%Y-%m-%dT%H:%M:%S%z", time.gmtime())
update_order(order_id, {"status": "archived", "archived_at": now})
mark_archived(order_id)
return redirect(url_for("structures.structures")) # zurück zu /strukturen

View File

@@ -10,17 +10,17 @@ from webapp.services.ccookbook import (
)
# Storage
from webapp.storage.orders import (
load_orders, update_order,
cache_costs, cache_materials,
update_order, get_order_by_id,
cache_costs, cache_materials, load_pending
)
bp = Blueprint("cookbook", __name__, url_prefix="/api")
# ----------------------------- Helpers ----------------------------------------
def _now() -> str:
return time.strftime("%Y-%m-%dT%H:%M:%S%z", time.gmtime())
# ----------------------------- Helpers ----------------------------------------
def _arg(name: str, default: str = "") -> str:
v = request.args.get(name)
return v if v is not None else default
@@ -31,12 +31,6 @@ def _to_int(s: str, default: int = 0) -> int:
except Exception:
return default
def _order_by_id(order_id: str) -> Dict[str, Any] | None:
for o in load_orders():
if o.get("id") == order_id:
return o
return None
# ----------------------------- Endpoints Raw --------------------------------
@bp.get("/cookbook/build_cost")
@@ -113,7 +107,7 @@ def api_cache(order_id: str):
@bp.post("/order/<order_id>/refresh")
def api_refresh_one(order_id: str):
o = _order_by_id(order_id)
o = get_order_by_id(order_id)
if not o:
return jsonify({"error": "order not found"}), 404
bp_id = o.get("blueprint_type_id")
@@ -172,7 +166,7 @@ def api_refresh_one(order_id: str):
@bp.post("/orders/refresh_all")
def api_refresh_all():
orders = [o for o in load_orders() if o.get("status") == "open" and o.get("blueprint_type_id")]
orders = [o for o in load_pending() if o.get("blueprint_type_id")]
done = 0
for o in orders:
try:
@@ -195,8 +189,8 @@ def api_open_costs():
"""
items: List[Dict[str, Any]] = []
total = 0.0
for o in load_orders():
if o.get("status") != "open" or not o.get("blueprint_type_id"):
for o in load_pending():
if not o.get("blueprint_type_id"):
continue
entry: Dict[str, Any] = {
@@ -258,8 +252,8 @@ def api_open_materials():
if cost is not None:
row["cost"] += float(cost or 0)
for o in load_orders():
if o.get("status") != "open" or not o.get("blueprint_type_id"):
for o in load_pending():
if not o.get("blueprint_type_id"):
continue
mats = o.get("materials")

View File

@@ -3,7 +3,7 @@ import uuid, time
from typing import List, Dict, Any
from flask import Blueprint, render_template, request, redirect, url_for
from webapp.storage.orders import load_orders, add_order, mark_done
from webapp.storage.orders import load_pending,load_done, add_order, mark_done
from webapp.services.blueprints import resolve_blueprint_id
bp = Blueprint("structures", __name__)
@@ -16,12 +16,7 @@ REAC_STRUCTS= ["Athanor", "Tatara"]
REAC_RIGS = ["None", "T1", "T2"]
def _split_orders() -> Dict[str, List[Dict[str, Any]]]:
orders = load_orders()
open_orders = [o for o in orders if o.get("status") == "open"]
done_orders = [o for o in orders if o.get("status") == "done"]
open_orders.sort(key=lambda o: o.get("created_at",""), reverse=True)
done_orders.sort(key=lambda o: o.get("done_at","") or "", reverse=True)
return {"open": open_orders, "done": done_orders}
return {"open": load_pending(), "done": load_done()}
@bp.route("/strukturen", methods=["GET", "POST"])
def structures():