From bab14a1fbf31739fad032b4207b38175158f7037 Mon Sep 17 00:00:00 2001 From: Ozzieisaacs Date: Thu, 2 Apr 2020 18:23:24 +0200 Subject: [PATCH] Update shelfs handling, bugfix changed updater --- cps/opds.py | 65 +++++++++++++++++++++++++++------------ cps/server.py | 3 +- cps/shelf.py | 47 +++++++++++++++++++++------- cps/templates/detail.html | 6 ++-- cps/templates/feed.xml | 4 +++ cps/templates/index.xml | 15 ++------- cps/templates/layout.html | 12 ++++---- cps/templates/search.html | 4 +-- cps/web.py | 4 +-- 9 files changed, 103 insertions(+), 57 deletions(-) diff --git a/cps/opds.py b/cps/opds.py index f76d5dc0..6e98e539 100644 --- a/cps/opds.py +++ b/cps/opds.py @@ -25,7 +25,7 @@ import sys import datetime from functools import wraps -from flask import Blueprint, request, render_template, Response, g, make_response +from flask import Blueprint, request, render_template, Response, g, make_response, abort from flask_login import current_user from sqlalchemy.sql.expression import func, text, or_, and_ from werkzeug.security import check_password_hash @@ -165,7 +165,10 @@ def feed_author(book_id): off = request.args.get("offset") or 0 entries, __, pagination = fill_indexpage((int(off) / (int(config.config_books_per_page)) + 1), db.Books, db.Books.authors.any(db.Authors.id == book_id), [db.Books.timestamp.desc()]) - return render_xml_template('feed.xml', entries=entries, pagination=pagination) + if len(entries): + return render_xml_template('feed.xml', entries=entries, pagination=pagination) + else: + return abort(404) @opds.route("/opds/publisher") @@ -186,7 +189,10 @@ def feed_publisher(book_id): entries, __, pagination = fill_indexpage((int(off) / (int(config.config_books_per_page)) + 1), db.Books, db.Books.publishers.any(db.Publishers.id == book_id), [db.Books.timestamp.desc()]) - return render_xml_template('feed.xml', entries=entries, pagination=pagination) + if len(entries): + return render_xml_template('feed.xml', entries=entries, pagination=pagination) + else: + return abort(404) @opds.route("/opds/category") @@ -206,7 +212,10 @@ def feed_category(book_id): off = request.args.get("offset") or 0 entries, __, pagination = fill_indexpage((int(off) / (int(config.config_books_per_page)) + 1), db.Books, db.Books.tags.any(db.Tags.id == book_id), [db.Books.timestamp.desc()]) - return render_xml_template('feed.xml', entries=entries, pagination=pagination) + if len(entries): + return render_xml_template('feed.xml', entries=entries, pagination=pagination) + else: + return abort(404) @opds.route("/opds/series") @@ -226,7 +235,10 @@ def feed_series(book_id): off = request.args.get("offset") or 0 entries, __, pagination = fill_indexpage((int(off) / (int(config.config_books_per_page)) + 1), db.Books, db.Books.series.any(db.Series.id == book_id), [db.Books.series_index]) - return render_xml_template('feed.xml', entries=entries, pagination=pagination) + if len(entries): + return render_xml_template('feed.xml', entries=entries, pagination=pagination) + else: + return abort(404) @opds.route("/opds/ratings") @requires_basic_auth_if_no_ano @@ -250,7 +262,11 @@ def feed_ratings(book_id): off = request.args.get("offset") or 0 entries, __, pagination = fill_indexpage((int(off) / (int(config.config_books_per_page)) + 1), db.Books, db.Books.ratings.any(db.Ratings.id == book_id),[db.Books.timestamp.desc()]) - return render_xml_template('feed.xml', entries=entries, pagination=pagination) + if len(entries): + return render_xml_template('feed.xml', entries=entries, pagination=pagination) + else: + return abort(404) + @opds.route("/opds/formats") @@ -274,7 +290,11 @@ def feed_format(book_id): off = request.args.get("offset") or 0 entries, __, pagination = fill_indexpage((int(off) / (int(config.config_books_per_page)) + 1), db.Books, db.Books.data.any(db.Data.format == book_id.upper()), [db.Books.timestamp.desc()]) - return render_xml_template('feed.xml', entries=entries, pagination=pagination) + if len(entries): + return render_xml_template('feed.xml', entries=entries, pagination=pagination) + else: + return abort(404) + @opds.route("/opds/language") @opds.route("/opds/language/") @@ -305,20 +325,23 @@ def feed_languages(book_id): off = request.args.get("offset") or 0 entries, __, pagination = fill_indexpage((int(off) / (int(config.config_books_per_page)) + 1), db.Books, db.Books.languages.any(db.Languages.id == book_id), [db.Books.timestamp.desc()]) - return render_xml_template('feed.xml', entries=entries, pagination=pagination) - - -@opds.route("/opds/shelfindex", defaults={'public': 0}) -@opds.route("/opds/shelfindex/") -@requires_basic_auth_if_no_ano -def feed_shelfindex(public): - off = request.args.get("offset") or 0 - if public != 0: - shelf = g.public_shelfes - number = len(shelf) + if len(entries): + return render_xml_template('feed.xml', entries=entries, pagination=pagination) else: - shelf = g.user.shelf - number = shelf.count() + return abort(404) + + +@opds.route("/opds/shelfindex") #, defaults={'public': 0}) +# @opds.route("/opds/shelfindex/") +@requires_basic_auth_if_no_ano +def feed_shelfindex(): + off = request.args.get("offset") or 0 + # if public != 0: + shelf = g.shelves_access + number = len(shelf) + #else: + #shelf = g.user.shelf + # number = shelf.count() pagination = Pagination((int(off) / (int(config.config_books_per_page)) + 1), config.config_books_per_page, number) return render_xml_template('feed.xml', listelements=shelf, folder='opds.feed_shelf', pagination=pagination) @@ -346,6 +369,8 @@ def feed_shelf(book_id): pagination = Pagination((int(off) / (int(config.config_books_per_page)) + 1), config.config_books_per_page, len(result)) return render_xml_template('feed.xml', entries=result, pagination=pagination) + else: + return abort(404) @opds.route("/opds/download///") diff --git a/cps/server.py b/cps/server.py index a59d7a38..a108181b 100755 --- a/cps/server.py +++ b/cps/server.py @@ -24,7 +24,7 @@ import signal import socket try: - from gevent.pyewsgi import WSGIServer + from gevent.pywsgi import WSGIServer from gevent.pool import Pool from gevent import __version__ as _version VERSION = 'Gevent ' + _version @@ -171,6 +171,7 @@ class WebServer(object): except Exception as ex: log.error("Error starting server: %s", ex) print("Error starting server: %s" % ex) + self.stop() return False finally: self.wsgiserver = None diff --git a/cps/shelf.py b/cps/shelf.py index afee1eaa..02e910eb 100644 --- a/cps/shelf.py +++ b/cps/shelf.py @@ -204,12 +204,24 @@ def create_shelf(): shelf.is_public = 1 shelf.name = to_save["title"] shelf.user_id = int(current_user.id) - existing_shelf = ub.session.query(ub.Shelf).filter( - or_((ub.Shelf.name == to_save["title"]) & (ub.Shelf.is_public == 1), - (ub.Shelf.name == to_save["title"]) & (ub.Shelf.user_id == int(current_user.id)))).first() - if existing_shelf: - flash(_(u"A shelf with the name '%(title)s' already exists.", title=to_save["title"]), category="error") + + is_shelf_name_unique = False + if shelf.is_public == 1: + is_shelf_name_unique = ub.session.query(ub.Shelf) \ + .filter((ub.Shelf.name == to_save["title"]) & (ub.Shelf.is_public == 1)) \ + .first() is None + + if not is_shelf_name_unique: + flash(_(u"A public shelf with the name '%(title)s' already exists.", title=to_save["title"]), category="error") else: + is_shelf_name_unique = ub.session.query(ub.Shelf) \ + .filter((ub.Shelf.name == to_save["title"]) & (ub.Shelf.is_public == 0) & (ub.Shelf.user_id == int(current_user.id))) \ + .first() is None + + if not is_shelf_name_unique: + flash(_(u"A private shelf with the name '%(title)s' already exists.", title=to_save["title"]), category="error") + + if is_shelf_name_unique: try: ub.session.add(shelf) ub.session.commit() @@ -227,13 +239,26 @@ def edit_shelf(shelf_id): shelf = ub.session.query(ub.Shelf).filter(ub.Shelf.id == shelf_id).first() if request.method == "POST": to_save = request.form.to_dict() - existing_shelf = ub.session.query(ub.Shelf).filter( - or_((ub.Shelf.name == to_save["title"]) & (ub.Shelf.is_public == 1), - (ub.Shelf.name == to_save["title"]) & (ub.Shelf.user_id == int(current_user.id)))).filter( - ub.Shelf.id != shelf_id).first() - if existing_shelf: - flash(_(u"A shelf with the name '%(title)s' already exists.", title=to_save["title"]), category="error") + + is_shelf_name_unique = False + if shelf.is_public == 1: + is_shelf_name_unique = ub.session.query(ub.Shelf) \ + .filter((ub.Shelf.name == to_save["title"]) & (ub.Shelf.is_public == 1)) \ + .filter(ub.Shelf.id != shelf_id) \ + .first() is None + + if not is_shelf_name_unique: + flash(_(u"A public shelf with the name '%(title)s' already exists.", title=to_save["title"]), category="error") else: + is_shelf_name_unique = ub.session.query(ub.Shelf) \ + .filter((ub.Shelf.name == to_save["title"]) & (ub.Shelf.is_public == 0) & (ub.Shelf.user_id == int(current_user.id))) \ + .filter(ub.Shelf.id != shelf_id) \ + .first() is None + + if not is_shelf_name_unique: + flash(_(u"A private shelf with the name '%(title)s' already exists.", title=to_save["title"]), category="error") + + if is_shelf_name_unique: shelf.name = to_save["title"] if "is_public" in to_save: shelf.is_public = 1 diff --git a/cps/templates/detail.html b/cps/templates/detail.html index d84dbe61..00510a2b 100644 --- a/cps/templates/detail.html +++ b/cps/templates/detail.html @@ -217,7 +217,7 @@
{% if g.user.is_authenticated %} - {% if g.user.shelf.all() or g.public_shelfes %} + {% if g.user.shelf.all() or g.shelves_access %}