From b295842066ef4b9d14b311ad51943073a826e83a Mon Sep 17 00:00:00 2001 From: Andrew Grant Date: Sat, 2 May 2020 20:00:22 -0400 Subject: [PATCH] Update Machine to be tied to a User, not a string --- OpenBench/models.py | 4 ++-- OpenBench/utils.py | 11 +++++------ OpenBench/views.py | 8 ++++---- Templates/OpenBench/machines.html | 4 ++-- Templates/OpenBench/test.html | 2 +- 5 files changed, 14 insertions(+), 15 deletions(-) diff --git a/OpenBench/models.py b/OpenBench/models.py index 4734bdd..3b44d5d 100644 --- a/OpenBench/models.py +++ b/OpenBench/models.py @@ -50,7 +50,7 @@ class Profile(Model): class Machine(Model): - owner = CharField(max_length=64) + user = ForeignKey(User, PROTECT, related_name='owner') osname = CharField(max_length=128) threads = IntegerField(default=0) mnps = FloatField(default=0.00) @@ -58,7 +58,7 @@ class Machine(Model): workload = ForeignKey('Test', PROTECT, related_name='workload', default=1) def __str__(self): - return '{0}-{1} ({2})'.format(self.owner, self.osname, self.id) + return '{0}-{1} ({2})'.format(self.user.username, self.osname, self.id) class Result(Model): diff --git a/OpenBench/utils.py b/OpenBench/utils.py index 31bb52e..d5f5ce5 100644 --- a/OpenBench/utils.py +++ b/OpenBench/utils.py @@ -75,7 +75,7 @@ def getMachineStatus(username=None): machines = getRecentMachines() if username != None: - machines = machines.filter(owner=username) + machines = machines.filter(user__username=username) return "{0} Machines ".format(len(machines)) + \ "{0} Threads ".format(sum([f.threads for f in machines])) + \ @@ -251,7 +251,7 @@ def createNewTest(request): return test, None -def getMachine(machineid, owner, osname, threads): +def getMachine(machineid, user, osname, threads): try: # Create a new or fetch an existing Machine if machineid == 'None': machine = Machine() @@ -260,11 +260,11 @@ def getMachine(machineid, owner, osname, threads): # Verify that the fetched Machine matches if machineid != 'None': - if machine.owner != owner: return 'Bad Machine' + if machine.user != user: return 'Bad Machine' if machine.osname != osname: return 'Bad Machine' # Update the Machine's running status - machine.owner = owner ; machine.osname = osname + machine.user = user ; machine.osname = osname machine.threads = int(threads) ; machine.mnps = 0.00 return machine @@ -282,12 +282,11 @@ def getWorkload(user, request): # Extract worker information machineid = request.POST['machineid'] - owner = request.POST['username' ] osname = request.POST['osname' ] threads = request.POST['threads' ] # If we don't get a Machine back, there was an error - machine = getMachine(machineid, owner, osname, threads) + machine = getMachine(machineid, user, osname, threads) if type(machine) == str: return machine machine.save() diff --git a/OpenBench/views.py b/OpenBench/views.py index 439f2bd..f9d79f8 100644 --- a/OpenBench/views.py +++ b/OpenBench/views.py @@ -491,7 +491,7 @@ def clientWrongBench(request): # Verify the Machine belongs to the User machine = Machine.objects.get(id=int(request.POST['machineid'])) - if machine.owner != user.username: return HttpResponse('Bad Machine') + if machine.user != user: return HttpResponse('Bad Machine') # Find and stop the test with the bad bench test = Test.objects.get(id=int(request.POST['testid'])) @@ -533,7 +533,7 @@ def clientSubmitNPS(request): # Verify the Machine belongs to the User machine = Machine.objects.get(id=int(request.POST['machineid'])) - if machine.owner != user.username: return HttpResponse('Bad Machine') + if machine.user != user: return HttpResponse('Bad Machine') # Update the NPS and return 'None' to signal no errors machine.mnps = float(request.POST['nps']) / 1e6; machine.save() @@ -559,7 +559,7 @@ def clientSubmitError(request): # Verify the Machine belongs to the User machine = Machine.objects.get(id=int(request.POST['machineid'])) - if machine.owner != user.username: return HttpResponse('Bad Machine') + if machine.user != user: return HttpResponse('Bad Machine') # Log the Error into the Events table LogEvent.objects.create( @@ -581,7 +581,7 @@ def clientSubmitResults(request): # Verify the Machine belongs to the User machine = Machine.objects.get(id=int(request.POST['machineid'])) - if machine.owner != user.username: return HttpResponse('Bad Machine') + if machine.user != user: return HttpResponse('Bad Machine') # updateTest() will return 'None' or 'Stop' return HttpResponse(OpenBench.utils.updateTest(request, user)) \ No newline at end of file diff --git a/Templates/OpenBench/machines.html b/Templates/OpenBench/machines.html index 5b20e10..481015e 100644 --- a/Templates/OpenBench/machines.html +++ b/Templates/OpenBench/machines.html @@ -19,8 +19,8 @@ {% for machine in machines %} {{machine.id}} - {{machine.owner}} - {{machine.osname}} + {{machine.user.username}} + {{machine.osname|truncatewords:1|slice:":-4"}} {{machine.updated|date:'Y-m-d H:i'}} {{machine.workload.dev.name|prettyName}} {{machine.threads}} diff --git a/Templates/OpenBench/test.html b/Templates/OpenBench/test.html index 4e9c0d1..1b2728d 100644 --- a/Templates/OpenBench/test.html +++ b/Templates/OpenBench/test.html @@ -117,7 +117,7 @@ {% for result in results %} {{result.machine.id}} - {{result.machine.owner|slice:"6"}} + {{result.machine.user.username|slice:"6"}} {{result.machine.osname|truncatewords:1|slice:":-4"}} {{result.updated|date:'Y-m-d H:i'}} {{result.games}}