Adding search functionality

This commit is contained in:
Denis-Cosmin Nutiu 2017-04-19 18:21:00 +03:00
parent a8bb3b15c3
commit b2d90b202d
4 changed files with 43 additions and 12 deletions

View file

@ -41,25 +41,30 @@
<span aria-hidden="true">&laquo;</span>
{% else %}
<li>
<a href="{{ url_for('scoreboard.index', page=pagination[2] - 1) }}" aria-label="Previous">
<a href="{{ url_for('scoreboard.index', page=pagination[2] - 1,
result_name=request.args.get('result_name')) }}" 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>
<li><a href="{{ url_for('scoreboard.index', page=i + 1,
result_name=request.args.get('result_name')) }}">{{ i + 1 }}</a></li>
{% endfor %}
<li class="active"><a href="{{ url_for('scoreboard.index', page=pagination[2]) }}">{{ pagination[2] }}</a></li>
<li class="active"><a href="{{ url_for('scoreboard.index', page=pagination[2],
result_name=request.args.get('result_name')) }}">{{ 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>
<li><a href="{{ url_for('scoreboard.index', page=i + 1,
result_name=request.args.get('result_name')) }}">{{ 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">
<a href="{{ url_for('scoreboard.index', page=pagination[2] + 1,
result_name=request.args.get('result_name')) }}" aria-label="Next">
<span aria-hidden="true">&raquo;</span>
</a>
{% endif %}

View file

@ -28,9 +28,15 @@
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li><a href="/">Home</a></li>
<li><a href="/upload">Upload</a></li>
<li><a href="{{ url_for("scoreboard.upload") }}">Upload</a></li>
<li><a href="//github.com/Metonimie/benchmark-scoreboard">Fork this Website</a></li>
</ul>
<form method="get" action="{{ url_for('scoreboard.index') }}" class="navbar-form navbar-left">
<div class="form-group">
<input type="text" class="form-control" name="result_name" placeholder="Search for name">
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
</div>
</div>
</div>

View file

@ -85,7 +85,7 @@ def result(id):
flask.abort(404)
@scoreboard.route("/")
@scoreboard.route("/", methods=['GET', 'POST'])
def index():
"""
This method returns the index page.
@ -93,15 +93,24 @@ def index():
results_per_page = flask.current_app.config["MAX_RESULTS_PER_PAGE"]
max_pages = flask.current_app.config["MAX_PAGES"]
# We're extracting the page argument from the url, if it's not present we set page_no to zero.
# We're extracting the page argument from the url, if it's not present we set page_no to zero.
page_no = utilities.to_zero_count(flask.request.args.get('page'))
searched_name = flask.request.args.get('result_name')
# Compute the offset and the available pages
results_length = Result.query.count()
# The filters dictionary is used to filter the data
filters = {}
if searched_name is not None and searched_name is not '':
filters['name'] = searched_name
# Computing the offset for the results
offset = page_no * results_per_page
available_pages = math.floor((results_length - offset) / results_per_page)
results = Result.query.order_by(Result.score.desc()).offset(offset).limit(results_per_page)
# We're getting the results length and data
results_length = Result.query.filter_by(**filters).count()
results = Result.query.filter_by(**filters).order_by(Result.score.desc()).offset(offset).limit(results_per_page)
# This is used by the view to display available pages, if any.
available_pages = math.floor((results_length - offset) / results_per_page)
# Compute the available pages to the left
pages_left = min(page_no, max_pages)

View file

@ -67,6 +67,17 @@ class ScoreboardTestCase(unittest.TestCase):
self.assertTrue("true" in response.get_data(as_text=True))
self.assertEqual(response.status_code, 201)
def test_search(self):
e = Result(name="Test", cpu="TestCPU", gpu="TestGPU", log="TestLOG", score=11)
response = self.client.post('/', data={"result_name": "test"})
self.assertTrue("No results found" in response.get_data(as_text=True))
db.session.add(e)
db.session.commit()
response = self.client.post('/', data={"result_name": "test"})
self.assertFalse("No results found" in response.get_data(as_text=True))
def test_get_upload(self):
response = self.client.get('/upload')
self.assertEqual(response.status_code, 200)