feat(koeb-nu): read-only "buy now" shopping status vs current offers

Add a /koeb-nu page that analyses the list without touching quantities:
- which fixed items are on offer in Lübeck right now (honest per-unit
  offer price in EUR + DKK, no fabricated saving vs the Danish-origin
  estimate, since the units differ)
- what still needs ticking off, grouped by section (+ picked offers)
- a selection of this week's cheapest Tilbud specials not on the list,
  linking to the full Tilbud page
Header gets a 🛒 Køb nu link. The source carries no pre-price, so no
discount % is shown.
This commit is contained in:
Henrik Jess Nielsen
2026-10-30 10:43:22 +01:00
parent 65a99d2138
commit 3b0ea28db5
3 changed files with 220 additions and 0 deletions

59
app.py
View File

@@ -616,6 +616,65 @@ def offer_refresh():
return jsonify(ok=True, **summary)
@app.route('/koeb-nu')
def koeb_nu():
with get_db() as c:
saved = {r['key']: dict(r) for r in c.execute("SELECT * FROM prices")}
offers = [dict(r) for r in c.execute("SELECT * FROM offers")]
picks = [dict(r) for r in c.execute(
"SELECT * FROM offers WHERE picked=1 ORDER BY catalog, heading")]
deals = match_catalog_offers(offers) if offers else {}
# 1) Your fixed items currently on offer. No "saving vs estimate" — the
# estimate is the whole line (e.g. 6 × ½ kg) while the offer price is per
# unit (e.g. per 100 g), so a kr-diff would be misleading. Show the honest
# offer price + unit instead.
on_offer = []
for sec in SECTIONS:
for item in sec['varer']:
d = deals.get(item['key'])
if not d:
continue
on_offer.append({
'item': item, 'sec': sec, 'offer': d,
'offer_dkk': (d.get('price_eur') or 0) * EUR_DKK,
'bought': bool(saved.get(item['key'], {}).get('checked')),
})
on_offer.sort(key=lambda r: (r['bought'], -(r['offer'].get('price_eur') or 0)))
# 2) What still needs to be ticked off (fixed items + picked offers)
remaining = []
for sec in SECTIONS:
items = [it for it in sec['varer']
if not saved.get(it['key'], {}).get('checked')]
if items:
remaining.append({'sec': sec, 'varer': items})
picks_remaining = [p for p in picks if not saved.get(p['id'], {}).get('checked')]
# 3) A taste of this week's headline "Tilbud" specials not already on the
# list (the source carries no pre-price, so there is no discount % to rank
# by; show the cheapest finds and link to the full Tilbud page).
on_list_ids = set(deals[k]['id'] for k in deals) | set(p['id'] for p in picks)
other = []
for o in offers:
if o.get('catalog') == 'Tilbud' and o['id'] not in on_list_ids and o.get('price_eur'):
o = dict(o)
o['dkk'] = o['price_eur'] * EUR_DKK
other.append(o)
other.sort(key=lambda o: o['price_eur'])
other = other[:25]
total = sum(len(sec['varer']) for sec in SECTIONS) + len(picks)
bought = sum(1 for sec in SECTIONS for it in sec['varer']
if saved.get(it['key'], {}).get('checked')) \
+ sum(1 for p in picks if saved.get(p['id'], {}).get('checked'))
return render_template('koeb_nu.html', on_offer=on_offer, remaining=remaining,
picks_remaining=picks_remaining, other=other,
eur_dkk=EUR_DKK, total=total, bought=bought,
has_offers=bool(offers))
@app.route('/analyse')
def analyse():
with get_db() as c: