Skip to content
This repository has been archived by the owner on Feb 3, 2024. It is now read-only.

Pagination #68

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 25 additions & 30 deletions quotefault/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why change this?

quote_query = quote_query.order_by(Quote.id.desc())
# Filter hidden quotes
if not include_hidden:
quote_query = quote_query.filter(Quote.hidden == False)
Expand All @@ -206,48 +206,43 @@ 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/<page>', 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))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this really rows? Seems more like it's total number of pages


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()
return render_template(
'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
Comment on lines +244 to +245
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this logic not be done in jinja? seems kinda weird to pass these when you're already passing the page.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Min and Max are not jinja functions

)

@app.route('/report/<quote_id>', methods=['POST'])
Expand Down
62 changes: 0 additions & 62 deletions quotefault/static/js/load_more.js

This file was deleted.

2 changes: 0 additions & 2 deletions quotefault/templates/bootstrap/additional_quotes.html

This file was deleted.

32 changes: 28 additions & 4 deletions quotefault/templates/bootstrap/storage.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,34 @@
{% endif %}
{% endwith %}
{{ display( quotes ) }}
<button href="#moreQuotes" id="get_more" data-toggle="collapse" class="btn btn-default center-block">But wait, there's more!</button>
<br>
<div id="moreQuotes" class="collapse" aria-expanded="false">
</div>
<nav aria-label="page selector">
<ul class="pagination pagination-lg justify-content-center">

{% if page == 1 %}
<li class="page-item disabled">
<a class="page-link" href="/storage/{{ page - 1 }}" tabindex="-1">Previous</a>
{% else %}
<li class="page-item">
<a class="page-link" href="/storage/{{ page - 1 }}">Previous</a>
{% endif %}
</li>
{% for num in range(begin, end) %}
<li class="page-item">
<a class="page-link" href="/storage/{{ num }}">{{ num }}</a>
</li>
{% endfor %}
{% if page == rows %}
<li class="page-item disabled">
<a class="page-link" href="/storage/{{ page + 1 }}" tabindex="-1">Next</a>
{% else %}
<li class="page-item">
<a class="page-link" href="/storage/{{ page + 1 }}">Next</a>
{% endif %}
</li>

</li>
</ul>
</nav>
</div>
{% endblock %}

Expand Down