From 605ab18b8a9d2a74a05f450ab673d303007c809b Mon Sep 17 00:00:00 2001 From: AndyGrant Date: Sat, 23 Jun 2018 15:07:19 -0400 Subject: [PATCH] Add paging to the GUI --- OpenBench/static/style.css | 17 +++++ OpenBench/urls.py | 2 + OpenBench/utils.py | 30 +++++++++ OpenBench/views.py | 38 +++++++++-- Templates/OpenBench/eventLog.html | 13 ++++ Templates/OpenBench/index.html | 105 +++++++++++++++++------------- 6 files changed, 154 insertions(+), 51 deletions(-) diff --git a/OpenBench/static/style.css b/OpenBench/static/style.css index bb5620c..a9fafe7 100644 --- a/OpenBench/static/style.css +++ b/OpenBench/static/style.css @@ -188,6 +188,23 @@ body { font-weight: bold; } +#content #pagebrowse { + text-align: center; + margin-top: 16px; +} + +#content .page { + border-style: solid; + border-width: 1px; + border-color: black; + border-radius: 16px; + text-align: center; + display: inline-block; + padding-top : 8px; + padding-bottom: 8px; + width: 5%; +} + #error { background-color: #ED7676; padding: 8px; diff --git a/OpenBench/urls.py b/OpenBench/urls.py index 3f26127..2d570a9 100644 --- a/OpenBench/urls.py +++ b/OpenBench/urls.py @@ -14,8 +14,10 @@ urlpatterns = [ path(r'index//', OpenBench.views.index), path(r'users/', OpenBench.views.users), path(r'viewUser//', OpenBench.views.viewUser), + path(r'viewUser///', OpenBench.views.viewUser), path(r'machines/', OpenBench.views.machines), path(r'eventLog/', OpenBench.views.eventLog), + path(r'eventLog//', OpenBench.views.eventLog), path(r'newTest/', OpenBench.views.newTest), path(r'viewTest//', OpenBench.views.viewTest), diff --git a/OpenBench/utils.py b/OpenBench/utils.py index 6ef4bad..66c2fb3 100644 --- a/OpenBench/utils.py +++ b/OpenBench/utils.py @@ -7,6 +7,36 @@ from django.contrib.auth import authenticate from OpenBench.config import * from OpenBench.models import Engine, Profile, Machine, Result, Test +def pagingContext(page, pageLength, items, url): + + # Tests within the given page + start = page * pageLength + end = min(start + pageLength, items) + + # Get page numbers near the current page + pagenum = math.ceil(items / pageLength) + temp = list(range(0, min(3, pagenum))) + temp.extend(range(pagenum-1, pagenum-4, -1)) + temp.extend(range(max(0, page-2), min(pagenum-1, page+3))) + temp = list(set(temp)) + temp.sort() + + # Fill in gaps with an ellipsis, throw out negatives + pages = [] + for f in range(len(temp)): + if temp[f] < 0: continue + pages.append(temp[f]) + if f < len(temp) - 1 and temp[f] != temp[f+1] - 1: + pages.append("...") + + # Create context dictionary for paging setup + return { + "url" : url, + "page" : page, + "pages" : pages, + "prev" : max(0, page - 1), + "next" : max(0, min(page + 1, pagenum - 1)), + } def getSourceLocation(branch, repo): diff --git a/OpenBench/views.py b/OpenBench/views.py index 86dcc6d..90ad48c 100644 --- a/OpenBench/views.py +++ b/OpenBench/views.py @@ -12,6 +12,7 @@ from django.utils.timezone import utc from OpenBench.config import * from OpenBench.models import LogEvent, Engine, Profile from OpenBench.models import Machine, Result, Test +from OpenBench.utils import pagingContext import OpenBench.utils, datetime @@ -106,7 +107,7 @@ def editProfile(request): # Send back to see the changes return HttpResponseRedirect('/viewProfile/') -def index(request, page=0, username=None, error=''): +def index(request, page=0, pageLength=50, username=None, error=''): # Get tests pending approval pending = Test.objects.filter(approved=False) @@ -130,13 +131,25 @@ def index(request, page=0, username=None, error=''): pending = pending.filter(author=username) active = active.filter(author=username) completed = completed.filter(author=username) + source = "viewUser/{0}".format(username) + else: + source = "index" + + # Choose tests within the given page, if any + items = len(completed) + start = page * pageLength + end = start + pageLength + start = max(0, min(start, items)) + end = max(0, min(end, items)) + paging = pagingContext(page, pageLength, items, source) # Build context dictionary for index template data = { 'pending' : pending, 'active' : active, - 'completed' : completed[:50], + 'completed' : completed[start:end], 'error' : error, + 'paging' : paging, } return render(request, 'index.html', data) @@ -147,10 +160,10 @@ def users(request): data = {'profiles' : Profile.objects.order_by('games')} return render(request, 'users.html', data) -def viewUser(request, username): +def viewUser(request, username, page=0): # Index but with only username's tests - return index(request, username=username) + return index(request, page, username=username) def machines(request): @@ -159,10 +172,23 @@ def machines(request): data = {'machines' : Machine.objects.filter(updated__gte=target)} return render(request, 'machines.html', data) -def eventLog(request): +def eventLog(request, page=0, pageLength=50): + + # Choose events within the given page, if any + events = LogEvent.objects.all() + items = len(events) + start = page * pageLength + end = start + pageLength + start = max(0, min(start, items)) + end = max(0, min(end, items)) + paging = pagingContext(page, pageLength, items, 'eventLog') # Build context dictionary for event log template - data = {'events': LogEvent.objects.all().order_by('-id')[:50]} + data = { + 'events': events[start:end], + 'paging': paging + } + return render(request, 'eventLog.html', data) @login_required(login_url='/login/') diff --git a/Templates/OpenBench/eventLog.html b/Templates/OpenBench/eventLog.html index 666dd4e..db016ae 100644 --- a/Templates/OpenBench/eventLog.html +++ b/Templates/OpenBench/eventLog.html @@ -21,4 +21,17 @@ {% endfor %} +
+ {% if paging.prev != paging.next %} + <-- + {% for page in paging.pages %} + {% if page != "..." %} + {{page}} + {% else %} + {{page}} + {% endif %} + {% endfor %} + --> + {% endif %} +
{% endblock %} diff --git a/Templates/OpenBench/index.html b/Templates/OpenBench/index.html index a91cb44..2a4b1fe 100644 --- a/Templates/OpenBench/index.html +++ b/Templates/OpenBench/index.html @@ -2,52 +2,54 @@ {% load mytags %} {% block content %}
- - {% if pending %} - - - - - - - - - + {% if paging.page == 0 %} + + {% if pending %} + + + + + + + + + + {% endif %} + {% for test in pending %} + + + + + + + + + + {% endfor %} + + {% if active %} + + + + + + + + + + {% endif %} + {% for test in active %} + + + + + + + + + + {% endfor %} {% endif %} - {% for test in pending %} - - - - - - - - - - {% endfor %} - - {% if active %} - - - - - - - - - - {% endif %} - {% for test in active %} - - - - - - - - - - {% endfor %} {% if completed %} @@ -72,4 +74,17 @@ {% endfor %}

