Jun 24 - Implement devices tracker - Update
This commit is contained in:
+7
-16
@@ -8,7 +8,7 @@ from flask import Blueprint, render_template, request, redirect, url_for, flash
|
||||
from flask_login import login_required, current_user
|
||||
|
||||
from app import db
|
||||
from app.models.device_registration import DeviceRegistration
|
||||
from app.models.api_token import DeviceToken
|
||||
from app.models.notification import Notification
|
||||
from app.utils.decorators import admin_required
|
||||
from app.utils.audit import log_action, ACTION_CREATE
|
||||
@@ -24,8 +24,8 @@ bp = Blueprint('devices', __name__, url_prefix='/admin/devices')
|
||||
def index():
|
||||
"""Show all registered devices, most-recently-seen first."""
|
||||
devices = (
|
||||
DeviceRegistration.query
|
||||
.order_by(DeviceRegistration.last_seen_at.desc())
|
||||
DeviceToken.query
|
||||
.order_by(DeviceToken.last_seen_at.desc().nullslast())
|
||||
.all()
|
||||
)
|
||||
return render_template('admin/devices.html', devices=devices)
|
||||
@@ -38,11 +38,6 @@ def notify_update():
|
||||
"""
|
||||
Send an in-app update notice to all users whose app_version is below
|
||||
the version string entered by the admin.
|
||||
|
||||
Form fields
|
||||
-----------
|
||||
current_version str The version string to treat as current (e.g. "1.3.0")
|
||||
message str Optional custom message body (default provided)
|
||||
"""
|
||||
current_version = (request.form.get('current_version') or '').strip()
|
||||
custom_message = (request.form.get('message') or '').strip()
|
||||
@@ -52,7 +47,6 @@ def notify_update():
|
||||
return redirect(url_for('devices.index'))
|
||||
|
||||
def version_tuple(v: str):
|
||||
"""Convert "1.3.0" → (1, 3, 0) for comparison. Non-numeric parts → 0."""
|
||||
try:
|
||||
return tuple(int(x) for x in v.strip().split('.'))
|
||||
except ValueError:
|
||||
@@ -60,16 +54,13 @@ def notify_update():
|
||||
|
||||
target_v = version_tuple(current_version)
|
||||
|
||||
# Find all devices running an older version
|
||||
all_devices = DeviceRegistration.query.all()
|
||||
outdated = [d for d in all_devices if version_tuple(d.app_version) < target_v]
|
||||
all_devices = DeviceToken.query.all()
|
||||
outdated = [d for d in all_devices if version_tuple(d.app_version or '0') < target_v]
|
||||
|
||||
if not outdated:
|
||||
flash(f'No devices found running a version older than {current_version}.', 'info')
|
||||
return redirect(url_for('devices.index'))
|
||||
|
||||
# Deduplicate by user_id — one notification per user even if they have
|
||||
# multiple devices registered (e.g. primary + secondary server devices).
|
||||
seen_users = set()
|
||||
notified = 0
|
||||
for device in outdated:
|
||||
@@ -98,8 +89,8 @@ def notify_update():
|
||||
f'v{current_version} notice → {notified} user(s)',
|
||||
f'outdated_devices={len(outdated)}; sent_by={current_user.username}')
|
||||
|
||||
logger.info('DEVICES | update_notice | version=%s | users_notified=%d | devices_outdated=%d | by=%s',
|
||||
current_version, notified, len(outdated), current_user.username)
|
||||
logger.info('DEVICES | update_notice | version=%s | users_notified=%d | by=%s',
|
||||
current_version, notified, current_user.username)
|
||||
|
||||
flash(
|
||||
f'Update notice sent to {notified} user(s) on {len(outdated)} outdated device(s).',
|
||||
|
||||
Reference in New Issue
Block a user