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)
|
||||
|
||||
def query_music_by_id(self, _id):
|
||||
return self.query_music(Condition().and_equal("id", _id))
|
||||
|
||||
def query_music_by_keywords(self, keywords):
|
||||
condition = Condition()
|
||||
|
||||
for keyword in keywords:
|
||||
condition.and_like("title", f"%{keyword}%", case_sensitive=False)
|
||||
|
||||
conn = sqlite3.connect(self.db_path)
|
||||
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)
|
||||
return self.query_music(condition)
|
||||
|
||||
def query_music_by_tags(self, tags):
|
||||
condition = Condition()
|
||||
@ -349,13 +346,7 @@ class MusicDatabase:
|
||||
for tag in tags:
|
||||
condition.and_like("tags", f"%{tag},%", case_sensitive=False)
|
||||
|
||||
conn = sqlite3.connect(self.db_path)
|
||||
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)
|
||||
return self.query_music(condition)
|
||||
|
||||
def query_tags(self, condition):
|
||||
# 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
|
||||
import errno
|
||||
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, \
|
||||
get_cached_wrapper
|
||||
from database import MusicDatabase, Condition
|
||||
@ -172,7 +172,7 @@ def playlist():
|
||||
if len(var.playlist) == 0:
|
||||
return jsonify({'items': [render_template('playlist.html',
|
||||
m=False,
|
||||
index=-1
|
||||
index=None
|
||||
)]
|
||||
})
|
||||
|
||||
@ -290,18 +290,16 @@ def post():
|
||||
var.bot.is_pause = False
|
||||
time.sleep(0.1)
|
||||
|
||||
elif 'delete_music_file' in request.form and ".." not in request.form['delete_music_file']:
|
||||
path = var.music_folder + request.form['delete_music_file']
|
||||
if os.path.isfile(path):
|
||||
log.info("web: delete file " + path)
|
||||
os.remove(path)
|
||||
elif 'delete_item_from_library' in request.form:
|
||||
_id = request.form['delete_item_from_library']
|
||||
var.playlist.remove_by_id(_id)
|
||||
item = var.cache.get_item_by_id(var.bot, _id)
|
||||
|
||||
elif 'delete_folder' in request.form and ".." not in request.form['delete_folder']:
|
||||
path = var.music_folder + request.form['delete_folder']
|
||||
if os.path.isdir(path):
|
||||
log.info("web: delete folder " + path)
|
||||
shutil.rmtree(path)
|
||||
time.sleep(0.1)
|
||||
if os.path.isfile(item.uri()):
|
||||
log.info("web: delete file " + item.uri())
|
||||
os.remove(item.uri())
|
||||
|
||||
var.cache.free_and_delete(_id)
|
||||
|
||||
elif 'add_tag' in request.form:
|
||||
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):
|
||||
sub_cond.or_equal("id", var.cache.file_id_lookup[file])
|
||||
condition.and_sub_condition(sub_cond)
|
||||
condition.and_equal("type", "file")
|
||||
elif form['type'] == 'url':
|
||||
condition.and_equal("type", "url")
|
||||
elif form['type'] == 'radio':
|
||||
@ -406,6 +405,9 @@ def library():
|
||||
condition = build_library_query_condition(request.form)
|
||||
|
||||
total_count = var.music_db.query_music_count(condition)
|
||||
if not total_count:
|
||||
abort(404)
|
||||
|
||||
page_count = math.ceil(total_count / ITEM_PER_PAGE)
|
||||
|
||||
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())
|
||||
|
||||
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)
|
||||
else:
|
||||
results = []
|
||||
|
@ -73,7 +73,7 @@ class MusicCache(dict):
|
||||
return items
|
||||
|
||||
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:
|
||||
music_dict = music_dicts[0]
|
||||
self[id] = dict_to_item(bot, music_dict)
|
||||
|
@ -268,9 +268,9 @@ class OneshotPlaylist(BasePlaylist):
|
||||
return self
|
||||
|
||||
def next(self):
|
||||
self.version += 1
|
||||
|
||||
if len(self) > 0:
|
||||
self.version += 1
|
||||
|
||||
if self.current_index != -1:
|
||||
super().__delitem__(self.current_index)
|
||||
if len(self) == 0:
|
||||
|
@ -247,12 +247,13 @@
|
||||
</div>
|
||||
|
||||
<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"
|
||||
onclick="request('post', {action : 'rescan'}); updateResults()">
|
||||
<i class="fas fa-sync-alt" aria-hidden="true"></i> Rescan Files
|
||||
</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>
|
||||
@ -557,6 +558,7 @@
|
||||
request('post', {
|
||||
'delete_item_from_library': $(e.currentTarget).parent().parent().find(".library-item-id").val()
|
||||
});
|
||||
updateResults();
|
||||
}
|
||||
);
|
||||
|
||||
@ -686,13 +688,28 @@
|
||||
$.ajax({
|
||||
type: 'POST',
|
||||
url : 'library',
|
||||
data: data,
|
||||
statusCode: {200: processResults}
|
||||
data: data
|
||||
});
|
||||
|
||||
updatePlaylist();
|
||||
}
|
||||
|
||||
function deleteAllResults(){
|
||||
data = getFilters();
|
||||
data.action = "delete";
|
||||
|
||||
console.log(data);
|
||||
|
||||
$.ajax({
|
||||
type: 'POST',
|
||||
url : 'library',
|
||||
data: data
|
||||
});
|
||||
|
||||
updatePlaylist();
|
||||
updateResults();
|
||||
}
|
||||
|
||||
function downloadAllResults(){
|
||||
cond = getFilters();
|
||||
download_id.val();
|
||||
@ -736,8 +753,10 @@
|
||||
var page_li_copy;
|
||||
var page_no_copy;
|
||||
|
||||
if(total_pages > 27){
|
||||
i = (active_page - 13 >= 1) ? active_page - 13 : 1;
|
||||
if(total_pages > 25){
|
||||
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_no_copy = page_no.clone();
|
||||
page_no_copy.html("«");
|
||||
@ -750,7 +769,7 @@
|
||||
page_li_copy.appendTo(page_ul);
|
||||
}
|
||||
|
||||
limit = i + 26;
|
||||
limit = i + 24;
|
||||
for (; i <= total_pages && i <= limit; i++) {
|
||||
page_li_copy = page_li.clone();
|
||||
page_no_copy = page_no.clone();
|
||||
@ -767,7 +786,7 @@
|
||||
page_li_copy.appendTo(page_ul);
|
||||
}
|
||||
|
||||
if(total_pages > 27){
|
||||
if(limit < total_pages){
|
||||
page_li_copy = page_li.clone();
|
||||
page_no_copy = page_no.clone();
|
||||
page_no_copy.html("»");
|
||||
|
@ -1,7 +1,7 @@
|
||||
{% if index == -1 %}
|
||||
<tr id="playlist-loading">
|
||||
{% if index == None %}
|
||||
<tr id="playlist-empty" class="playlist-item">
|
||||
<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>
|
||||
</tr>
|
||||
{% else %}
|
||||
|
Loading…
x
Reference in New Issue
Block a user