From 765b81738410e5cad52f6478fac91bce9a8962a1 Mon Sep 17 00:00:00 2001 From: Ozzieisaacs Date: Sat, 16 Mar 2019 16:53:22 +0100 Subject: [PATCH] Refactored shelf.py and editbooks.py --- cps/editbooks.py | 28 ++++++------------------- cps/shelf.py | 45 +++++++++------------------------------- cps/templates/shelf.html | 2 +- 3 files changed, 17 insertions(+), 58 deletions(-) diff --git a/cps/editbooks.py b/cps/editbooks.py index a39e32a0..667cefef 100644 --- a/cps/editbooks.py +++ b/cps/editbooks.py @@ -45,8 +45,6 @@ editbook = Blueprint('editbook', __name__) EXTENSIONS_CONVERT = {'pdf', 'epub', 'mobi', 'azw3', 'docx', 'rtf', 'fb2', 'lit', 'lrf', 'txt', 'htmlz', 'rtf', 'odt'} - - # Modifies different Database objects, first check if elements have to be added to database, than check # if elements have to be deleted, because they are no longer used def modify_database_object(input_elements, db_book_object, db_object, db_session, db_type): @@ -260,26 +258,12 @@ def edit_cc_data(book_id, book, to_save): else: cc_db_value = None if to_save[cc_string].strip(): - if c.datatype == 'bool': + if c.datatype == 'int' or c.datatype == 'bool': if to_save[cc_string] == 'None': to_save[cc_string] = None - else: + elif c.datatype == 'bool': to_save[cc_string] = 1 if to_save[cc_string] == 'True' else 0 - if to_save[cc_string] != cc_db_value: - if cc_db_value is not None: - if to_save[cc_string] is not None: - setattr(getattr(book, cc_string)[0], 'value', to_save[cc_string]) - else: - del_cc = getattr(book, cc_string)[0] - getattr(book, cc_string).remove(del_cc) - db.session.delete(del_cc) - else: - cc_class = db.cc_classes[c.id] - new_cc = cc_class(value=to_save[cc_string], book=book_id) - db.session.add(new_cc) - elif c.datatype == 'int': - if to_save[cc_string] == 'None': - to_save[cc_string] = None + if to_save[cc_string] != cc_db_value: if cc_db_value is not None: if to_save[cc_string] is not None: @@ -401,12 +385,12 @@ def upload_cover(request, book): requested_file.save(saved_filename) # im=Image.open(saved_filename) book.has_cover = 1 - except OSError: - flash(_(u"Failed to store cover-file %(cover)s.", cover=saved_filename), category="error") - return redirect(url_for('web.show_book', book_id=book.id)) except IOError: flash(_(u"Cover-file is not a valid image file" % saved_filename), category="error") return redirect(url_for('web.show_book', book_id=book.id)) + except OSError: + flash(_(u"Failed to store cover-file %(cover)s.", cover=saved_filename), category="error") + return redirect(url_for('web.show_book', book_id=book.id)) @editbook.route("/admin/book/", methods=['GET', 'POST']) @login_required_if_no_ano diff --git a/cps/shelf.py b/cps/shelf.py index 109eff75..f7f27755 100644 --- a/cps/shelf.py +++ b/cps/shelf.py @@ -109,9 +109,9 @@ def search_to_shelf(shelf_id): book_ids = list() for book_id in books_in_shelf: book_ids.append(book_id.book_id) - for id in searched_ids[current_user.id]: - if id not in book_ids: - books_for_shelf.append(id) + for searchid in searched_ids[current_user.id]: + if searchid not in book_ids: + books_for_shelf.append(searchid) else: books_for_shelf = searched_ids[current_user.id] @@ -259,10 +259,11 @@ def delete_shelf(shelf_id): app.logger.info(_(u"successfully deleted shelf %(name)s", name=cur_shelf.name, category="success")) return redirect(url_for('web.index')) - -@shelf.route("/shelf/") +# @shelf.route("/shelfdown/") +@shelf.route("/shelf/", defaults={'type': 1}) +@shelf.route("/shelf//") @login_required -def show_shelf(shelf_id): +def show_shelf(type, shelf_id): if current_user.is_anonymous: shelf = ub.session.query(ub.Shelf).filter(ub.Shelf.is_public == 1, ub.Shelf.id == shelf_id).first() else: @@ -273,6 +274,8 @@ def show_shelf(shelf_id): result = list() # user is allowed to access shelf if shelf: + page = "shelf.html" if type == 1 else 'shelfdown.html' + books_in_shelf = ub.session.query(ub.BookShelf).filter(ub.BookShelf.shelf == shelf_id).order_by( ub.BookShelf.order.asc()).all() for book in books_in_shelf: @@ -283,41 +286,13 @@ def show_shelf(shelf_id): app.logger.info('Not existing book %s in shelf %s deleted' % (book.book_id, shelf.id)) ub.session.query(ub.BookShelf).filter(ub.BookShelf.book_id == book.book_id).delete() ub.session.commit() - return render_title_template('shelf.html', entries=result, title=_(u"Shelf: '%(name)s'", name=shelf.name), + return render_title_template(page, entries=result, title=_(u"Shelf: '%(name)s'", name=shelf.name), shelf=shelf, page="shelf") else: flash(_(u"Error opening shelf. Shelf does not exist or is not accessible"), category="error") return redirect(url_for("web.index")) -@shelf.route("/shelfdown/") -@login_required -def show_shelf_down(shelf_id): - if current_user.is_anonymous: - shelf = ub.session.query(ub.Shelf).filter(ub.Shelf.is_public == 1, ub.Shelf.id == shelf_id).first() - else: - shelf = ub.session.query(ub.Shelf).filter(ub.or_(ub.and_(ub.Shelf.user_id == int(current_user.id), - ub.Shelf.id == shelf_id), - ub.and_(ub.Shelf.is_public == 1, - ub.Shelf.id == shelf_id))).first() - result = list() - # user is allowed to access shelf - if shelf: - books_in_shelf = ub.session.query(ub.BookShelf).filter(ub.BookShelf.shelf == shelf_id).order_by( - ub.BookShelf.order.asc()).all() - for book in books_in_shelf: - cur_book = db.session.query(db.Books).filter(db.Books.id == book.book_id).first() - if cur_book: - result.append(cur_book) - else: - app.logger.info('Not existing book %s in shelf %s deleted' % (book.book_id, shelf.id)) - ub.session.query(ub.BookShelf).filter(ub.BookShelf.book_id == book.book_id).delete() - ub.session.commit() - return render_title_template('shelfdown.html', entries=result, title=_(u"Shelf: '%(name)s'", name=shelf.name), - shelf=shelf, page="shelf") - else: - flash(_(u"Error opening shelf. Shelf does not exist or is not accessible"), category="error") - return redirect(url_for("web.index")) @shelf.route("/shelf/order/", methods=["GET", "POST"]) @login_required diff --git a/cps/templates/shelf.html b/cps/templates/shelf.html index 271a02fc..0e379750 100644 --- a/cps/templates/shelf.html +++ b/cps/templates/shelf.html @@ -3,7 +3,7 @@

{{title}}

{% if g.user.role_download() %} - {{ _('Download') }} + {{ _('Download') }} {% endif %} {% if g.user.is_authenticated %} {% if (g.user.role_edit_shelfs() and shelf.is_public ) or not shelf.is_public %}