Pending

CreationAuthorTestDiffPTimeStats

Pending

CreationAuthorTestDiffPTimeStats
{{test.creation|date:'Y-m-d'}}{{test.author}}{{test.dev.name}}diff{{test.priority}}{{test.timecontrol}}{{test|shortStatBlock|linebreaksbr}}

Active

CreationAuthorTestDiffPTimeStats
{{test.creation|date:'Y-m-d'}}{{test.author}}{{test.dev.name}}diff{{test.priority}}{{test.timecontrol}}{{test|shortStatBlock|linebreaksbr}}
{{test.creation|date:'Y-m-d'}}{{test.author}}{{test.dev.name}}diff{{test.priority}}{{test.timecontrol}}{{test|shortStatBlock|linebreaksbr}}

Active

CreationAuthorTestDiffPTimeStats
{{test.creation|date:'Y-m-d'}}{{test.author}}{{test.dev.name}}diff{{test.priority}}{{test.timecontrol}}{{test|shortStatBlock|linebreaksbr}}

Finished

+
+ {% if paging.prev != paging.next %} + <-- + {% for page in paging.pages %} + {% if page != "..." %} + {{page}} + {% else %} + {{page}} + {% endif %} + {% endfor %} + --> + {% endif %} +
{% endblock %} \ No newline at end of file