diff --git a/app.py b/app.py index 2bdbfbd..22167a3 100644 --- a/app.py +++ b/app.py @@ -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: diff --git a/templates/index.html b/templates/index.html index d4ec4f6..6f33465 100644 --- a/templates/index.html +++ b/templates/index.html @@ -176,6 +176,7 @@