From 21f4ae3e25e4320654f9e92d16cfb93f937b3489 Mon Sep 17 00:00:00 2001 From: AndyGrant Date: Sun, 1 Mar 2020 23:56:27 -0500 Subject: [PATCH] Consolidate the views for Test Management --- OpenBench/models.py | 2 +- OpenBench/urls.py | 11 +- OpenBench/views.py | 197 ++++-------------- Templates/OpenBench/Blocks/testsummary.html | 2 +- Templates/OpenBench/eventLog.html | 2 +- Templates/OpenBench/machines.html | 2 +- .../OpenBench/{viewTest.html => test.html} | 18 +- 7 files changed, 57 insertions(+), 177 deletions(-) rename Templates/OpenBench/{viewTest.html => test.html} (86%) diff --git a/OpenBench/models.py b/OpenBench/models.py index 2920a6e..4734bdd 100644 --- a/OpenBench/models.py +++ b/OpenBench/models.py @@ -127,4 +127,4 @@ class LogEvent(Model): creation = DateTimeField(auto_now_add=True) def __str__(self): - return "{0} {1} {2}".format(self.author, str(self.test), self.data) \ No newline at end of file + return "{0} {1} {2}".format(self.author, str(self.test), self.data) diff --git a/OpenBench/urls.py b/OpenBench/urls.py index 5511e58..ea83fd4 100644 --- a/OpenBench/urls.py +++ b/OpenBench/urls.py @@ -43,14 +43,11 @@ urlpatterns = [ django.urls.path(r'eventLog/', OpenBench.views.eventLog), django.urls.path(r'eventLog//', OpenBench.views.eventLog), - # Links for viewing and managing tests + # Links for viewing and managing tests (maintain Legacy) + django.urls.path(r'test//', OpenBench.views.test), + django.urls.path(r'viewTest//', OpenBench.views.test), + django.urls.path(r'test//', OpenBench.views.test), django.urls.path(r'newTest/', OpenBench.views.newTest), - django.urls.path(r'viewTest//', OpenBench.views.viewTest), - django.urls.path(r'editTest//', OpenBench.views.editTest), - django.urls.path(r'approveTest//', OpenBench.views.approveTest), - django.urls.path(r'restartTest//', OpenBench.views.restartTest), - django.urls.path(r'stopTest//', OpenBench.views.stopTest), - django.urls.path(r'deleteTest//', OpenBench.views.deleteTest), # Links for the Client to update the Server django.urls.path(r'getFiles/', OpenBench.views.getFiles), diff --git a/OpenBench/views.py b/OpenBench/views.py index afd4f91..084d607 100644 --- a/OpenBench/views.py +++ b/OpenBench/views.py @@ -362,172 +362,55 @@ def newTest(request): except Exception as error: return index(request, error=str(error)) -def viewTest(request, id): +def test(request, id, action=None): - try: - # Build context dictionary for test template + # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # + # # + # GET : The User is either trying to view the status of the selected # + # test, or adjust the running state of the test in some way. When # + # viewing a test, collect the results and return an HTML template # + # using that data. Otherwise, look to adjust the running state of # + # the test. We throw out any invalid requests. Create a LogEvent # + # if the we attempt to modify the test's state in any way # + # # + # POST : The only valid POST request is for the action MODIFY. Requests # + # to modify contain an updated Priority and Throughput parameter # + # for the selected test. Bound the updated values, and log the # + # modification of the test with the creation of a new LogEvent # + # # + # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # + + if not Test.objects.filter(id=id): + return django.http.HttpResponseRedirect('/index/') + + if action not in ['APPROVE', 'RESTART', 'STOP', 'DELETE', 'MODIFY']: test = Test.objects.get(id=id) - results = Result.objects.all().filter(test=test) - results = results.order_by('machine_id') - data = {'test' : test, 'results' : results} - return render(request, 'viewTest.html', data) + results = Result.objects.filter(test=test).order_by('machine_id') + return render(request, 'test.html', {'test' : test, 'results': results}) - # Unable to find test - except Exception as error: - return index(request, error=str(error)) + user = request.user + test = Test.objects.get(id=id) + profile = Profile.objects.get(user=user) -@login_required(login_url='/login/') -def editTest(request, id): + if not profile.approver and test.author != profile.user.username: + return django.http.HttpResponseRedirect('/index/') - try: - # Only let approvers or the test author edit a test - test = Test.objects.get(id=id) - profile = Profile.objects.get(user=request.user) - if not profile.approver and test.author != profile.user.username: - raise Exception('Only Admins Or Test Owners Can Edit A Test') + if action == 'APPROVE' and test.author == user.username and not user.is_superuser: + return django.http.HttpResponseRedirect('/index/') - # Edit the provided test - test = Test.objects.get(id=id) + if action == 'APPROVE': test.approved = True; test.save() + if action == 'RESTART': test.finished = False; test.save() + if action == 'STOP' : test.finished = True; test.save() + if action == 'DELETE' : test.deleted = True; test.save() + + if action == 'MODIFY': test.priority = int(request.POST['priority']) - test.throughput = max(0, int(request.POST['throughput'])) + test.throughput = max(1, int(request.POST['throughput'])) test.save() - # Log changes to the test settings - LogEvent.objects.create( - data='Edited Test P={0} TP={1}'.format(test.priority, test.throughput), - author=request.user.username, - test=test) - - return HttpResponseRedirect('/index/') - - # Bad test id, permissions, or other - except Exception as error: - return index(request, error=str(error)) - -@login_required(login_url='/login/') -def approveTest(request, id): - - try: - # Throw out users without approver status - profile = Profile.objects.get(user=request.user) - if not profile.approver: - raise Exception('No Approver Permissions on Account') - - # Ensure cross verification is used, except for the site admin - test = Test.objects.get(id=id) - if test.author == profile.user.username and not profile.user.is_superuser: - raise Exception('Cross Approval is Required') - - # Approve the provided test - if test.approved: - return HttpResponseRedirect('/index/') - test.approved = True - test.save() - - # Log the test approval - LogEvent.objects.create( - data='Approved Test', - author=request.user.username, - test=test) - - return HttpResponseRedirect('/index/') - - # Bad test id, permissions, or other - except Exception as error: - return index(request, error=str(error)) - -@login_required(login_url='/login/') -def restartTest(request, id): - - try: - # Only let approvers or the author restart a test - test = Test.objects.get(id=id) - profile = Profile.objects.get(user=request.user) - if not profile.approver and test.author != profile.user.username: - raise Exception('Only Admins Or Test Owners Can Restart A Test') - - # Restart the provided test - test = Test.objects.get(id=id) - if not test.finished: - return HttpResponseRedirect('/index/') - test.finished = False - test.save() - - # Log the test stopping - LogEvent.objects.create( - data='Restarted Test', - author=request.user.username, - test=test) - - return HttpResponseRedirect('/index/') - - # Bad test id, permissions, or other - except Exception as error: - return index(request, error=str(error)) - -@login_required(login_url='/login/') -def stopTest(request, id): - - try: - # Only let approvers or the author stop a test - test = Test.objects.get(id=id) - profile = Profile.objects.get(user=request.user) - if not profile.approver and test.author != profile.user.username: - raise Exception('Only Admins Or Test Owners Can Stop A Test') - - # Stop the provided test - test = Test.objects.get(id=id) - if test.finished: - return HttpResponseRedirect('/index/') - test.finished = True - test.save() - - # Log the test stopping - LogEvent.objects.create( - data='Stopped Test', - author=request.user.username, - test=test) - - return HttpResponseRedirect('/index/') - - # Bad test id, permissions, or other - except Exception as error: - return index(request, error=str(error)) - -@login_required(login_url='/login/') -def deleteTest(request, id): - - try: - # Only let approvers or the author delete a test - test = Test.objects.get(id=id) - profile = Profile.objects.get(user=request.user) - if not profile.approver and test.author != profile.user.username: - raise Exception('Only Admins Or Test Owners Can Delete A Test') - - # Delete the provided test - test = Test.objects.get(id=id) - if test.deleted: - return HttpResponseRedirect('/index/') - test.deleted = True - test.save() - - # Reduce the test count for the test author - user = User.objects.get(username=test.author) - profile = Profile.objects.get(user=user) - profile.tests -= 1 - profile.save() - - # Log the test deltion - LogEvent.objects.create( - data='Deleted Test', - author=request.user.username, - test=test) - - return HttpResponseRedirect('/index/') - - # Bad test id, permissions, or other - except Exception as error: - return index(request, error=str(error)) + action += " P={0} TP={1}".format(test.priority, test.throughput) + LogEvent.objects.create(data=action, author=user.username, test=test) + return django.http.HttpResponseRedirect('/index/') @csrf_exempt @not_minified_response diff --git a/Templates/OpenBench/Blocks/testsummary.html b/Templates/OpenBench/Blocks/testsummary.html index 06a5580..aedf440 100644 --- a/Templates/OpenBench/Blocks/testsummary.html +++ b/Templates/OpenBench/Blocks/testsummary.html @@ -2,7 +2,7 @@ {{test.author|slice:":6"}} {{test.engine}} - {{test.dev.name}} + {{test.dev.name}} diff {{test.timecontrol}} {{test|shortStatBlock|linebreaksbr}} diff --git a/Templates/OpenBench/eventLog.html b/Templates/OpenBench/eventLog.html index bbc891f..89ca93a 100644 --- a/Templates/OpenBench/eventLog.html +++ b/Templates/OpenBench/eventLog.html @@ -16,7 +16,7 @@ {{event.creation|date:'Y-m-d H:i:s'}} {{event.author}} - {{event.test.dev.name}} + {{event.test.dev.name}} {{event.test.timecontrol}} {{event.data}} diff --git a/Templates/OpenBench/machines.html b/Templates/OpenBench/machines.html index 1fd2f53..bf7a0b2 100644 --- a/Templates/OpenBench/machines.html +++ b/Templates/OpenBench/machines.html @@ -22,7 +22,7 @@ {{machine.owner}} {{machine.osname}} {{machine.updated|date:'Y-m-d H:i'}} - {{machine.workload.dev.name}} + {{machine.workload.dev.name}} {{machine.threads}} {{machine.mnps|twoDigitPrecision}} diff --git a/Templates/OpenBench/viewTest.html b/Templates/OpenBench/test.html similarity index 86% rename from Templates/OpenBench/viewTest.html rename to Templates/OpenBench/test.html index e167871..a0aa80e 100644 --- a/Templates/OpenBench/viewTest.html +++ b/Templates/OpenBench/test.html @@ -35,34 +35,34 @@ {% if not test.approved and profile.approver and profile.user.username != test.author %} - Approve + Approve {% elif not test.approved and request.user.is_superuser %} - Approve + Approve {% else %} Approve {% endif %} {% if test.finished and profile.approver %} - Restart + Restart {% elif test.finished and profile.user.username == test.author %} - Restart + Restart {% elif test.finished %} Restart {% endif %} {% if not test.finished and profile.approver %} - Stop + Stop {% elif not test.finished and profile.user.username == test.author %} - Stop + Stop {% elif not test.finished %} Stop {% endif %} {% if profile.approver or profile.user.username == test.author %} - Delete + Delete {% else %} Delete {% endif %} @@ -77,7 +77,7 @@ -
+ {% csrf_token %} @@ -105,7 +105,7 @@ ID USER OS - Last Updated + UPDATED GAMES WON LOST