Flask app progress
This commit is contained in:
parent
c4fe022d42
commit
b0994069ad
38 changed files with 6631 additions and 220 deletions
139
docker/router-dash/app/action_apply_ddns_providers.py
Normal file
139
docker/router-dash/app/action_apply_ddns_providers.py
Normal file
|
|
@ -0,0 +1,139 @@
|
|||
from flask import Blueprint, request, redirect, flash
|
||||
from auth import require_level
|
||||
import json
|
||||
|
||||
bp = Blueprint('action_apply_ddns_providers', __name__)
|
||||
|
||||
DDNS_FILE = '/configs/ddns.json'
|
||||
|
||||
|
||||
@bp.route('/action/add_ddns_provider', methods=['POST'])
|
||||
@require_level('administrator')
|
||||
def add_ddns_provider():
|
||||
provider_type = request.form.get('provider', '').strip().lower()
|
||||
description = request.form.get('description', '').strip()
|
||||
hostnames_raw = request.form.get('hostnames', '')
|
||||
hostnames = [h.strip() for h in hostnames_raw.splitlines() if h.strip()]
|
||||
|
||||
if not description:
|
||||
flash('Description is required.', 'error')
|
||||
return redirect('/view/view_ddns')
|
||||
if not hostnames:
|
||||
flash('At least one hostname is required.', 'error')
|
||||
return redirect('/view/view_ddns')
|
||||
if provider_type not in ('noip', 'cloudflare', 'duckdns'):
|
||||
flash('Unknown provider type.', 'error')
|
||||
return redirect('/view/view_ddns')
|
||||
|
||||
try:
|
||||
with open(DDNS_FILE) as f:
|
||||
data = json.load(f)
|
||||
except Exception as ex:
|
||||
flash(f'Could not read config: {ex}', 'error')
|
||||
return redirect('/view/view_ddns')
|
||||
|
||||
entry = {
|
||||
'description': description,
|
||||
'provider': provider_type,
|
||||
'enabled': True,
|
||||
'hostnames': hostnames,
|
||||
}
|
||||
if provider_type == 'noip':
|
||||
entry['username'] = request.form.get('username', '').strip()
|
||||
entry['password'] = request.form.get('password', '').strip()
|
||||
else:
|
||||
entry['api_token'] = request.form.get('api_token', '').strip()
|
||||
|
||||
data.setdefault('providers', []).append(entry)
|
||||
|
||||
try:
|
||||
with open(DDNS_FILE, 'w') as f:
|
||||
json.dump(data, f, indent=2)
|
||||
flash(f'DDNS provider "{description}" added.', 'success')
|
||||
except Exception as ex:
|
||||
flash(f'Could not save config: {ex}', 'error')
|
||||
return redirect('/view/view_ddns')
|
||||
|
||||
|
||||
@bp.route('/action/edit_ddns_provider', methods=['POST'])
|
||||
@require_level('administrator')
|
||||
def edit_ddns_provider():
|
||||
try:
|
||||
row_index = int(request.form.get('row_index', -1))
|
||||
except (TypeError, ValueError):
|
||||
flash('Invalid row index.', 'error')
|
||||
return redirect('/view/view_ddns')
|
||||
|
||||
provider_type = request.form.get('provider', '').strip().lower()
|
||||
description = request.form.get('description', '').strip()
|
||||
hostnames_raw = request.form.get('hostnames', '')
|
||||
enabled = request.form.get('enabled') == 'on'
|
||||
hostnames = [h.strip() for h in hostnames_raw.splitlines() if h.strip()]
|
||||
|
||||
try:
|
||||
with open(DDNS_FILE) as f:
|
||||
data = json.load(f)
|
||||
except Exception as ex:
|
||||
flash(f'Could not read config: {ex}', 'error')
|
||||
return redirect('/view/view_ddns')
|
||||
|
||||
providers = data.get('providers', [])
|
||||
if row_index < 0 or row_index >= len(providers):
|
||||
flash('Invalid provider index.', 'error')
|
||||
return redirect('/view/view_ddns')
|
||||
|
||||
entry = {
|
||||
'description': description,
|
||||
'provider': provider_type,
|
||||
'enabled': enabled,
|
||||
'hostnames': hostnames,
|
||||
}
|
||||
if provider_type == 'noip':
|
||||
entry['username'] = request.form.get('username', '').strip()
|
||||
entry['password'] = request.form.get('password', '').strip()
|
||||
else:
|
||||
entry['api_token'] = request.form.get('api_token', '').strip()
|
||||
|
||||
providers[row_index] = entry
|
||||
data['providers'] = providers
|
||||
|
||||
try:
|
||||
with open(DDNS_FILE, 'w') as f:
|
||||
json.dump(data, f, indent=2)
|
||||
flash('DDNS provider updated.', 'success')
|
||||
except Exception as ex:
|
||||
flash(f'Could not save config: {ex}', 'error')
|
||||
return redirect('/view/view_ddns')
|
||||
|
||||
|
||||
@bp.route('/action/delete_ddns_provider', methods=['POST'])
|
||||
@require_level('administrator')
|
||||
def delete_ddns_provider():
|
||||
try:
|
||||
row_index = int(request.form.get('row_index', -1))
|
||||
except (TypeError, ValueError):
|
||||
flash('Invalid row index.', 'error')
|
||||
return redirect('/view/view_ddns')
|
||||
|
||||
try:
|
||||
with open(DDNS_FILE) as f:
|
||||
data = json.load(f)
|
||||
except Exception as ex:
|
||||
flash(f'Could not read config: {ex}', 'error')
|
||||
return redirect('/view/view_ddns')
|
||||
|
||||
providers = data.get('providers', [])
|
||||
if row_index < 0 or row_index >= len(providers):
|
||||
flash('Invalid provider index.', 'error')
|
||||
return redirect('/view/view_ddns')
|
||||
|
||||
del providers[row_index]
|
||||
data['providers'] = providers
|
||||
|
||||
try:
|
||||
with open(DDNS_FILE, 'w') as f:
|
||||
json.dump(data, f, indent=2)
|
||||
flash('DDNS provider deleted.', 'success')
|
||||
except Exception as ex:
|
||||
flash(f'Could not save config: {ex}', 'error')
|
||||
return redirect('/view/view_ddns')
|
||||
Loading…
Add table
Add a link
Reference in a new issue