lets deploy and see

This commit is contained in:
Karl 2025-07-19 11:56:30 +01:00
parent aeaac5fd4e
commit 64b54e959e
4 changed files with 24 additions and 7 deletions

View File

@ -53,9 +53,10 @@ def send_expiry_notifications(app) -> None:
for account in expiring_accounts:
expiry_date = datetime.fromtimestamp(account['expiaryDate'])
days_to_expiry = (expiry_date - now).days
days_to_expiry = (expiry_date.date() - now.date()).days
if days_to_expiry == 30 or days_to_expiry == 7:
print(f"Found expiring account: {account['username']}")
user_id = account['user_id']
subscriptions = get_push_subscriptions(user_id)
for sub in subscriptions:
@ -67,8 +68,11 @@ def send_expiry_notifications(app) -> None:
if last_notified and last_notified.date() == now.date():
continue
message = f"Your account {account['username']} is due to expire in {days_to_expiry} days."
send_notification(sub['subscription_json'], message)
message = {
"title": "Account Expiry Warning",
"body": f"Your account {account['username']} is due to expire in {days_to_expiry} days."
}
send_notification(sub['subscription_json'], json.dumps(message))
# Update the last notified timestamp
update_last_notified_query = "UPDATE push_subscriptions SET last_notified = %s WHERE id = %s"

View File

@ -237,3 +237,14 @@ def get_push_subscriptions(user_id: Optional[int] = None) -> List[Dict[str, Any]
else:
query = "SELECT * FROM push_subscriptions"
return _execute_query(query)
def delete_push_subscription(subscription_json: str) -> None:
"""Deletes a push subscription from the database.
Args:
subscription_json: The push subscription information as a JSON string.
"""
query = "DELETE FROM push_subscriptions WHERE subscription_json = %s"
params = (subscription_json,)
_execute_query(query, params)

View File

@ -1,8 +1,11 @@
import json
from flask import current_app
from pywebpush import webpush, WebPushException
from ktvmanager.lib.database import delete_push_subscription
def send_notification(subscription_info, message_body):
def send_notification(subscription_json, message_body):
try:
subscription_info = json.loads(subscription_json)
webpush(
subscription_info=subscription_info,
data=message_body,
@ -11,7 +14,6 @@ def send_notification(subscription_info, message_body):
)
except WebPushException as ex:
print(f"Web push error: {ex}")
# You might want to remove the subscription if it's invalid
if ex.response and ex.response.status_code == 410:
print("Subscription is no longer valid, removing from DB.")
# Add logic to remove the subscription from your database
delete_push_subscription(subscription_json)

View File

@ -237,7 +237,7 @@ def send_test_notification_route(username: str, password: str) -> Response:
for sub in subscriptions:
try:
send_notification(json.loads(sub['subscription_json']), message_body)
send_notification(sub['subscription_json'], message_body)
success_count += 1
except Exception as e:
print(f"Error sending notification to subscription ID {sub.get('id', 'N/A')}: {e}")