04/23 Fix smtp issue
This commit is contained in:
@@ -581,7 +581,15 @@ class UserDashboardView(ttk.Frame):
|
||||
self._notify_job = self.after(60_000, self._schedule_notifications)
|
||||
|
||||
def _check_notifications(self):
|
||||
"""Fire a desktop notification if a shift ends within NOTIFY_MINUTES_BEFORE."""
|
||||
"""
|
||||
Fire a single consolidated desktop notification listing ALL unchecked
|
||||
sites whose shift ends within NOTIFY_MINUTES_BEFORE minutes.
|
||||
|
||||
Previously the loop fired one notification per site, which could
|
||||
produce a burst of popups when multiple sites were unchecked.
|
||||
Now one notification is fired per shift-end-time group, listing all
|
||||
unchecked sites for that shift together.
|
||||
"""
|
||||
from models import get_unchecked_sites_for_user
|
||||
import datetime as dt
|
||||
try:
|
||||
@@ -590,6 +598,10 @@ class UserDashboardView(ttk.Frame):
|
||||
return
|
||||
|
||||
now = dt.datetime.now().time()
|
||||
|
||||
# Group unchecked sites by their shift end_time so one notification
|
||||
# covers all sites in the same shift.
|
||||
groups = {} # end_t -> {"diff": int, "names": [str]}
|
||||
for site in unchecked:
|
||||
end_time = site.get("end_time")
|
||||
if not end_time:
|
||||
@@ -606,20 +618,41 @@ class UserDashboardView(ttk.Frame):
|
||||
except Exception:
|
||||
continue
|
||||
|
||||
# Compute minutes until shift end
|
||||
now_mins = now.hour * 60 + now.minute
|
||||
end_mins = end_t.hour * 60 + end_t.minute
|
||||
diff = end_mins - now_mins
|
||||
|
||||
key = (site["id"], end_t)
|
||||
if 0 < diff <= NOTIFY_MINUTES_BEFORE and key not in self._notified_sites:
|
||||
self._notified_sites.add(key)
|
||||
self._fire_notification(site["name"], diff, end_t)
|
||||
if 0 < diff <= NOTIFY_MINUTES_BEFORE:
|
||||
if end_t not in groups:
|
||||
groups[end_t] = {"diff": diff, "names": []}
|
||||
groups[end_t]["names"].append(site["name"])
|
||||
|
||||
def _fire_notification(self, site_name: str, minutes_left: int, end_time):
|
||||
title = "Shift Reminder"
|
||||
message = (f"'{site_name}' is unchecked — "
|
||||
f"shift ends at {end_time.strftime('%H:%M')} "
|
||||
# Fire one notification per end-time group that has not been notified yet
|
||||
for end_t, info in groups.items():
|
||||
key = (frozenset(info["names"]), end_t)
|
||||
if key in self._notified_sites:
|
||||
continue
|
||||
self._notified_sites.add(key)
|
||||
self._fire_notification(info["names"], info["diff"], end_t)
|
||||
|
||||
def _fire_notification(self, site_names: list, minutes_left: int, end_time):
|
||||
"""
|
||||
Fire one consolidated desktop notification for all unchecked sites
|
||||
in a shift group.
|
||||
site_names: list of unchecked website names
|
||||
"""
|
||||
title = "Shift Reminder — Unchecked Sites"
|
||||
count = len(site_names)
|
||||
if count == 1:
|
||||
body = f"{site_names[0]} is unchecked."
|
||||
else:
|
||||
# Show up to 3 names inline; append "+ N more" if longer
|
||||
preview = ", ".join(site_names[:3])
|
||||
body = (f"{preview}" if count <= 3
|
||||
else f"{preview} + {count - 3} more")
|
||||
body = f"{count} sites unchecked: {body}."
|
||||
message = (f"{body} Shift ends at "
|
||||
f"{end_time.strftime('%H:%M')} "
|
||||
f"({minutes_left} min remaining).")
|
||||
logger.info(f"Notification: {message}")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user