Adding basic pagination support

This commit is contained in:
Denis-Cosmin Nutiu 2017-04-18 18:44:50 +03:00
parent 32dcd5596a
commit 3d8fb2af0f
4 changed files with 102 additions and 3 deletions

View file

@ -31,7 +31,8 @@ class Config:
APP_IP = "0.0.0.0"
APP_PORT = 5000
# Pagination
MAX_RESULTS_PER_PAGE = 10
MAX_RESULTS_PER_PAGE = 50
MAX_PAGES = 2
@staticmethod
def init_app(app):
@ -49,6 +50,7 @@ class DevelopmentConfig(Config):
class ProductionConfig(Config):
BOOTSTRAP_USE_MINIFIED = True
# Database Configuration
MYSQL_USERNAME = ""
MYSQL_PASSWORD = ""
MYSQL_HOSTNAME = ""
@ -63,6 +65,7 @@ class ProductionConfig(Config):
db.create_all()
class TestingConfig(Config):
MAX_RESULTS_PER_PAGE = 1
SQLALCHEMY_DATABASE_URI = 'sqlite:///' + os.path.join(basedir, 'test_database.sqlite')
@staticmethod

View file

@ -30,5 +30,47 @@
{% endfor %}
</tbody>
</table>
{# [0] -> Results Lenght, [1] -> Max Items Per Page, [2] -> Current Page, [3] -> Pages Left, [4] -> Pages Right #}
{% if pagination[0] > pagination[1] %}
<nav aria-label="Page navigation">
<div class="text-center">
<ul class="pagination">
{% if pagination[2] <= 1 %}
<li class="disabled">
<span aria-hidden="true">&laquo;</span>
{% else %}
<li>
<a href="{{ url_for('scoreboard.index', page=pagination[2] - 1) }}" aria-label="Previous">
<span aria-hidden="true">&laquo;</span>
</a>
</li>
{% endif %}
{# Things are kinda messy here #}
{% for i in range(pagination[2] - pagination[3] - 1, pagination[2] - 1) %}
<li><a href="{{ url_for('scoreboard.index', page=i + 1) }}">{{ i + 1 }}</a></li>
{% endfor %}
<li class="active"><a href="{{ url_for('scoreboard.index', page=pagination[2]) }}">{{ pagination[2] }}</a></li>
{% for i in range(pagination[2], pagination[2] + pagination[4]) %}
<li><a href="{{ url_for('scoreboard.index', page=i + 1) }}">{{ i + 1 }}</a></li>
{% endfor %}
{% if pagination[4] <= 0 %}
<li class="disabled">
<span aria-hidden="true">&raquo;</span>
{% else %}
<li>
<a href="{{ url_for('scoreboard.index', page=pagination[2] + 1) }}" aria-label="Next">
<span aria-hidden="true">&raquo;</span>
</a>
{% endif %}
</li>
</ul>
</div>
</nav>
{% elif pagination[0] == 0 %}
<div class="text-center">
<h2>No results found :(</h2>
</div>
{% endif %}
</div>
{% endblock %}

View file

@ -18,6 +18,7 @@
from src.models import Result
from src.models import db
import json
import math
import flask
scoreboard = flask.Blueprint('scoreboard', __name__, template_folder='templates')
@ -76,6 +77,29 @@ def index():
"""
This method returns the index page.
"""
# TODO: Add pagination. Research ?page, check if presend, send no of records to template
results_per_page = flask.current_app.config["MAX_RESULTS_PER_PAGE"]
max_pages = flask.current_app.config["MAX_PAGES"]
results = Result.query.order_by(Result.score.desc()).all()
return flask.render_template("index.html", results=results)
# We're extracting the page argument from the url, if it's not present we set page_no to zero.
page_no = flask.request.args.get('page')
try:
page_no = int(page_no) - 1
if page_no < 0: page_no = 0
except (TypeError, ValueError): # page_no is not an int
page_no = 0
offset = page_no * results_per_page
available_pages = math.floor((len(results) - offset) / results_per_page)
# Compute the available pages to the left
pages_left = min(page_no, max_pages)
# Compute the available pages to the right
pages_right = min(max_pages, available_pages)
# Create pagination information tuple
pagination_information = len(results), results_per_page, page_no + 1, pages_left, pages_right
return flask.render_template("index.html",
results=results[offset:offset + results_per_page],
pagination=pagination_information)

View file

@ -69,3 +69,33 @@ class ScoreboardTestCase(unittest.TestCase):
def test_get_upload(self):
response = self.client.get('/upload')
self.assertEqual(response.status_code, 200)
def test_pagination(self):
data1 = Result(cpu="TestCPU", gpu="TestGPU", log="TestLOG", score=11)
data2 = Result(cpu="TestCPU", gpu="TestGPU", log="TestLOG", score=22)
data3 = Result(cpu="TestCPU", gpu="TestGPU", log="TestLOG", score=33)
response = self.client.get("/")
self.assertTrue("No results found" in response.get_data(as_text=True))
self.assertEqual(response.status_code, 200)
db.session.add(data1)
db.session.commit()
response = self.client.get("/?page=1")
self.assertTrue("11" in response.get_data(as_text=True))
self.assertEqual(response.status_code, 200)
db.session.add(data2)
db.session.commit()
response = self.client.get("/?page=2")
self.assertTrue("11" in response.get_data(as_text=True))
self.assertEqual(response.status_code, 200)
db.session.add(data3)
db.session.commit()
response = self.client.get("/?page=3")
self.assertTrue("11" in response.get_data(as_text=True))
self.assertEqual(response.status_code, 200)