Skip to content

Commit

Permalink
Merge pull request #176 from ben-kaufman/settings-tabs
Browse files Browse the repository at this point in the history
Separate setttings into tabs
  • Loading branch information
stepansnigirev authored Jun 28, 2020
2 parents fe00ae6 + 928c875 commit ad59e8e
Show file tree
Hide file tree
Showing 11 changed files with 336 additions and 202 deletions.
184 changes: 122 additions & 62 deletions src/cryptoadvance/specter/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,44 +181,82 @@ def logout():
app.specter.clear_user_session()
return redirect("/login")

@app.route('/settings/', methods=['GET', 'POST'])
@app.route('/settings/', methods=['GET'])
@login_required
def settings():
if current_user.is_admin:
return redirect("/settings/bitcoin_core")
else:
return redirect("/settings/general")

@app.route('/settings/hwi', methods=['GET'])
@login_required
def hwi_settings():
current_version = notify_upgrade()
app.specter.check()
return render_template(
"settings/hwi_settings.jinja",
specter=app.specter,
current_version=current_version,
rand=rand
)

@app.route('/settings/general', methods=['GET', 'POST'])
@login_required
def general_settings():
current_version = notify_upgrade()
app.specter.check()
explorer = app.specter.explorer
hwi_bridge_url = app.specter.hwi_bridge_url
loglevel = get_loglevel(app)
if request.method == 'POST':
action = request.form['action']
explorer = request.form['explorer']
hwi_bridge_url = request.form['hwi_bridge_url']
if current_user.is_admin:
loglevel = request.form['loglevel']

if action == "save":
if current_user.is_admin:
set_loglevel(app,loglevel)

app.specter.update_explorer(explorer, current_user)
app.specter.update_hwi_bridge_url(hwi_bridge_url, current_user)
app.specter.check()
return render_template(
"settings/general_settings.jinja",
explorer=explorer,
hwi_bridge_url=hwi_bridge_url,
loglevel=loglevel,
specter=app.specter,
current_version=current_version,
rand=rand
)

@app.route('/settings/bitcoin_core', methods=['GET', 'POST'])
@login_required
def bitcoin_core_settings():
current_version = notify_upgrade()
app.specter.check()
if not current_user.is_admin:
flash('Only an admin is allowed to access this page.', 'error')
return redirect("/")
rpc = app.specter.config['rpc']
user = rpc['user']
passwd = rpc['password']
port = rpc['port']
host = rpc['host']
protocol = 'http'
explorer = app.specter.explorer
auth = app.specter.config['auth']
if auth == 'none':
app.login('admin')
hwi_bridge_url = app.specter.hwi_bridge_url
new_otp = -1
loglevel = get_loglevel(app)
if "protocol" in rpc:
protocol = rpc["protocol"]
test = None
if request.method == 'POST':
action = request.form['action']
explorer = request.form['explorer']
hwi_bridge_url = request.form['hwi_bridge_url']
if 'specter_username' in request.form:
specter_username = request.form['specter_username']
specter_password = request.form['specter_password']
else:
specter_username = None
specter_password = None
if current_user.is_admin:
user = request.form['username']
passwd = request.form['password']
port = request.form['port']
host = request.form['host']
auth = request.form['auth']
loglevel = request.form['loglevel']

# protocol://host
if "://" in host:
Expand All @@ -227,78 +265,100 @@ def settings():
host = arr[1]

if action == "test":
test = app.specter.test_rpc(user=user,
password=passwd,
port=port,
host=host,
protocol=protocol,
autodetect=False
)
test = app.specter.test_rpc(
user=user,
password=passwd,
port=port,
host=host,
protocol=protocol,
autodetect=False
)
elif action == "save":
if current_user.is_admin:
app.specter.update_rpc(
user=user,
password=passwd,
port=port,
host=host,
protocol=protocol,
autodetect=False
)
app.specter.check()

return render_template(
"settings/bitcoin_core_settings.jinja",
test=test,
username=user,
password=passwd,
port=port,
host=host,
protocol=protocol,
specter=app.specter,
current_version=current_version,
rand=rand
)

