From 3d8fb2af0f3954fba95c27cda9308b852784255c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Denis=20Nut=CC=A6iu?= Date: Tue, 18 Apr 2017 18:44:50 +0300 Subject: [PATCH] Adding basic pagination support --- src/config_lock.py | 5 ++++- src/templates/index.html | 42 ++++++++++++++++++++++++++++++++++++++++ src/views/scoreboard.py | 28 +++++++++++++++++++++++++-- test/test_scoreboard.py | 30 ++++++++++++++++++++++++++++ 4 files changed, 102 insertions(+), 3 deletions(-) diff --git a/src/config_lock.py b/src/config_lock.py index e92f693..94f92da 100644 --- a/src/config_lock.py +++ b/src/config_lock.py @@ -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 diff --git a/src/templates/index.html b/src/templates/index.html index 38862c3..576583a 100644 --- a/src/templates/index.html +++ b/src/templates/index.html @@ -30,5 +30,47 @@ {% endfor %} + + {# [0] -> Results Lenght, [1] -> Max Items Per Page, [2] -> Current Page, [3] -> Pages Left, [4] -> Pages Right #} + {% if pagination[0] > pagination[1] %} + + {% elif pagination[0] == 0 %} +
+

No results found :(

+
+ {% endif %} {% endblock %} \ No newline at end of file diff --git a/src/views/scoreboard.py b/src/views/scoreboard.py index 0fa724e..8f62e65 100644 --- a/src/views/scoreboard.py +++ b/src/views/scoreboard.py @@ -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) diff --git a/test/test_scoreboard.py b/test/test_scoreboard.py index 443a5ff..c657206 100644 --- a/test/test_scoreboard.py +++ b/test/test_scoreboard.py @@ -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)