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

Commit 50f7a0d

Browse files
committed
Pagination
1 parent 3a46e07 commit 50f7a0d

File tree

4 files changed

+26
-98
lines changed

4 files changed

+26
-98
lines changed

quotefault/__init__.py

+20-30
Original file line numberDiff line numberDiff line change
@@ -186,13 +186,13 @@ def submit():
186186
), 200
187187

188188

189-
def get_quote_query(speaker: str = "", submitter: str = "", include_hidden: bool = False):
189+
def get_quote_query(speaker: str = "", submitter: str = "", last_id = None, include_hidden: bool = False):
190190
"""Return a query based on the args, with vote count attached to the quotes"""
191191
# Get all the quotes with their votes
192192
quote_query = db.session.query(Quote,
193193
func.sum(Vote.direction).label('votes')).outerjoin(Vote).group_by(Quote)
194194
# Put the most recent first
195-
quote_query = quote_query.order_by(Quote.quote_time.desc())
195+
quote_query = quote_query.order_by(Quote.id.desc())
196196
# Filter hidden quotes
197197
if not include_hidden:
198198
quote_query = quote_query.filter(Quote.hidden == False)
@@ -201,53 +201,43 @@ def get_quote_query(speaker: str = "", submitter: str = "", include_hidden: bool
201201
quote_query = quote_query.filter(Quote.speaker == request.args.get('speaker'))
202202
if request.args.get('submitter'):
203203
quote_query = quote_query.filter(Quote.submitter == request.args.get('submitter'))
204+
if last_id:
205+
quote_query = quote_query.filter(Quote.id < last_id)
204206
return quote_query
205207

206208
# display first 20 stored quotes
207209
@app.route('/storage', methods=['GET'])
208210
@auth.oidc_auth
209-
def get():
211+
def default_get():
212+
return redirect("/storage/1")
213+
214+
# display first 20 stored quotes
215+
@app.route('/storage/<page>', methods=['GET'])
216+
@auth.oidc_auth
217+
def get(page):
210218
"""
211219
Show submitted quotes, only showing first 20 initially
212220
"""
213221
metadata = get_metadata()
214222

223+
page = int(page)
224+
215225
# Get the most recent 20 quotes
216226
quotes = get_quote_query(speaker = request.args.get('speaker'),
217-
submitter = request.args.get('submitter')).limit(20).all()
227+
submitter = request.args.get('submitter')).offset((page-1)*20).limit(20).all()
228+
229+
last_id = list(quotes)[-1][0].id
230+
231+
last_id = list(quotes)[-1][0].id
218232

219233
#tie any votes the user has made to their uid
220234
user_votes = Vote.query.filter(Vote.voter == metadata['uid']).all()
221235
return render_template(
222236
'bootstrap/storage.html',
223237
quotes=quotes,
224238
metadata=metadata,
225-
user_votes=user_votes
226-
)
227-
228-
229-
# display ALL stored quotes
230-
@app.route('/additional', methods=['GET'])
231-
@auth.oidc_auth
232-
def additional_quotes():
233-
"""
234-
Show beyond the first 20 quotes
235-
"""
236-
237-
metadata = get_metadata()
238-
239-
# Get all the quotes
240-
quotes = get_quote_query(speaker = request.args.get('speaker'),
241-
submitter = request.args.get('submitter')).all()
242-
243-
#tie any votes the user has made to their uid
244-
user_votes = db.session.query(Vote).filter(Vote.voter == metadata['uid']).all()
245-
246-
return render_template(
247-
'bootstrap/additional_quotes.html',
248-
quotes=quotes[20:],
249-
metadata=metadata,
250-
user_votes=user_votes
239+
user_votes=user_votes,
240+
page=page
251241
)
252242

253243
@app.route('/report/<quote_id>', methods=['POST'])

quotefault/static/js/load_more.js

-62
This file was deleted.

quotefault/templates/bootstrap/additional_quotes.html

-2
This file was deleted.

quotefault/templates/bootstrap/storage.html

+6-4
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,18 @@
1818
{% endif %}
1919
{% endwith %}
2020
{{ display( quotes ) }}
21-
<button href="#moreQuotes" id="get_more" data-toggle="collapse" class="btn btn-default center-block">But wait, there's more!</button>
22-
<br>
23-
<div id="moreQuotes" class="collapse" aria-expanded="false">
24-
</div>
21+
<a href="/storage/{{ page + 1 }}">
22+
<button id="get_more" class="btn btn-default center-block">But wait, there's more!</button>
23+
</a>
2524
</div>
2625
{% endblock %}
2726

2827
{% block scripts %}
2928
<script src="{{ url_for('static', filename='js/load_more.js') }}"></script>
3029
<script src="{{ url_for('static', filename='css/votes/upvote.js') }}"></script>
3130
<script src="{{ url_for('static', filename='js/vote.js') }}"></script>
31+
<script>
32+
localStorage.setItem("last_id", {{ last_id }});
33+
</script>
3234
{% endblock %}
3335

0 commit comments

Comments
 (0)