Skip to content

Commit

Permalink
[jarun#624] fix filters being reordered after editing
Browse files Browse the repository at this point in the history
  • Loading branch information
LeXofLeviafan committed Sep 6, 2024
1 parent 989c447 commit ae23db9
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
7 changes: 7 additions & 0 deletions bukuserver/static/bukuserver/js/filters_fix.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
$(document).ready(function () {
const IDX = [[36, 'a'], [10, 'A'], [0, '0']];
let idxChar = (i, [x, c]=IDX.find(([x]) => i >= x)) => String.fromCharCode(c.charCodeAt(0) + (i-x));
filter_form.onsubmit = function () {
$(`.filter-val[name]`, this).each((i, e) => {e.name = e.name.replace(/(?<=^flt)[^_]+(?=_)/, idxChar(i))});
};
});
6 changes: 4 additions & 2 deletions bukuserver/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ def get_detail_value(self, context, model, name):
edit_template = "bukuserver/bookmark_edit.html"
named_filter_urls = True
extra_css = ['/static/bukuserver/css/' + it for it in ('bookmark.css', 'modal.css')]
extra_js = ['/static/bukuserver/js/' + it for it in ('page_size.js', 'last_page.js')]
extra_js = ['/static/bukuserver/js/' + it for it in ('page_size.js', 'last_page.js', 'filters_fix.js')]
last_page = expose('/last-page')(last_page)

def __init__(self, bukudb: buku.BukuDb, *args, **kwargs):
Expand Down Expand Up @@ -432,7 +432,7 @@ def _name_formatter(self, _, model, name):
}
list_template = 'bukuserver/tags_list.html'
edit_template = "bukuserver/tag_edit.html"
extra_js = ['/static/bukuserver/js/' + it for it in ('page_size.js', 'last_page.js')]
extra_js = ['/static/bukuserver/js/' + it for it in ('page_size.js', 'last_page.js', 'filters_fix.js')]
last_page = expose('/last-page')(last_page)

def __init__(self, bukudb, *args, **kwargs):
Expand Down Expand Up @@ -620,6 +620,8 @@ def page_of(items, size, idx):
return []

def filter_key(flt, idx=''):
if isinstance(idx, int) and idx > 9:
idx = (chr(ord('A') + idx-10) if idx < 36 else chr(ord('a') + idx-36))
return 'flt' + str(idx) + '_' + BookmarkModelView._filter_arg(flt)

def format_value(field, bookmark, spacing=''):
Expand Down
9 changes: 8 additions & 1 deletion tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
resources: https://flask.palletsprojects.com/en/2.2.x/testing/
"""
from argparse import Namespace
from unittest import mock

import pytest
from flask import request
Expand All @@ -11,7 +12,7 @@

from buku import BukuDb
from bukuserver import server
from bukuserver.views import BookmarkModelView, TagModelView
from bukuserver.views import BookmarkModelView, TagModelView, filter_key
from tests.util import mock_fetch, _add_rec


Expand Down Expand Up @@ -61,6 +62,12 @@ def bmv_instance(bukudb):
return BookmarkModelView(bukudb)


@pytest.mark.parametrize('idx, char', [('', ''), (0, '0'), (9, '9'), (10, 'A'), (35, 'Z'), (36, 'a'), (61, 'z')])
def test_filter_key(idx, char):
with mock.patch('bukuserver.views.BookmarkModelView._filter_arg', return_value='filter_name'):
assert filter_key(None, idx) == f'flt{char}_filter_name'


@pytest.mark.parametrize('disable_favicon', [False, True])
def test_bookmark_model_view(bukudb, disable_favicon, app):
inst = BookmarkModelView(bukudb)
Expand Down

0 comments on commit ae23db9

Please sign in to comment.