05/23 Phase 7
This commit is contained in:
+59
-2
@@ -5,10 +5,14 @@ Draw History browser screen.
|
||||
Shows a sortable, filterable Treeview of all draw records.
|
||||
"""
|
||||
|
||||
import os
|
||||
import tkinter as tk
|
||||
from tkinter import ttk
|
||||
from tkinter import ttk, filedialog, messagebox
|
||||
|
||||
from db.models import get_all_games, get_game_by_name, get_draws_with_game
|
||||
from core.exporter import (
|
||||
export_draws_excel, export_draws_csv, default_path, ensure_exports_dir,
|
||||
)
|
||||
|
||||
_COLUMNS = ("game", "date", "numbers", "bonus", "multiplier", "source")
|
||||
_LABELS = {
|
||||
@@ -77,7 +81,9 @@ class HistoryScreen(ttk.Frame):
|
||||
ttk.Button(bar, text="Search", command=self._apply_filter).pack(side="left", padx=(0, 4))
|
||||
ttk.Button(bar, text="Clear", command=self._clear_filter).pack(side="left")
|
||||
|
||||
ttk.Button(bar, text="↻ Refresh", command=self.refresh).pack(side="right")
|
||||
ttk.Button(bar, text="↻ Refresh", command=self.refresh).pack(side="right")
|
||||
ttk.Button(bar, text="Export CSV", command=self._export_csv).pack(side="right", padx=(0, 4))
|
||||
ttk.Button(bar, text="Export Excel",command=self._export_excel).pack(side="right", padx=(0, 4))
|
||||
|
||||
# ── Treeview + scrollbars ─────────────────────────────────────────────
|
||||
tree_frame = ttk.Frame(self)
|
||||
@@ -213,3 +219,54 @@ class HistoryScreen(ttk.Frame):
|
||||
for c in _COLUMNS:
|
||||
self._tree.heading(c, text=_LABELS[c])
|
||||
self._apply_filter()
|
||||
|
||||
# ── Export ────────────────────────────────────────────────────────────────
|
||||
|
||||
def _current_filter(self):
|
||||
"""Return (game_id, date_from, date_to) for the active filter."""
|
||||
name = self._game_var.get()
|
||||
if name == "All Games":
|
||||
game_id = None
|
||||
else:
|
||||
g = get_game_by_name(name)
|
||||
game_id = g["id"] if g else None
|
||||
|
||||
raw_from = self._from_entry.get().strip()
|
||||
raw_to = self._to_entry.get().strip()
|
||||
date_from = raw_from if raw_from and raw_from != "YYYY-MM-DD" else None
|
||||
date_to = raw_to if raw_to and raw_to != "YYYY-MM-DD" else None
|
||||
return game_id, date_from, date_to
|
||||
|
||||
def _export_excel(self):
|
||||
ensure_exports_dir()
|
||||
fp = filedialog.asksaveasfilename(
|
||||
title="Export draw history",
|
||||
defaultextension=".xlsx",
|
||||
filetypes=[("Excel workbook", "*.xlsx")],
|
||||
initialfile=f"draws_{__import__('datetime').datetime.now().strftime('%Y%m%d')}.xlsx",
|
||||
)
|
||||
if not fp:
|
||||
return
|
||||
try:
|
||||
game_id, date_from, date_to = self._current_filter()
|
||||
export_draws_excel(fp, game_id=game_id, date_from=date_from, date_to=date_to)
|
||||
self._count_var.set(f"Exported → {os.path.basename(fp)}")
|
||||
except Exception as e:
|
||||
messagebox.showerror("Export failed", str(e))
|
||||
|
||||
def _export_csv(self):
|
||||
ensure_exports_dir()
|
||||
fp = filedialog.asksaveasfilename(
|
||||
title="Export draw history",
|
||||
defaultextension=".csv",
|
||||
filetypes=[("CSV file", "*.csv")],
|
||||
initialfile=f"draws_{__import__('datetime').datetime.now().strftime('%Y%m%d')}.csv",
|
||||
)
|
||||
if not fp:
|
||||
return
|
||||
try:
|
||||
game_id, date_from, date_to = self._current_filter()
|
||||
export_draws_csv(fp, game_id=game_id, date_from=date_from, date_to=date_to)
|
||||
self._count_var.set(f"Exported → {os.path.basename(fp)}")
|
||||
except Exception as e:
|
||||
messagebox.showerror("Export failed", str(e))
|
||||
|
||||
Reference in New Issue
Block a user