butter-portal/app/forms.py

55 lines
3 KiB
Python
Raw Normal View History

2026-02-17 08:42:33 +00:00
from flask_wtf import FlaskForm
from flask_wtf.file import FileAllowed, FileRequired
from wtforms import StringField, PasswordField, SubmitField, BooleanField, FileField, RadioField
from wtforms.validators import DataRequired, ValidationError, Length
2026-02-17 08:42:33 +00:00
from flask_babel import lazy_gettext as _l
import re
2026-03-10 10:27:59 +00:00
from wtforms.widgets import PasswordInput
def hostname_check(form, field):
regex= re.compile("[a-zA-Z0-9-_]+")
matches = re.fullmatch(regex, field.data)
if not matches:
raise ValidationError(_l('Only dashes, underscores, letters and numbers allowed'))
def wifi_length_check(form, field):
if len(field.data) > 63:
raise ValidationError('Wifi password cannot be longer than 63 characters.')
if len(field.data) in range(1,9):
raise ValidationError('Wifi password cannot be shorter than 8 characters.')
2026-02-17 08:42:33 +00:00
class LoginForm(FlaskForm):
username = StringField(_l('Username'), validators=[DataRequired()])
password = PasswordField(_l('Password'), validators=[DataRequired()])
submit = SubmitField(_l('Sign In'))
remember_me = BooleanField('Remember Me')
class SettingsForm(FlaskForm):
# Access point settings
ssid = StringField(_l('SSID'), validators=[DataRequired(), Length(1, 64), hostname_check])
wifi_password = StringField(_l('WiFi Password'), validators=[wifi_length_check])
2026-02-17 10:23:25 +00:00
enable_access_point = BooleanField(_l('Enable Access Point'))
2026-02-17 08:42:33 +00:00
# Customisation settings
butterbox_name = StringField(_l('Butterbox Name'), validators=[DataRequired()])
butterbox_logo = FileField((_l('Butterbox Logo')), validators=[FileAllowed(['jpg', 'png', 'svg'], 'Images only!')])
# Services settings
2026-02-17 10:23:25 +00:00
enable_file_viewer = BooleanField(_l('Enable File Viewer'))
enable_map_viewer = BooleanField(_l('Enable Map Viewer'))
enable_chat = BooleanField(_l('Enable Chat'))
enable_app_store = BooleanField(_l('Enable App Store'))
enable_deltachat = BooleanField(_l('Enable DeltaChat'))
enable_wifi_sharing = BooleanField(_l('Enable WiFi Sharing'))
2026-02-17 08:42:33 +00:00
# Access Settings
2026-03-10 10:27:59 +00:00
admin_password = StringField('Admin Password', widget=PasswordInput(hide_value=False))
root_password = StringField('Root Password', widget=PasswordInput(hide_value=False), validators=[DataRequired()])
2026-03-10 10:27:59 +00:00
root_account_settings= RadioField(_l('Secure Root Account Method'), choices=[ ('lock_root_account', 'Lock root account'), ( 'set_root_password', 'Use root password')], validators=[DataRequired()])
ssh_access_settings = RadioField(_l('SSH Access Method'), choices=[ ('disable_ssh', 'Disable SSH'), ( 'enable_ssh_with_root_password', 'Enable SSH with root password'), ], validators=[DataRequired()])
# ('enable_ssh_with_public_key', 'Enable SSH with public key'),
lock_root_account = BooleanField(_l('Lock Root Account'))
butterbox_hostname = StringField(_l('Butterbox Hostname'), validators=[DataRequired(), Length(1, 64), hostname_check])
2026-02-17 08:42:33 +00:00
submit = SubmitField(_l('Submit'))
apply_changes = SubmitField(_l('Apply Changes'))