feat: add delete function
This commit is contained in:
parent
c04a83c37c
commit
7f29deba01
19
database.py
19
database.py
@ -329,19 +329,16 @@ class MusicDatabase:
|
|||||||
|
|
||||||
return self._result_to_dict(results)
|
return self._result_to_dict(results)
|
||||||
|
|
||||||
|
def query_music_by_id(self, _id):
|
||||||
|
return self.query_music(Condition().and_equal("id", _id))
|
||||||
|
|
||||||
def query_music_by_keywords(self, keywords):
|
def query_music_by_keywords(self, keywords):
|
||||||
condition = Condition()
|
condition = Condition()
|
||||||
|
|
||||||
for keyword in keywords:
|
for keyword in keywords:
|
||||||
condition.and_like("title", f"%{keyword}%", case_sensitive=False)
|
condition.and_like("title", f"%{keyword}%", case_sensitive=False)
|
||||||
|
|
||||||
conn = sqlite3.connect(self.db_path)
|
return self.query_music(condition)
|
||||||
cursor = conn.cursor()
|
|
||||||
results = cursor.execute("SELECT id, type, title, metadata, tags FROM music "
|
|
||||||
"WHERE %s" % condition.sql(), condition.filler).fetchall()
|
|
||||||
conn.close()
|
|
||||||
|
|
||||||
return self._result_to_dict(results)
|
|
||||||
|
|
||||||
def query_music_by_tags(self, tags):
|
def query_music_by_tags(self, tags):
|
||||||
condition = Condition()
|
condition = Condition()
|
||||||
@ -349,13 +346,7 @@ class MusicDatabase:
|
|||||||
for tag in tags:
|
for tag in tags:
|
||||||
condition.and_like("tags", f"%{tag},%", case_sensitive=False)
|
condition.and_like("tags", f"%{tag},%", case_sensitive=False)
|
||||||
|
|
||||||
conn = sqlite3.connect(self.db_path)
|
return self.query_music(condition)
|
||||||
cursor = conn.cursor()
|
|
||||||
results = cursor.execute("SELECT id, type, title, metadata, tags FROM music "
|
|
||||||
"WHERE %s" % condition.sql(), condition.filler).fetchall()
|
|
||||||
conn.close()
|
|
||||||
|
|
||||||
return self._result_to_dict(results)
|
|
||||||
|
|
||||||
def query_tags(self, condition):
|
def query_tags(self, condition):
|
||||||
# TODO: Can we keep a index of tags?
|
# TODO: Can we keep a index of tags?
|
||||||
|
43
interface.py
43
interface.py
@ -11,7 +11,7 @@ import shutil
|
|||||||
from werkzeug.utils import secure_filename
|
from werkzeug.utils import secure_filename
|
||||||
import errno
|
import errno
|
||||||
import media
|
import media
|
||||||
from media.item import dicts_to_items
|
from media.item import dicts_to_items, dict_to_item
|
||||||
from media.cache import get_cached_wrapper_from_scrap, get_cached_wrapper_by_id, get_cached_wrappers_by_tags, \
|
from media.cache import get_cached_wrapper_from_scrap, get_cached_wrapper_by_id, get_cached_wrappers_by_tags, \
|
||||||
get_cached_wrapper
|
get_cached_wrapper
|
||||||
from database import MusicDatabase, Condition
|
from database import MusicDatabase, Condition
|
||||||
@ -172,7 +172,7 @@ def playlist():
|
|||||||
if len(var.playlist) == 0:
|
if len(var.playlist) == 0:
|
||||||
return jsonify({'items': [render_template('playlist.html',
|
return jsonify({'items': [render_template('playlist.html',
|
||||||
m=False,
|
m=False,
|
||||||
index=-1
|
index=None
|
||||||
)]
|
)]
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -290,18 +290,16 @@ def post():
|
|||||||
var.bot.is_pause = False
|
var.bot.is_pause = False
|
||||||
time.sleep(0.1)
|
time.sleep(0.1)
|
||||||
|
|
||||||
elif 'delete_music_file' in request.form and ".." not in request.form['delete_music_file']:
|
elif 'delete_item_from_library' in request.form:
|
||||||
path = var.music_folder + request.form['delete_music_file']
|
_id = request.form['delete_item_from_library']
|
||||||
if os.path.isfile(path):
|
var.playlist.remove_by_id(_id)
|
||||||
log.info("web: delete file " + path)
|
item = var.cache.get_item_by_id(var.bot, _id)
|
||||||
os.remove(path)
|
|
||||||
|
|
||||||
elif 'delete_folder' in request.form and ".." not in request.form['delete_folder']:
|
if os.path.isfile(item.uri()):
|
||||||
path = var.music_folder + request.form['delete_folder']
|
log.info("web: delete file " + item.uri())
|
||||||
if os.path.isdir(path):
|
os.remove(item.uri())
|
||||||
log.info("web: delete folder " + path)
|
|
||||||
shutil.rmtree(path)
|
var.cache.free_and_delete(_id)
|
||||||
time.sleep(0.1)
|
|
||||||
|
|
||||||
elif 'add_tag' in request.form:
|
elif 'add_tag' in request.form:
|
||||||
music_wrappers = get_cached_wrappers_by_tags(var.bot, [request.form['add_tag']], user)
|
music_wrappers = get_cached_wrappers_by_tags(var.bot, [request.form['add_tag']], user)
|
||||||
@ -372,6 +370,7 @@ def build_library_query_condition(form):
|
|||||||
if file.startswith(folder):
|
if file.startswith(folder):
|
||||||
sub_cond.or_equal("id", var.cache.file_id_lookup[file])
|
sub_cond.or_equal("id", var.cache.file_id_lookup[file])
|
||||||
condition.and_sub_condition(sub_cond)
|
condition.and_sub_condition(sub_cond)
|
||||||
|
condition.and_equal("type", "file")
|
||||||
elif form['type'] == 'url':
|
elif form['type'] == 'url':
|
||||||
condition.and_equal("type", "url")
|
condition.and_equal("type", "url")
|
||||||
elif form['type'] == 'radio':
|
elif form['type'] == 'radio':
|
||||||
@ -406,6 +405,9 @@ def library():
|
|||||||
condition = build_library_query_condition(request.form)
|
condition = build_library_query_condition(request.form)
|
||||||
|
|
||||||
total_count = var.music_db.query_music_count(condition)
|
total_count = var.music_db.query_music_count(condition)
|
||||||
|
if not total_count:
|
||||||
|
abort(404)
|
||||||
|
|
||||||
page_count = math.ceil(total_count / ITEM_PER_PAGE)
|
page_count = math.ceil(total_count / ITEM_PER_PAGE)
|
||||||
|
|
||||||
current_page = int(request.form['page']) if 'page' in request.form else 1
|
current_page = int(request.form['page']) if 'page' in request.form else 1
|
||||||
@ -424,6 +426,21 @@ def library():
|
|||||||
|
|
||||||
log.info("cmd: add to playlist: " + music_wrapper.format_debug_string())
|
log.info("cmd: add to playlist: " + music_wrapper.format_debug_string())
|
||||||
|
|
||||||
|
return redirect("./", code=302)
|
||||||
|
elif 'action' in request.form and request.form['action'] == 'delete':
|
||||||
|
for item in items:
|
||||||
|
var.playlist.remove_by_id(item.id)
|
||||||
|
item = var.cache.get_item_by_id(var.bot, item.id)
|
||||||
|
|
||||||
|
if os.path.isfile(item.uri()):
|
||||||
|
log.info("web: delete file " + item.uri())
|
||||||
|
os.remove(item.uri())
|
||||||
|
|
||||||
|
var.cache.free_and_delete(item.id)
|
||||||
|
|
||||||
|
if len(os.listdir(var.music_folder + request.form['dir'])) == 0:
|
||||||
|
os.rmdir(var.music_folder + request.form['dir'])
|
||||||
|
|
||||||
return redirect("./", code=302)
|
return redirect("./", code=302)
|
||||||
else:
|
else:
|
||||||
results = []
|
results = []
|
||||||
|
@ -73,7 +73,7 @@ class MusicCache(dict):
|
|||||||
return items
|
return items
|
||||||
|
|
||||||
def fetch(self, bot, id):
|
def fetch(self, bot, id):
|
||||||
music_dicts = self.db.query_music(Condition().and_equal("id", id))
|
music_dicts = self.db.query_music_by_id(id)
|
||||||
if music_dicts:
|
if music_dicts:
|
||||||
music_dict = music_dicts[0]
|
music_dict = music_dicts[0]
|
||||||
self[id] = dict_to_item(bot, music_dict)
|
self[id] = dict_to_item(bot, music_dict)
|
||||||
|
@ -268,9 +268,9 @@ class OneshotPlaylist(BasePlaylist):
|
|||||||
return self
|
return self
|
||||||
|
|
||||||
def next(self):
|
def next(self):
|
||||||
self.version += 1
|
|
||||||
|
|
||||||
if len(self) > 0:
|
if len(self) > 0:
|
||||||
|
self.version += 1
|
||||||
|
|
||||||
if self.current_index != -1:
|
if self.current_index != -1:
|
||||||
super().__delitem__(self.current_index)
|
super().__delitem__(self.current_index)
|
||||||
if len(self) == 0:
|
if len(self) == 0:
|
||||||
|
@ -247,12 +247,13 @@
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="btn-group" style="margin-bottom: 5px;" role="group">
|
<div class="btn-group" style="margin-bottom: 5px;" role="group">
|
||||||
|
<button type="submit" class="btn btn-secondary btn-space" onclick="addAllResults()"><i class="fa fa-plus" aria-hidden="true"></i> Add All</button>
|
||||||
<button type="submit" class="btn btn-secondary btn-space"
|
<button type="submit" class="btn btn-secondary btn-space"
|
||||||
onclick="request('post', {action : 'rescan'}); updateResults()">
|
onclick="request('post', {action : 'rescan'}); updateResults()">
|
||||||
<i class="fas fa-sync-alt" aria-hidden="true"></i> Rescan Files
|
<i class="fas fa-sync-alt" aria-hidden="true"></i> Rescan Files
|
||||||
</button>
|
</button>
|
||||||
<button type="submit" class="btn btn-secondary btn-space" onclick="downloadAllResults()"><i class="fa fa-download" aria-hidden="true"></i> Download All</button>
|
<button type="submit" class="btn btn-secondary btn-space" onclick="downloadAllResults()"><i class="fa fa-download" aria-hidden="true"></i> Download All</button>
|
||||||
<button type="submit" class="btn btn-secondary btn-space" onclick="addAllResults()"><i class="fa fa-plus" aria-hidden="true"></i> Add All</button>
|
<button type="submit" class="btn btn-secondary btn-space" onclick="deleteAllResults()"><i class="fas fa-trash-alt" aria-hidden="true"></i> Delete All</button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@ -557,6 +558,7 @@
|
|||||||
request('post', {
|
request('post', {
|
||||||
'delete_item_from_library': $(e.currentTarget).parent().parent().find(".library-item-id").val()
|
'delete_item_from_library': $(e.currentTarget).parent().parent().find(".library-item-id").val()
|
||||||
});
|
});
|
||||||
|
updateResults();
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -686,13 +688,28 @@
|
|||||||
$.ajax({
|
$.ajax({
|
||||||
type: 'POST',
|
type: 'POST',
|
||||||
url : 'library',
|
url : 'library',
|
||||||
data: data,
|
data: data
|
||||||
statusCode: {200: processResults}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
updatePlaylist();
|
updatePlaylist();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function deleteAllResults(){
|
||||||
|
data = getFilters();
|
||||||
|
data.action = "delete";
|
||||||
|
|
||||||
|
console.log(data);
|
||||||
|
|
||||||
|
$.ajax({
|
||||||
|
type: 'POST',
|
||||||
|
url : 'library',
|
||||||
|
data: data
|
||||||
|
});
|
||||||
|
|
||||||
|
updatePlaylist();
|
||||||
|
updateResults();
|
||||||
|
}
|
||||||
|
|
||||||
function downloadAllResults(){
|
function downloadAllResults(){
|
||||||
cond = getFilters();
|
cond = getFilters();
|
||||||
download_id.val();
|
download_id.val();
|
||||||
@ -736,8 +753,10 @@
|
|||||||
var page_li_copy;
|
var page_li_copy;
|
||||||
var page_no_copy;
|
var page_no_copy;
|
||||||
|
|
||||||
if(total_pages > 27){
|
if(total_pages > 25){
|
||||||
i = (active_page - 13 >= 1) ? active_page - 13 : 1;
|
i = (active_page - 12 >= 1) ? active_page - 12 : 1;
|
||||||
|
_i = total_pages - 24;
|
||||||
|
i = (i < _i) ? i : _i;
|
||||||
page_li_copy = page_li.clone();
|
page_li_copy = page_li.clone();
|
||||||
page_no_copy = page_no.clone();
|
page_no_copy = page_no.clone();
|
||||||
page_no_copy.html("«");
|
page_no_copy.html("«");
|
||||||
@ -750,7 +769,7 @@
|
|||||||
page_li_copy.appendTo(page_ul);
|
page_li_copy.appendTo(page_ul);
|
||||||
}
|
}
|
||||||
|
|
||||||
limit = i + 26;
|
limit = i + 24;
|
||||||
for (; i <= total_pages && i <= limit; i++) {
|
for (; i <= total_pages && i <= limit; i++) {
|
||||||
page_li_copy = page_li.clone();
|
page_li_copy = page_li.clone();
|
||||||
page_no_copy = page_no.clone();
|
page_no_copy = page_no.clone();
|
||||||
@ -767,7 +786,7 @@
|
|||||||
page_li_copy.appendTo(page_ul);
|
page_li_copy.appendTo(page_ul);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(total_pages > 27){
|
if(limit < total_pages){
|
||||||
page_li_copy = page_li.clone();
|
page_li_copy = page_li.clone();
|
||||||
page_no_copy = page_no.clone();
|
page_no_copy = page_no.clone();
|
||||||
page_no_copy.html("»");
|
page_no_copy.html("»");
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
{% if index == -1 %}
|
{% if index == None %}
|
||||||
<tr id="playlist-loading">
|
<tr id="playlist-empty" class="playlist-item">
|
||||||
<td colspan="4" style="text-align:center;">
|
<td colspan="4" style="text-align:center;">
|
||||||
<img style="margin: auto; width: 35px;" src="static/image/loading.svg" />
|
<img style="margin: auto; width: 35px;" src="static/image/empty_box.svg" />
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
{% else %}
|
{% else %}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user