Refactored list checkbox sort

This commit is contained in:
Ozzie Isaacs 2021-04-12 18:39:09 +02:00
parent 0f95800dde
commit 90f2b3fb21
3 changed files with 28 additions and 30 deletions

View File

@ -246,15 +246,16 @@ def list_users():
limit = int(request.args.get("limit") or 10)
search = request.args.get("search")
sort = request.args.get("sort", "state")
order = request.args.get("order")
order = request.args.get("order", "").lower()
state = None
if sort != "state" and order:
order = text(sort + " " + order)
else:
order = ub.User.name.desc()
if sort == "state":
state = json.loads(request.args.get("state"))
if sort != "state" and order:
order = text(sort + " " + order)
elif not state:
order = ub.User.name.desc()
all_user = ub.session.query(ub.User)
if not config.config_anonbrowse:
all_user = all_user.filter(ub.User.role.op('&')(constants.ROLE_ANONYMOUS) != constants.ROLE_ANONYMOUS)
@ -266,16 +267,7 @@ def list_users():
func.lower(ub.User.kindle_mail).ilike("%" + search + "%"),
func.lower(ub.User.email).ilike("%" + search + "%")))
if state:
outcome = list()
userlist = {user.id:user for user in all_user.all()}
for entry in state:
outcome.append(userlist[entry])
del userlist[entry]
for entry in userlist:
outcome.append(userlist[entry])
if request.args.get("order", "").lower() == "asc":
outcome.reverse()
users = outcome[off:off + limit]
users = calibre_db.get_checkbox_sorted(all_user.all(), state, off, limit, request.args.get("order", "").lower())
else:
users = all_user.order_by(order).offset(off).limit(limit).all()
if search:

View File

@ -616,6 +616,19 @@ class CalibreDB():
return and_(lang_filter, pos_content_tags_filter, ~neg_content_tags_filter,
pos_content_cc_filter, ~neg_content_cc_filter, archived_filter)
@staticmethod
def get_checkbox_sorted(inputlist, state, offset, limit, order):
outcome = list()
elementlist = {ele.id: ele for ele in inputlist}
for entry in state:
outcome.append(elementlist[entry])
del elementlist[entry]
for entry in elementlist:
outcome.append(elementlist[entry])
if order == "asc":
outcome.reverse()
return outcome[offset:offset + limit]
# Fill indexpage with all requested data from database
def fill_indexpage(self, page, pagesize, database, db_filter, order, *join):
return self.fill_indexpage_with_archived_books(page, pagesize, database, db_filter, order, False, *join)

View File

@ -757,33 +757,26 @@ def list_books():
limit = int(request.args.get("limit") or config.config_books_per_page)
search = request.args.get("search")
sort = request.args.get("sort", "state")
order = request.args.get("order")
order = request.args.get("order", "").lower()
state = None
if sort != "state" and order:
order = [text(sort + " " + order)]
else:
order = [db.Books.timestamp.desc()]
if sort == "state":
state = json.loads(request.args.get("state"))
if sort != "state" and order:
order = [text(sort + " " + order)]
elif not state:
order = [db.Books.timestamp.desc()]
total_count = filtered_count = calibre_db.session.query(db.Books).count()
if state:
outcome = list()
if search:
books = calibre_db.search_query(search)
filtered_count = len(books)
else:
books = calibre_db.session.query(db.Books).filter(calibre_db.common_filters()).all()
booklist = {book.id: book for book in books}
for entry in state:
outcome.append(booklist[entry])
del booklist[entry]
for entry in booklist:
outcome.append(booklist[entry])
if request.args.get("order", "").lower() == "asc":
outcome.reverse()
entries = outcome[off:off + limit]
entries = calibre_db.get_checkbox_sorted(books, state, off, limit,order)
elif search:
entries, filtered_count, __ = calibre_db.get_search_results(search, off, order, limit)
else: