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

Commit 6a8930c

Browse files
committed
Pagination
1 parent 3a46e07 commit 6a8930c

File tree

4 files changed

+25
-81
lines changed

4 files changed

+25
-81
lines changed

quotefault/__init__.py

+19-13
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,6 +201,8 @@ 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
@@ -216,38 +218,42 @@ def get():
216218
quotes = get_quote_query(speaker = request.args.get('speaker'),
217219
submitter = request.args.get('submitter')).limit(20).all()
218220

221+
last_id = list(quotes)[-1][0].id
222+
219223
#tie any votes the user has made to their uid
220224
user_votes = Vote.query.filter(Vote.voter == metadata['uid']).all()
221225
return render_template(
222226
'bootstrap/storage.html',
223227
quotes=quotes,
224228
metadata=metadata,
225-
user_votes=user_votes
229+
user_votes=user_votes,
230+
last_id = last_id
226231
)
227232

228233

229234
# display ALL stored quotes
230-
@app.route('/additional', methods=['GET'])
235+
@app.route('/storage/<last_id>', methods=['GET'])
231236
@auth.oidc_auth
232-
def additional_quotes():
237+
def additional_quotes(last_id):
233238
"""
234239
Show beyond the first 20 quotes
235240
"""
236-
237241
metadata = get_metadata()
238242

239-
# Get all the quotes
243+
# Get the most recent 20 quotes
240244
quotes = get_quote_query(speaker = request.args.get('speaker'),
241-
submitter = request.args.get('submitter')).all()
245+
submitter = request.args.get('submitter'), last_id = last_id).limit(20).all()
242246

243-
#tie any votes the user has made to their uid
244-
user_votes = db.session.query(Vote).filter(Vote.voter == metadata['uid']).all()
247+
last_id = list(quotes)[-1][0].id
245248

249+
#tie any votes the user has made to their uid
250+
user_votes = Vote.query.filter(Vote.voter == metadata['uid']).all()
246251
return render_template(
247-
'bootstrap/additional_quotes.html',
248-
quotes=quotes[20:],
252+
'bootstrap/storage.html',
253+
quotes=quotes,
249254
metadata=metadata,
250-
user_votes=user_votes
255+
user_votes=user_votes,
256+
last_id = last_id
251257
)
252258

253259
@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/{{ last_id }}">
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)