lets deploy and see
This commit is contained in:
parent
aeaac5fd4e
commit
64b54e959e
@ -53,9 +53,10 @@ def send_expiry_notifications(app) -> None:
|
|||||||
|
|
||||||
for account in expiring_accounts:
|
for account in expiring_accounts:
|
||||||
expiry_date = datetime.fromtimestamp(account['expiaryDate'])
|
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:
|
if days_to_expiry == 30 or days_to_expiry == 7:
|
||||||
|
print(f"Found expiring account: {account['username']}")
|
||||||
user_id = account['user_id']
|
user_id = account['user_id']
|
||||||
subscriptions = get_push_subscriptions(user_id)
|
subscriptions = get_push_subscriptions(user_id)
|
||||||
for sub in subscriptions:
|
for sub in subscriptions:
|
||||||
@ -67,8 +68,11 @@ def send_expiry_notifications(app) -> None:
|
|||||||
if last_notified and last_notified.date() == now.date():
|
if last_notified and last_notified.date() == now.date():
|
||||||
continue
|
continue
|
||||||
|
|
||||||
message = f"Your account {account['username']} is due to expire in {days_to_expiry} days."
|
message = {
|
||||||
send_notification(sub['subscription_json'], 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 the last notified timestamp
|
||||||
update_last_notified_query = "UPDATE push_subscriptions SET last_notified = %s WHERE id = %s"
|
update_last_notified_query = "UPDATE push_subscriptions SET last_notified = %s WHERE id = %s"
|
||||||
|
@ -237,3 +237,14 @@ def get_push_subscriptions(user_id: Optional[int] = None) -> List[Dict[str, Any]
|
|||||||
else:
|
else:
|
||||||
query = "SELECT * FROM push_subscriptions"
|
query = "SELECT * FROM push_subscriptions"
|
||||||
return _execute_query(query)
|
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)
|
||||||
|
@ -1,8 +1,11 @@
|
|||||||
|
import json
|
||||||
from flask import current_app
|
from flask import current_app
|
||||||
from pywebpush import webpush, WebPushException
|
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:
|
try:
|
||||||
|
subscription_info = json.loads(subscription_json)
|
||||||
webpush(
|
webpush(
|
||||||
subscription_info=subscription_info,
|
subscription_info=subscription_info,
|
||||||
data=message_body,
|
data=message_body,
|
||||||
@ -11,7 +14,6 @@ def send_notification(subscription_info, message_body):
|
|||||||
)
|
)
|
||||||
except WebPushException as ex:
|
except WebPushException as ex:
|
||||||
print(f"Web push error: {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:
|
if ex.response and ex.response.status_code == 410:
|
||||||
print("Subscription is no longer valid, removing from DB.")
|
print("Subscription is no longer valid, removing from DB.")
|
||||||
# Add logic to remove the subscription from your database
|
delete_push_subscription(subscription_json)
|
@ -237,7 +237,7 @@ def send_test_notification_route(username: str, password: str) -> Response:
|
|||||||
|
|
||||||
for sub in subscriptions:
|
for sub in subscriptions:
|
||||||
try:
|
try:
|
||||||
send_notification(json.loads(sub['subscription_json']), message_body)
|
send_notification(sub['subscription_json'], message_body)
|
||||||
success_count += 1
|
success_count += 1
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Error sending notification to subscription ID {sub.get('id', 'N/A')}: {e}")
|
print(f"Error sending notification to subscription ID {sub.get('id', 'N/A')}: {e}")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user