From ef44b06bfbfa4158ebcad17ebd92640e1f5eaa41 Mon Sep 17 00:00:00 2001 From: boyska Date: Tue, 3 Jan 2017 20:31:38 +0100 Subject: [PATCH] WebUI completion for dirs --- larigira/audioform_mostrecent.py | 7 ++-- larigira/audioform_randomdir.py | 7 ++-- larigira/dbadmin/__init__.py | 33 ++++++++++++++++--- .../dbadmin/templates/add_audio_kind.html | 12 ++++--- 4 files changed, 46 insertions(+), 13 deletions(-) diff --git a/larigira/audioform_mostrecent.py b/larigira/audioform_mostrecent.py index 6874ab4..fe1717e 100644 --- a/larigira/audioform_mostrecent.py +++ b/larigira/audioform_mostrecent.py @@ -2,12 +2,15 @@ from pytimeparse.timeparse import timeparse from flask_wtf import Form from wtforms import StringField, validators, SubmitField, ValidationError +from larigira.formutils import AutocompleteStringField + class AudioForm(Form): nick = StringField('Audio nick', validators=[validators.required()], description='A simple name to recognize this audio') - path = StringField('Path', validators=[validators.required()], - description='Directory to pick file from') + path = AutocompleteStringField('dl-suggested-dirs', + 'Path', validators=[validators.required()], + description='Directory to pick file from') maxage = StringField('Max age', validators=[validators.required()], description='in seconds, or human-readable ' diff --git a/larigira/audioform_randomdir.py b/larigira/audioform_randomdir.py index c4a4c03..40a4c37 100644 --- a/larigira/audioform_randomdir.py +++ b/larigira/audioform_randomdir.py @@ -1,12 +1,15 @@ import flask_wtf from wtforms import StringField, validators, SubmitField, IntegerField +from larigira.formutils import AutocompleteStringField + class Form(flask_wtf.Form): nick = StringField('Audio nick', validators=[validators.required()], description='A simple name to recognize this audio') - path = StringField('Path', validators=[validators.required()], - description='Full path to source directory') + path = AutocompleteStringField('dl-suggested-dirs', + 'Path', validators=[validators.required()], + description='Full path to source directory') howmany = IntegerField('Number', validators=[validators.optional()], default=1, description='How many songs to be picked' diff --git a/larigira/dbadmin/__init__.py b/larigira/dbadmin/__init__.py index dec46bb..46de389 100644 --- a/larigira/dbadmin/__init__.py +++ b/larigira/dbadmin/__init__.py @@ -43,6 +43,29 @@ def get_suggested_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('/') def home(): return render_template('dbadmin_base.html') @@ -81,7 +104,8 @@ def edit_time(alarmid): data = receiver(form) model.update_alarm(alarmid, data) 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', form=form, kind=kind, @@ -124,7 +148,8 @@ def addaudio_kind(kind): return jsonify(dict(inserted=eid, data=data)) return render_template('add_audio_kind.html', form=form, kind=kind, - suggested_files=get_suggested_files()) + suggestions=get_suggestions() + ) @db.route('/edit/audio/', methods=['GET', 'POST']) @@ -140,12 +165,12 @@ def edit_audio(actionid): data = receiver(form) model.update_action(actionid, data) model.reload() - return redirect(url_for('db.list')) + return redirect(url_for('db.events_list')) return render_template('add_audio_kind.html', form=form, kind=kind, mode='edit', - suggested_files=get_suggested_files() + suggestions=get_suggestions() ) diff --git a/larigira/dbadmin/templates/add_audio_kind.html b/larigira/dbadmin/templates/add_audio_kind.html index c1f1722..2e7bcd4 100644 --- a/larigira/dbadmin/templates/add_audio_kind.html +++ b/larigira/dbadmin/templates/add_audio_kind.html @@ -10,11 +10,13 @@ resubmit {% endif %} - - {% for fname in suggested_files %} - +{% for category in suggestions %} + + {% for fname in suggestions[category] %} + +{% endfor %}
{{wtf.quick_form(form)}}