diff --git a/quotefault/__init__.py b/quotefault/__init__.py index edf2e46..cf2a353 100644 --- a/quotefault/__init__.py +++ b/quotefault/__init__.py @@ -192,7 +192,7 @@ def get_quote_query(speaker: str = "", submitter: str = "", include_hidden: bool quote_query = db.session.query(Quote, func.sum(Vote.direction).label('votes')).outerjoin(Vote).group_by(Quote) # Put the most recent first - quote_query = quote_query.order_by(Quote.quote_time.desc()) + quote_query = quote_query.order_by(Quote.id.desc()) # Filter hidden quotes if not include_hidden: quote_query = quote_query.filter(Quote.hidden == False) @@ -206,15 +206,31 @@ def get_quote_query(speaker: str = "", submitter: str = "", include_hidden: bool # display first 20 stored quotes @app.route('/storage', methods=['GET']) @auth.oidc_auth -def get(): +def default_get(): + return redirect("/storage/1") + +# display first 20 stored quotes +@app.route('/storage/', methods=['GET']) +@auth.oidc_auth +def get(page): """ Show submitted quotes, only showing first 20 initially """ metadata = get_metadata() + page = int(page) + + query = get_quote_query(speaker = request.args.get('speaker'), + submitter = request.args.get('submitter')) + + rows = query.count() + rows = int(rows//20 + bool(rows%20 > 0)) + + if page > rows or page < 1: + return "Page Out of Bounds", 404 + # Get the most recent 20 quotes - quotes = get_quote_query(speaker = request.args.get('speaker'), - submitter = request.args.get('submitter')).limit(20).all() + quotes = query.offset((page-1)*20).limit(20).all() #tie any votes the user has made to their uid user_votes = Vote.query.filter(Vote.voter == metadata['uid']).all() @@ -222,32 +238,11 @@ def get(): 'bootstrap/storage.html', quotes=quotes, metadata=metadata, - user_votes=user_votes - ) - - -# display ALL stored quotes -@app.route('/additional', methods=['GET']) -@auth.oidc_auth -def additional_quotes(): - """ - Show beyond the first 20 quotes - """ - - metadata = get_metadata() - - # Get all the quotes - quotes = get_quote_query(speaker = request.args.get('speaker'), - submitter = request.args.get('submitter')).all() - - #tie any votes the user has made to their uid - user_votes = db.session.query(Vote).filter(Vote.voter == metadata['uid']).all() - - return render_template( - 'bootstrap/additional_quotes.html', - quotes=quotes[20:], - metadata=metadata, - user_votes=user_votes + user_votes=user_votes, + page=page, + rows=rows, + begin=max(1, page-6), + end=min(page+6, rows) + 1 ) @app.route('/report/', methods=['POST']) diff --git a/quotefault/static/js/load_more.js b/quotefault/static/js/load_more.js deleted file mode 100644 index 5ac7c9c..0000000 --- a/quotefault/static/js/load_more.js +++ /dev/null @@ -1,62 +0,0 @@ -function buttonAjax(buttonElem, options, failOnly) { - if (failOnly === undefined) { - failOnly = false; - } - - // Get original html and click events - var html = buttonElem.html(); - - // Disable button - buttonElem - .prop('disabled', true) - .css('pointer-events', 'none') - .css('cursor', 'not-allowed'); - - // Prepend spinner - buttonElem.html(' ' + html); - - var disableFunc = function () { - // Reset original html - buttonElem.html(html); - - // Enable button - buttonElem - .css('pointer-events', '') - .prop('disabled', false) - .css('cursor', ''); - }; - - // Send AJAX - if (failOnly === true) { - return $.ajax(options).always(fail); - } else { - return $.ajax(options).always(disableFunc); - } -} - -$(function () { - $("#get_more").one("click", function (e) { - //Prepare the url with the proper query strings - let urlParams = new URLSearchParams(window.location.search); - let speaker = urlParams.get('speaker'); - let submitter = urlParams.get('submitter'); - let urlStr = `/additional`; - if(speaker){ - urlStr+=`?speaker=${speaker}`; - } - if(submitter){ - urlStr+=`?submitter=${submitter}`; - } - buttonAjax($(this), { - url: urlStr, - method: 'GET', - success: function (data, textStatus, jqXHR) { - $("#moreQuotes").html(data) - .collapse("show"); - }, - error: function (error) { - console.log(error); - } - }); - }); -}); diff --git a/quotefault/templates/bootstrap/additional_quotes.html b/quotefault/templates/bootstrap/additional_quotes.html deleted file mode 100644 index aed71bf..0000000 --- a/quotefault/templates/bootstrap/additional_quotes.html +++ /dev/null @@ -1,2 +0,0 @@ -{% from 'bootstrap/macro.html' import display_quotes as display with context %} -{{ display( quotes ) }} \ No newline at end of file diff --git a/quotefault/templates/bootstrap/storage.html b/quotefault/templates/bootstrap/storage.html index 16eba26..6c8c363 100644 --- a/quotefault/templates/bootstrap/storage.html +++ b/quotefault/templates/bootstrap/storage.html @@ -18,10 +18,34 @@ {% endif %} {% endwith %} {{ display( quotes ) }} - -
- + {% endblock %}