Development
This commit is contained in:
parent
60df03e85c
commit
22c18d9edd
4 changed files with 72 additions and 39 deletions
|
|
@ -1,7 +1,8 @@
|
|||
from flask import Blueprint, request, redirect, flash, session
|
||||
from auth import require_level
|
||||
from config_utils import (flush_pending_to_queue, get_dashboard_pending,
|
||||
_is_locked, _format_timing, _seconds_until_next_run)
|
||||
from config_utils import (flush_selected_to_queue, delete_pending_by_uuids,
|
||||
get_dashboard_pending, _is_locked, _format_timing,
|
||||
_seconds_until_next_run)
|
||||
|
||||
bp = Blueprint('action_actions', __name__)
|
||||
|
||||
|
|
@ -19,11 +20,11 @@ def actions_cardoptions_save():
|
|||
@bp.route('/action/actions_cardpendingchanges_applyselected', methods=['POST'])
|
||||
@require_level('administrator')
|
||||
def actions_cardpendingchanges_applyselected():
|
||||
items = get_dashboard_pending()
|
||||
if not items:
|
||||
flash('No pending changes to apply.', 'info')
|
||||
selected_uuids = request.form.getlist('selected_uuids')
|
||||
if not selected_uuids:
|
||||
flash('No items selected.', 'info')
|
||||
return redirect(_VIEW)
|
||||
flush_pending_to_queue()
|
||||
flush_selected_to_queue(selected_uuids)
|
||||
if _is_locked():
|
||||
msg = 'Changes queued. They are being applied now.'
|
||||
else:
|
||||
|
|
@ -39,5 +40,10 @@ def actions_cardpendingchanges_applyselected():
|
|||
@bp.route('/action/actions_cardpendingchanges_revertselected', methods=['POST'])
|
||||
@require_level('administrator')
|
||||
def actions_cardpendingchanges_revertselected():
|
||||
flash('Not yet implemented.', 'info')
|
||||
selected_uuids = request.form.getlist('selected_uuids')
|
||||
if not selected_uuids:
|
||||
flash('No items selected.', 'info')
|
||||
return redirect(_VIEW)
|
||||
delete_pending_by_uuids(selected_uuids)
|
||||
flash('Selected changes reverted.', 'success')
|
||||
return redirect(_VIEW)
|
||||
|
|
|
|||
|
|
@ -67,9 +67,9 @@ def _read_pending(done_set):
|
|||
return pending
|
||||
for line in lines:
|
||||
try:
|
||||
parts = line.split(None, 3)
|
||||
if len(parts) == 4:
|
||||
entry_uuid, entry_ts, _dt, rest = parts
|
||||
parts = line.split(None, 2)
|
||||
if len(parts) == 3:
|
||||
entry_uuid, entry_ts, rest = parts
|
||||
cmd_user = rest.rsplit(' (', 1)
|
||||
entry_cmd = cmd_user[0].strip('[]')
|
||||
entry_user = cmd_user[1].rstrip(')') if len(cmd_user) == 2 else ''
|
||||
|
|
@ -128,9 +128,9 @@ def _read_dashboard_pending():
|
|||
continue
|
||||
try:
|
||||
main, _, desc = line.partition(' :: ')
|
||||
parts = main.split(None, 3)
|
||||
if len(parts) == 4:
|
||||
entry_uuid, entry_ts, _dt, rest = parts
|
||||
parts = main.split(None, 2)
|
||||
if len(parts) == 3:
|
||||
entry_uuid, entry_ts, rest = parts
|
||||
cmd_user = rest.rsplit(' (', 1)
|
||||
entry_cmd = cmd_user[0].strip('[]')
|
||||
entry_user = cmd_user[1].rstrip(')') if len(cmd_user) == 2 else ''
|
||||
|
|
@ -154,12 +154,42 @@ def flush_pending_to_queue():
|
|||
with open(DASHBOARD_QUEUE, 'a') as f:
|
||||
for entry_uuid, entry_ts, entry_cmd, entry_user, _desc in items:
|
||||
if entry_uuid not in existing_ids:
|
||||
dt_str = datetime.fromtimestamp(entry_ts).strftime('%Y-%m-%dT%H:%M:%S')
|
||||
f.write(f'{entry_uuid} {entry_ts} {dt_str} [{entry_cmd}] ({entry_user})\n')
|
||||
f.write(f'{entry_uuid} {entry_ts} [{entry_cmd}] ({entry_user})\n')
|
||||
open(DASHBOARD_PENDING, 'w').close()
|
||||
_trim_if_needed()
|
||||
|
||||
|
||||
def _remove_pending_by_uuids(uuid_set):
|
||||
try:
|
||||
lines = open(DASHBOARD_PENDING).read().splitlines()
|
||||
except Exception:
|
||||
return
|
||||
kept = [l for l in lines if l.strip() and l.split(None, 1)[0] not in uuid_set]
|
||||
with open(DASHBOARD_PENDING, 'w') as f:
|
||||
f.write('\n'.join(kept) + ('\n' if kept else ''))
|
||||
|
||||
|
||||
def flush_selected_to_queue(selected_uuids):
|
||||
if not selected_uuids:
|
||||
return
|
||||
selected_set = set(selected_uuids)
|
||||
items = _read_dashboard_pending()
|
||||
done_set = _load_done_set()
|
||||
existing_ids = {uu for uu, *_ in _read_pending(done_set)}
|
||||
with open(DASHBOARD_QUEUE, 'a') as f:
|
||||
for entry_uuid, entry_ts, entry_cmd, entry_user, _desc in items:
|
||||
if entry_uuid in selected_set and entry_uuid not in existing_ids:
|
||||
f.write(f'{entry_uuid} {entry_ts} [{entry_cmd}] ({entry_user})\n')
|
||||
_remove_pending_by_uuids(selected_set)
|
||||
_trim_if_needed()
|
||||
|
||||
|
||||
def delete_pending_by_uuids(selected_uuids):
|
||||
if not selected_uuids:
|
||||
return
|
||||
_remove_pending_by_uuids(set(selected_uuids))
|
||||
|
||||
|
||||
def _queue_pending_command(cmd, description=''):
|
||||
"""Append cmd to .dashboard-pending if not already present for this cmd+user."""
|
||||
existing = _read_dashboard_pending()
|
||||
|
|
@ -170,10 +200,9 @@ def _queue_pending_command(cmd, description=''):
|
|||
entry_uuid = str(uuid.uuid4())
|
||||
now = datetime.now()
|
||||
entry_ts = int(now.timestamp())
|
||||
dt_str = now.strftime('%Y-%m-%dT%H:%M:%S')
|
||||
desc_suffix = f' :: {description}' if description else ''
|
||||
with open(DASHBOARD_PENDING, 'a') as f:
|
||||
f.write(f'{entry_uuid} {entry_ts} {dt_str} [{cmd}] ({current_user}){desc_suffix}\n')
|
||||
f.write(f'{entry_uuid} {entry_ts} [{cmd}] ({current_user}){desc_suffix}\n')
|
||||
return entry_uuid, entry_ts
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -597,12 +597,20 @@ def collect_tokens():
|
|||
for _uuid, ts, cmd, user, desc in pending_items:
|
||||
dt_str = datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M')
|
||||
label = e(desc) if desc else e(cmd)
|
||||
rows += (f'<tr><td class="table-cell">{e(dt_str)}</td>'
|
||||
rows += (f'<tr>'
|
||||
f'<td class="table-cell"><input type="checkbox" name="selected_uuids" value="{e(_uuid)}"/></td>'
|
||||
f'<td class="table-cell">{e(dt_str)}</td>'
|
||||
f'<td class="table-cell">{label}</td>'
|
||||
f'<td class="table-cell">{e(user)}</td></tr>')
|
||||
f'<td class="table-cell">{e(user)}</td>'
|
||||
f'</tr>')
|
||||
select_all = (
|
||||
'<input type="checkbox" '
|
||||
'onchange="document.querySelectorAll(\'[name=selected_uuids]\').forEach(c=>c.checked=this.checked)"/>'
|
||||
)
|
||||
pending_html = (
|
||||
'<table class="data-table" style="margin-bottom:1rem">'
|
||||
'<thead><tr>'
|
||||
f'<th class="table-header">{select_all}</th>'
|
||||
'<th class="table-header">Time</th>'
|
||||
'<th class="table-header">Change</th>'
|
||||
'<th class="table-header">User</th>'
|
||||
|
|
@ -833,9 +841,13 @@ def _render_item(item, tokens, inherited_req=None):
|
|||
extra = item.get('class', '')
|
||||
if extra:
|
||||
cls = f'{cls} {extra}'
|
||||
text = e(apply_tokens(item.get('text', ''), tokens))
|
||||
action = e(apply_tokens(item.get('action', '#'), tokens))
|
||||
disabled = ' disabled' if item.get('disabled') else ''
|
||||
text = e(apply_tokens(item.get('text', ''), tokens))
|
||||
action = e(apply_tokens(item.get('action', '#'), tokens))
|
||||
disabled = ' disabled' if item.get('disabled') else ''
|
||||
formaction = item.get('formaction', '')
|
||||
if formaction:
|
||||
formaction = e(apply_tokens(formaction, tokens))
|
||||
return f'<button type="submit" class="btn {e(cls)}" formaction="{formaction}"{disabled}>{text}</button>'
|
||||
if item.get('method', '').lower() == 'post':
|
||||
return (f'<form method="post" action="{action}" style="display:inline">'
|
||||
f'<button type="submit" class="btn {e(cls)}"{disabled}>{text}</button></form>')
|
||||
|
|
|
|||
|
|
@ -620,26 +620,12 @@
|
|||
"items": [
|
||||
{
|
||||
"type": "button_primary",
|
||||
"action": "/action/actions_cardpendingchanges_applyselected",
|
||||
"method": "post",
|
||||
"formaction": "/action/actions_cardpendingchanges_applyselected",
|
||||
"text": "Apply Selected"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"type": "form",
|
||||
"action": "/action/actions_cardpendingchanges_revertselected",
|
||||
"method": "post",
|
||||
"items": [
|
||||
{
|
||||
"type": "button_row",
|
||||
"items": [
|
||||
},
|
||||
{
|
||||
"type": "button_secondary",
|
||||
"action": "/action/actions_cardpendingchanges_revertselected",
|
||||
"method": "post",
|
||||
"formaction": "/action/actions_cardpendingchanges_revertselected",
|
||||
"text": "Revert Selected"
|
||||
}
|
||||
]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue