WebUI completion for dirs

This commit is contained in:
boyska 2017-01-03 20:31:38 +01:00
parent e6bee6865f
commit ef44b06bfb
No known key found for this signature in database
GPG key ID: 7395DCAE58289CA9
4 changed files with 46 additions and 13 deletions

View file

@ -2,12 +2,15 @@ from pytimeparse.timeparse import timeparse
from flask_wtf import Form from flask_wtf import Form
from wtforms import StringField, validators, SubmitField, ValidationError from wtforms import StringField, validators, SubmitField, ValidationError
from larigira.formutils import AutocompleteStringField
class AudioForm(Form): class AudioForm(Form):
nick = StringField('Audio nick', validators=[validators.required()], nick = StringField('Audio nick', validators=[validators.required()],
description='A simple name to recognize this audio') description='A simple name to recognize this audio')
path = StringField('Path', validators=[validators.required()], path = AutocompleteStringField('dl-suggested-dirs',
description='Directory to pick file from') 'Path', validators=[validators.required()],
description='Directory to pick file from')
maxage = StringField('Max age', maxage = StringField('Max age',
validators=[validators.required()], validators=[validators.required()],
description='in seconds, or human-readable ' description='in seconds, or human-readable '

View file

@ -1,12 +1,15 @@
import flask_wtf import flask_wtf
from wtforms import StringField, validators, SubmitField, IntegerField from wtforms import StringField, validators, SubmitField, IntegerField
from larigira.formutils import AutocompleteStringField
class Form(flask_wtf.Form): class Form(flask_wtf.Form):
nick = StringField('Audio nick', validators=[validators.required()], nick = StringField('Audio nick', validators=[validators.required()],
description='A simple name to recognize this audio') description='A simple name to recognize this audio')
path = StringField('Path', validators=[validators.required()], path = AutocompleteStringField('dl-suggested-dirs',
description='Full path to source directory') 'Path', validators=[validators.required()],
description='Full path to source directory')
howmany = IntegerField('Number', validators=[validators.optional()], howmany = IntegerField('Number', validators=[validators.optional()],
default=1, default=1,
description='How many songs to be picked' description='How many songs to be picked'

View file

@ -43,6 +43,29 @@ def get_suggested_files():
return files return files
def get_suggested_dirs():
dirset = set()
for f in get_suggested_files():
dirpath = os.path.dirname(f)
while dirpath:
if dirpath in dirset:
break
dirset.add(dirpath)
dirpath = os.path.dirname(dirpath)
return list(dirset)
def get_suggestions():
files = get_suggested_files()
if len(files) > 200:
current_app.logger.warn("Too many suggested files, cropping")
files = files[:200]
return dict(
files=files,
dirs=get_suggested_dirs())
@db.route('/') @db.route('/')
def home(): def home():
return render_template('dbadmin_base.html') return render_template('dbadmin_base.html')
@ -81,7 +104,8 @@ def edit_time(alarmid):
data = receiver(form) data = receiver(form)
model.update_alarm(alarmid, data) model.update_alarm(alarmid, data)
model.reload() model.reload()
return redirect(url_for('db.list', _anchor='event-%d' % alarmid)) return redirect(url_for('db.events_list',
_anchor='event-%d' % alarmid))
return render_template('add_time_kind.html', return render_template('add_time_kind.html',
form=form, form=form,
kind=kind, kind=kind,
@ -124,7 +148,8 @@ def addaudio_kind(kind):
return jsonify(dict(inserted=eid, data=data)) return jsonify(dict(inserted=eid, data=data))
return render_template('add_audio_kind.html', form=form, kind=kind, return render_template('add_audio_kind.html', form=form, kind=kind,
suggested_files=get_suggested_files()) suggestions=get_suggestions()
)
@db.route('/edit/audio/<int:actionid>', methods=['GET', 'POST']) @db.route('/edit/audio/<int:actionid>', methods=['GET', 'POST'])
@ -140,12 +165,12 @@ def edit_audio(actionid):
data = receiver(form) data = receiver(form)
model.update_action(actionid, data) model.update_action(actionid, data)
model.reload() model.reload()
return redirect(url_for('db.list')) return redirect(url_for('db.events_list'))
return render_template('add_audio_kind.html', return render_template('add_audio_kind.html',
form=form, form=form,
kind=kind, kind=kind,
mode='edit', mode='edit',
suggested_files=get_suggested_files() suggestions=get_suggestions()
) )

View file

@ -10,11 +10,13 @@
resubmit resubmit
</div> </div>
{% endif %} {% endif %}
<datalist id="dl-suggested-files"> {% for category in suggestions %}
{% for fname in suggested_files %} <datalist id="dl-suggested-{{category}}">
<option value="{{fname}}"> {% for fname in suggestions[category] %}
{% endfor %} <option value="{{fname}}">
</datalist> {% endfor %}
</datalist>
{% endfor %}
<div class="container-fluid"> <div class="container-fluid">
{{wtf.quick_form(form)}} {{wtf.quick_form(form)}}
</div> </div>