From 70f358f964d6a2a83f6c46cbf99ab559693ce24f Mon Sep 17 00:00:00 2001 From: Terry Geng Date: Fri, 20 Mar 2020 13:24:31 +0800 Subject: [PATCH] feat: add tags to playlist items in web interface --- interface.py | 19 +++++++++++++++---- templates/index.html | 8 ++++---- templates/playlist.html | 7 +++++-- 3 files changed, 24 insertions(+), 10 deletions(-) diff --git a/interface.py b/interface.py index 4d1c2da..4b152c6 100644 --- a/interface.py +++ b/interface.py @@ -181,9 +181,13 @@ def playlist(): items = [] for index, item_wrapper in enumerate(var.playlist): + tag_tuples = [] + for tag in item_wrapper.item().tags: + tag_tuples.append([tag, tags_color_lookup[tag]]) + items.append(render_template('playlist.html', index=index, - tags_color_lookup=tags_color_lookup, + tag_tuples=tag_tuples, m=item_wrapper.item(), playlist=var.playlist ) @@ -491,9 +495,16 @@ def library(): 'active_page': current_page }) elif request.form['action'] == 'edit_tags': - item = var.music_db.query_music_by_id(request.form['id']) - item['tags'] = list(dict.fromkeys(request.form['tags'].split(","))) # remove duplicated items - var.music_db.insert_music(item) + tags = list(dict.fromkeys(request.form['tags'].split(","))) # remove duplicated items + if request.form['id'] in var.cache: + music_wrapper = get_cached_wrapper_by_id(var.bot, request.form['id'], user) + music_wrapper.clear_tags() + music_wrapper.add_tags(tags) + var.playlist.version += 1 + else: + item = var.music_db.query_music_by_id(request.form['id']) + item['tags'] = tags + var.music_db.insert_music(item) return redirect("./", code=302) else: diff --git a/templates/index.html b/templates/index.html index 0c5976d..5003da8 100644 --- a/templates/index.html +++ b/templates/index.html @@ -598,7 +598,7 @@ tag.addClass('tag-clicked'); tag.removeClass('tag-unclicked'); } else { - tag.addClass('tag-umclicked'); + tag.addClass('tag-unclicked'); tag.removeClass('tag-clicked'); } updateResults(); @@ -684,8 +684,7 @@ var tag_edit_copy = tag_edit_element.clone(); tag_edit_copy.click(function(){ - addTagModalPrepare(item.id, item.title, item.tags); - add_tag_modal.modal('show'); + addTagModalShow(item.id, item.title, item.tags); }); tag_edit_copy.appendTo(tags); @@ -895,7 +894,7 @@ var modal_tag = $(".modal-tag"); var modal_tag_text = $(".modal-tag-text"); - function addTagModalPrepare(_id, _title, _tag_tuples){ + function addTagModalShow(_id, _title, _tag_tuples){ add_tag_modal_title.html("Edit tags for " + _title); add_tag_modal_item_id.val(_id); add_tag_modal_tags.empty(); @@ -910,6 +909,7 @@ tag_copy.appendTo(add_tag_modal_tags); modal_tag_text.html(""); }); + add_tag_modal.modal('show'); } function addTagModalAdd(){ diff --git a/templates/playlist.html b/templates/playlist.html index 68919e6..07f6994 100644 --- a/templates/playlist.html +++ b/templates/playlist.html @@ -39,8 +39,11 @@ Unknown Artist {% endif %}
- {% for tag in m.tags %} - {{ tag }} + + + {% for tag_tuple in tag_tuples %} + {{ tag_tuple[0] }} {% endfor %}