Update first time setup flow; update strings, include strings from figma

This commit is contained in:
Ana Custura 2026-03-29 17:17:47 +01:00
parent 9937cc8884
commit 4c25aeabf9
23 changed files with 1451 additions and 245 deletions

View file

@ -1,6 +1,6 @@
from app import app
from flask import render_template, flash, redirect, url_for, send_file
from app.forms import LoginForm, SettingsForm
from app.forms import LoginForm, SettingsForm, Step1Form, Step2Form, Step3Form, Step4Form
from flask_login import login_user, current_user, logout_user, login_required
import sqlalchemy as sa
from app import db
@ -16,6 +16,7 @@ import string
import glob
import time
import qrcode
from flask_babel import lazy_gettext as _l
CHANGES_REQUIRING_RESTART = ['wifi_password', 'ssid', 'enable_access_point', 'enable_chat', 'enable_delta_chat', 'butterbox_hostname', 'ssh_access_settings', 'root_account_settings', 'root_password']
@ -60,7 +61,6 @@ def get_files_in_path(path: str):
"icon_url": get_file_icon_url(x),
"relative_path": x.replace(app.config["BUTTERBOX_USB_PATH"], "")} for x
in list_of_files]
print(file_list)
return file_list
def get_setting(name) -> str:
@ -82,8 +82,6 @@ def dump_settings(filename: str) -> None:
@app.route('/index')
def index():
enable_chat = get_setting("enable_chat")
enable_app_store = get_setting("enable_app_store")
enable_map_viewer = get_setting("enable_map_viewer")
enable_file_viewer = get_setting("enable_file_viewer")
enable_deltachat = get_setting("enable_deltachat")
enable_wifi_sharing = get_setting("enable_wifi_sharing")
@ -94,19 +92,19 @@ def index():
usb_has_maps = False # actual test of whether USB has maps folder
usb_has_appstore = False # actual test of whether USB has an appstore
if enable_wifi_sharing == 'true':
service_array.append({"name": "Share WiFi", "image": url_for("static", filename="images/share-icon.svg"), "url": url_for("share")})
service_array.append({"name": _l("Share WiFi"), "image": url_for("static", filename="images/share-icon.svg"), "url": url_for("share")})
if enable_deltachat == 'true':
service_array.append({"name": "Secure Messaging", "image": url_for("static", filename="images/deltachat-icon.svg"), "url": url_for("messaging") })
service_array.append({"name": _l("Secure Messenger"), "image": url_for("static", filename="images/deltachat-icon.svg"), "url": url_for("messaging") })
if enable_chat == 'true':
service_array.append({"name": "Message Board", "image": url_for("static", filename="images/chat-icon.png"), "url": f"{app.config["CONVENE_INSTALL_PATH"]}/#/room/join/%23public%3abutterbox.local"})
if enable_app_store == 'true' and usb_has_appstore:
service_array.append({"name": "Apps", "image": url_for("static", filename="images/appstore-icon.svg")})
if enable_map_viewer == 'true' and usb_has_maps:
service_array.append({"name": "Offline Maps", "image": url_for("static", filename="images/maps-icon.png")})
service_array.append({"name": _l("Local Chat"), "image": url_for("static", filename="images/chat-icon.png"), "url": f"{app.config["CONVENE_INSTALL_PATH"]}/#/room/join/%23public%3abutterbox.local"})
if enable_file_viewer == 'true' and usb_has_appstore:
service_array.append({"name": _l("Apps"), "image": url_for("static", filename="images/appstore-icon.svg")})
if enable_file_viewer == 'true' and usb_has_maps:
service_array.append({"name": _l("Maps"), "image": url_for("static", filename="images/maps-icon.png")})
if enable_file_viewer == 'true':
name = "Files"
name = _l("Files")
if not usb_inserted:
name = "Insert USB to browse files"
name = _l("Insert USB to browse files")
service_array.append({
"name": name,
"image": url_for("static", filename="images/explore-icon.svg"),
@ -150,26 +148,151 @@ def login():
return render_template('login.html', title='Sign in', form=form, get_setting=get_setting)
@app.route('/first_setup')
def first_setup():
if get_setting("first_setup") == "true":
return render_template('first_setup_main_page.html', get_setting=get_setting)
return redirect(url_for('admin'))
@app.route('/admin_setup')
def admin_setup():
if get_setting("first_setup") == "true":
return render_template('admin_setup.html', get_setting=get_setting)
return redirect(url_for('admin'))
@app.route('/step1', methods=['GET', 'POST'])
def step1():
form = Step1Form()
step1_settings = ['enable_chat', 'enable_deltachat', 'enable_file_viewer']
if not form.is_submitted():
for s in step1_settings:
getattr(form, s).data = (get_setting(s) == "true")
if form.validate_on_submit():
if form.submit.data:
for s in step1_settings:
setting_value = getattr(form, s).data
setting_value = str(setting_value).lower()
set_setting(s, setting_value)
db.session.commit()
return redirect(url_for('step2'))
if get_setting("first_setup") == "true":
return render_template('step1.html', form=form, get_setting=get_setting)
return redirect(url_for('admin'))
@app.route('/step2', methods=['GET', 'POST'])
def step2():
form = Step2Form()
step2_settings = ['butterbox_hostname', 'butterbox_name']
if not form.is_submitted():
for s in step2_settings:
getattr(form, s).data = get_setting(s)
if form.validate_on_submit():
if form.submit.data:
for s in step2_settings:
setting_value = getattr(form, s).data
if s == 'butterbox_hostname':
setting_value = setting_value.lower().replace(" ", "")
set_setting(s, setting_value)
new_logo = form.butterbox_logo.data
if new_logo.filename:
logo_stream = form.butterbox_logo.data.stream
b64_logo = base64.b64encode(logo_stream.read()).decode('utf-8')
file_mimetype = form.butterbox_logo.data.mimetype
new_value = f"data:{file_mimetype};base64,{b64_logo}"
existing_value = get_setting('butterbox_logo')
if new_value != existing_value:
set_setting('butterbox_logo', new_value)
db.session.commit()
return redirect(url_for('step3'))
if get_setting("first_setup") == "true":
return render_template('step2.html', form=form, get_setting=get_setting)
return redirect(url_for('admin'))
@app.route('/step3', methods=['GET', 'POST'])
def step3():
form = Step3Form()
step3_bool_settings = ['enable_wifi_sharing', 'enable_access_point']
step3_settings = ['ssid', 'wifi_password']
if not form.is_submitted():
for s in step3_bool_settings:
getattr(form, s).data = (get_setting(s) == "true")
for s in step3_settings:
getattr(form, s).data = get_setting(s)
if form.validate_on_submit():
if form.submit.data:
for s in (step3_bool_settings + step3_settings):
setting_value = getattr(form, s).data
set_setting(s, setting_value)
db.session.commit()
return redirect(url_for('step4'))
if get_setting("first_setup") == "true":
return render_template('step3.html', form=form, get_setting=get_setting)
return redirect(url_for('admin'))
@app.route('/step4', methods=['GET', 'POST'])
def step4():
form = Step4Form()
step4_settings = ['root_account_settings', 'ssh_access_settings', 'root_password', 'admin_password']
if not form.is_submitted():
for s in step4_settings:
getattr(form, s).data = get_setting(s)
if form.validate_on_submit():
if form.submit.data:
new_admin_password = form.admin_password.data
if new_admin_password:
admin_user = db.session.scalar(sa.select(User).where(User.username == 'admin'))
if not admin_user.check_password(new_admin_password):
admin_user.set_password(new_admin_password)
db.session.add(admin_user)
step4_settings.remove('admin_password')
for s in step4_settings:
setting_value = getattr(form, s).data
set_setting(s, setting_value)
set_setting('first_setup', "false")
db.session.commit()
return redirect(url_for('setup_complete'))
if get_setting("first_setup") == "true":
return render_template('step4.html', form=form, get_setting=get_setting)
return redirect(url_for('admin'))
@app.route('/setup_complete')
def setup_complete():
if get_setting("first_setup"):
dump_settings("settings.txt")
return render_template('setup_complete.html', get_setting=get_setting)
return redirect(url_for('admin'))
@app.route('/logout')
def logout():
logout_user()
return redirect(url_for('admin'))
@app.route('/admin', methods=['GET', 'POST'])
@login_required
return redirect(url_for('admin_setup'))
@app.route('/admin', methods=['GET'])
def admin():
if get_setting("first_setup") == "true":
return redirect(url_for('first_setup'))
return redirect(url_for('admin_settings'))
@app.route('/admin_settings', methods=['GET', 'POST'])
@login_required
def admin_settings():
raspap_installed = os.path.exists("/var/www/html/raspap")
raspap_installed = True
form = SettingsForm()
populate_settings = ['butterbox_name', 'wifi_password', 'ssid', 'butterbox_hostname', 'root_account_settings', 'ssh_access_settings', 'root_password', 'admin_password']
bool_settings = ['enable_access_point','enable_file_viewer', 'enable_map_viewer', 'enable_app_store', 'enable_chat', 'enable_deltachat', 'enable_wifi_sharing']
populate_settings = ['butterbox_name', 'wifi_password', 'ssid', 'root_account_settings', 'ssh_access_settings', 'root_password', 'admin_password']
bool_settings = ['enable_access_point','enable_file_viewer', 'enable_chat', 'enable_deltachat', 'enable_wifi_sharing']
populate_settings.extend(bool_settings)
if not form.is_submitted():
for s in populate_settings:
if s in bool_settings:
getattr(form, s).data = (get_setting(s) == "true")
else:
print(s, get_setting(s))
getattr(form, s).data = get_setting(s)
non_admin_settings_changed = False
if not form.validate_on_submit():
print(form.errors)
if form.validate_on_submit():
if form.submit.data:
for s in populate_settings:
@ -215,7 +338,6 @@ def admin():
set_setting('apply_changes', "true")
dump_settings("settings.txt")
flash(_("⚠️ Changes applied! If needed, the system will restart. This may take up to two minutes."))
return render_template('admin.html', raspap_installed=raspap_installed, get_setting=get_setting, form=form)