diff --git a/cps/db.py b/cps/db.py index f5696d07..64abbe1b 100644 --- a/cps/db.py +++ b/cps/db.py @@ -685,9 +685,12 @@ class CalibreDB(): pos_content_cc_filter, ~neg_content_cc_filter, archived_filter) @staticmethod - def get_checkbox_sorted(inputlist, state, offset, limit, order): + def get_checkbox_sorted(inputlist, state, offset, limit, order, combo=False): outcome = list() - elementlist = {ele.id: ele for ele in inputlist} + if combo: + elementlist = {ele[0].id: ele for ele in inputlist} + else: + elementlist = {ele.id: ele for ele in inputlist} for entry in state: try: outcome.append(elementlist[entry]) @@ -702,10 +705,10 @@ class CalibreDB(): # 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) + return self.fill_indexpage_with_archived_books(page, pagesize, db_filter, order, False, database, join) - def fill_indexpage_with_archived_books(self, page, pagesize, database, db_filter, order, allow_show_archived, - *join): + def fill_indexpage_with_archived_books(self, page, pagesize, db_filter, order, allow_show_archived, + *args): pagesize = pagesize or self.config.config_books_per_page if current_user.show_detail_random(): randm = self.session.query(Books) \ @@ -714,20 +717,32 @@ class CalibreDB(): .limit(self.config.config_random_books).all() else: randm = false() + if len(args) > 1: + if isinstance(args[0], DeclarativeMeta): + query = self.session.query(args[0]) + else: + query = self.session.query(*args[0]) + join = args[1] + else: + join = tuple() + query = self.session.query(args) off = int(int(pagesize) * (page - 1)) - query = self.session.query(database) - if len(join) == 6: - query = query.outerjoin(join[0], join[1]).outerjoin(join[2]).outerjoin(join[3], join[4]).outerjoin(join[5]) - if len(join) == 5: - query = query.outerjoin(join[0], join[1]).outerjoin(join[2]).outerjoin(join[3], join[4]) - if len(join) == 4: - query = query.outerjoin(join[0], join[1]).outerjoin(join[2]).outerjoin(join[3]) - if len(join) == 3: - query = query.outerjoin(join[0], join[1]).outerjoin(join[2]) - elif len(join) == 2: - query = query.outerjoin(join[0], join[1]) - elif len(join) == 1: - query = query.outerjoin(join[0]) + + indx = len(join) + element = 0 + while indx: + if indx >= 3: + query = query.outerjoin(join[element], join[element+1]).outerjoin(join[element+2]) + indx -= 3 + element += 3 + elif indx == 2: + query = query.outerjoin(join[element], join[element+1]) + indx -= 2 + element += 2 + elif indx == 1: + query = query.outerjoin(join[element]) + indx -= 1 + element += 1 query = query.filter(db_filter)\ .filter(self.common_filters(allow_show_archived)) entries = list() @@ -785,7 +800,9 @@ class CalibreDB(): authorterms = re.split("[, ]+", term) for authorterm in authorterms: q.append(Books.authors.any(func.lower(Authors.name).ilike("%" + authorterm + "%"))) - query = self.session.query(Books) + query = (self.session.query(Books, ub.ArchivedBook.is_archived) + .outerjoin(ub.ArchivedBook, and_(Books.id == ub.ArchivedBook.book_id, + int(current_user.id) == ub.ArchivedBook.user_id))) if len(join) == 6: query = query.outerjoin(join[0], join[1]).outerjoin(join[2]).outerjoin(join[3], join[4]).outerjoin(join[5]) if len(join) == 3: @@ -816,7 +833,7 @@ class CalibreDB(): offset = 0 limit_all = result_count - ub.store_ids(result) + ub.store_combo_ids(result) return result[offset:limit_all], result_count, pagination # Creates for all stored languages a translated speaking name in the array for the UI @@ -831,15 +848,6 @@ class CalibreDB(): .group_by(text('books_languages_link.lang_code')).all() for lang in languages: lang.name = isoLanguages.get_language_name(get_locale(), lang.lang_code) - #try: - # if lang.lang_code.lower() == "und": - # lang.name = isoLanguages.get_language_name(get_locale(), lang.lang_code) - # # lang.name = _("Undetermined") - # else: - # cur_l = LC.parse(lang.lang_code) - # lang.name = cur_l.get_language_name(get_locale()) - #except UnknownLocaleError: - # lang.name = _(isoLanguages.get(part3=lang.lang_code).name) return languages def update_title_sort(self, config, conn=None): diff --git a/cps/ub.py b/cps/ub.py index 69ea55f6..e9ad264f 100644 --- a/cps/ub.py +++ b/cps/ub.py @@ -126,6 +126,12 @@ def store_ids(result): ids.append(element.id) searched_ids[current_user.id] = ids +def store_combo_ids(result): + ids = list() + for element in result: + ids.append(element[0].id) + searched_ids[current_user.id] = ids + class UserBase: diff --git a/cps/web.py b/cps/web.py index c6a5bfa1..0fcb43f4 100644 --- a/cps/web.py +++ b/cps/web.py @@ -677,10 +677,10 @@ def render_archived_books(page, order): archived_filter = db.Books.id.in_(archived_book_ids) entries, random, pagination = calibre_db.fill_indexpage_with_archived_books(page, 0, - db.Books, archived_filter, order, - allow_show_archived=True) + True, + db.Books) name = _(u'Archived Books') + ' (' + str(len(archived_book_ids)) + ')' pagename = "archived" @@ -770,20 +770,19 @@ def list_books(): sort = request.args.get("sort", "id") order = request.args.get("order", "").lower() state = None - join = tuple() + join = list() if sort == "state": state = json.loads(request.args.get("state", "[]")) - # order = [db.Books.timestamp.asc()] if order == "asc" else [db.Books.timestamp.desc()] # ToDo wrong: sort ticked elif sort == "tags": order = [db.Tags.name.asc()] if order == "asc" else [db.Tags.name.desc()] - join = db.books_tags_link,db.Books.id == db.books_tags_link.c.book, db.Tags + join = db.books_tags_link, db.Books.id == db.books_tags_link.c.book, db.Tags elif sort == "series": order = [db.Series.name.asc()] if order == "asc" else [db.Series.name.desc()] - join = db.books_series_link,db.Books.id == db.books_series_link.c.book, db.Series + join = db.books_series_link, db.Books.id == db.books_series_link.c.book, db.Series elif sort == "publishers": order = [db.Publishers.name.asc()] if order == "asc" else [db.Publishers.name.desc()] - join = db.books_publishers_link,db.Books.id == db.books_publishers_link.c.book, db.Publishers + join = db.books_publishers_link, db.Books.id == db.books_publishers_link.c.book, db.Publishers elif sort == "authors": order = [db.Authors.name.asc(), db.Series.name, db.Books.series_index] if order == "asc" \ else [db.Authors.name.desc(), db.Series.name.desc(), db.Books.series_index.desc()] @@ -798,24 +797,38 @@ def list_books(): order = [db.Books.timestamp.desc()] total_count = filtered_count = calibre_db.session.query(db.Books).filter(calibre_db.common_filters(False)).count() - if state is not None: if search: books = calibre_db.search_query(search).all() filtered_count = len(books) else: - books = calibre_db.session.query(db.Books).filter(calibre_db.common_filters()).all() - entries = calibre_db.get_checkbox_sorted(books, state, off, limit, order) + books = (calibre_db.session.query(db.Books,ub.ArchivedBook.is_archived) + .outerjoin(ub.ArchivedBook, and_(db.Books.id == ub.ArchivedBook.book_id, + int(current_user.id) == ub.ArchivedBook.user_id)) + .filter(calibre_db.common_filters()).all()) + entries = calibre_db.get_checkbox_sorted(books, state, off, limit, order, True) elif search: - entries, filtered_count, __ = calibre_db.get_search_results(search, off, order, limit, *join) + entries, filtered_count, __ = calibre_db.get_search_results(search, off, order, limit, *tuple(join)) else: - entries, __, __ = calibre_db.fill_indexpage((int(off) / (int(limit)) + 1), limit, db.Books, True, order, *join) + join.append(ub.ArchivedBook) + join.append(and_(db.Books.id == ub.ArchivedBook.book_id,int(current_user.id) == ub.ArchivedBook.user_id)) + entries, __, __ = calibre_db.fill_indexpage((int(off) / (int(limit)) + 1), + limit, + (db.Books, ub.ArchivedBook), + True, + order, + *tuple(join)) + result = list() for entry in entries: - for index in range(0, len(entry.languages)): - entry.languages[index].language_name = isoLanguages.get_language_name(get_locale(), entry.languages[ + val = entry[0] + val.is_archived = entry[1] == True + for index in range(0, len(val.languages)): + val.languages[index].language_name = isoLanguages.get_language_name(get_locale(), val.languages[ index].lang_code) - table_entries = {'totalNotFiltered': total_count, 'total': filtered_count, "rows": entries} + result.append(val) + + table_entries = {'totalNotFiltered': total_count, 'total': filtered_count, "rows": result} js_list = json.dumps(table_entries, cls=db.AlchemyEncoder) response = make_response(js_list)