@app.route('/settings/auth', methods=['GET', 'POST'])
@login_required
def auth_settings():
current_version = notify_upgrade()
app.specter.check()
auth = app.specter.config['auth']
if auth == 'none':
app.login('admin')
new_otp = -1
if request.method == 'POST':
action = request.form['action']
if 'specter_username' in request.form:
specter_username = request.form['specter_username']
specter_password = request.form['specter_password']
else:
specter_username = None
specter_password = None
if current_user.is_admin:
auth = request.form['auth']

if action == "save":
if specter_username:
if current_user.username != specter_username:
if User.get_user_by_name(app.specter, specter_username):
flash('Username is already taken, please choose another one', "error")
return render_template("settings.jinja",
test=test,
username=user,
password=passwd,
port=port,
host=host,
protocol=protocol,
explorer=explorer,
return render_template(
"settings/auth_settings.jinja",
auth=auth,
hwi_bridge_url=hwi_bridge_url,
new_otp=new_otp,
loglevel=loglevel,
specter=app.specter,
current_version=current_version,
rand=rand)
rand=rand
)
current_user.username = specter_username
if specter_password:
current_user.password = hash_password(specter_password)
current_user.save_info(app.specter)
if current_user.is_admin:
app.specter.update_rpc( user=user,
password=passwd,
port=port,
host=host,
protocol=protocol,
autodetect=False
)
app.specter.update_auth(auth)
if auth == "rpcpasswordaspin" or auth == "usernamepassword":
app.config['LOGIN_DISABLED'] = False
else:
app.config['LOGIN_DISABLED'] = True
set_loglevel(app,loglevel)

app.specter.update_explorer(explorer, current_user)
app.specter.update_hwi_bridge_url(hwi_bridge_url, current_user)
app.specter.check()
return redirect("/")
elif action == "adduser":
if current_user.is_admin:
new_otp = random.randint(100000, 999999)
app.specter.add_new_user_otp({ 'otp': new_otp, 'created_at': time.time() })
flash('New user link generated successfully: {}register?otp={}'.format(request.url_root, new_otp), 'info')
else:
flash('Error: Only the admin account can issue new registration links.', 'error')
return render_template("settings.jinja",
test=test,
username=user,
password=passwd,
port=port,
host=host,
protocol=protocol,
explorer=explorer,
auth=auth,
hwi_bridge_url=hwi_bridge_url,
new_otp=new_otp,
loglevel=loglevel,
specter=app.specter,
current_version=current_version,
rand=rand)
return render_template(
"settings/auth_settings.jinja",
auth=auth,
new_otp=new_otp,
specter=app.specter,
current_version=current_version,
rand=rand
)


################# wallet management #####################

Expand Down
6 changes: 5 additions & 1 deletion src/cryptoadvance/specter/hwi_server.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import json, os, random, requests
from flask import Blueprint, Flask, jsonify, render_template, request
from flask import Blueprint, Flask, jsonify, redirect, render_template, request
from flask import current_app as app
from flask_cors import CORS
from .hwi_rpc import HWIBridge
Expand All @@ -11,6 +11,10 @@
rand = random.randint(0, 1e32) # to force style refresh
hwi = HWIBridge()

@hwi_server.route("/", methods=["GET"])
def index():
return redirect('/hwi/settings')

@hwi_server.route("/api/", methods=["POST"])
def api():
"""JSON-RPC for HWI Bridge"""
Expand Down
6 changes: 5 additions & 1 deletion src/cryptoadvance/specter/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from pathlib import Path

from dotenv import load_dotenv
from flask import Flask
from flask import Flask, redirect
from flask_login import LoginManager, login_user

from .helpers import hwi_get_config
Expand Down Expand Up @@ -78,6 +78,10 @@ def login(id):
# --> View function mapping is overwriting an existing endpoint function
import importlib
importlib.reload(controller)
else:
@app.route("/", methods=["GET"])
def index():
return redirect('/hwi/settings')
return app

def create_and_init():
Expand Down
Loading

0 comments on commit ad59e8e

Please sign in to comment.