Allow proper upload, download, deletion, and setting default of Networks
This commit is contained in:
1 parent
6d7008c35d
commit
48cfff38c7
8 files changed
+176
-79
No files matched your search
@@ -26,3 +26,4 @@ django.contrib.admin.site.register(OpenBench.models.Machine)
|
||||
django.contrib.admin.site.register(OpenBench.models.Result)
|
||||
django.contrib.admin.site.register(OpenBench.models.Test)
|
||||
django.contrib.admin.site.register(OpenBench.models.LogEvent)
|
||||
django.contrib.admin.site.register(OpenBench.models.Network)
|
||||
+2
-2
@@ -135,7 +135,7 @@ class LogEvent(Model):
|
||||
|
||||
class Network(Model):
|
||||
|
||||
downloads = IntegerField(default=0)
|
||||
default = BooleanField(default=False)
|
||||
sha256 = CharField(max_length=8)
|
||||
name = CharField(max_length=64)
|
||||
engine = CharField(max_length=64)
|
||||
@@ -143,4 +143,4 @@ class Network(Model):
|
||||
created = DateTimeField(auto_now_add=True)
|
||||
|
||||
def __str__(self):
|
||||
return '{}-{1} ({2})'.format(self.engine, self.name, self.sha256)
|
||||
return '[{}] {} ({})'.format(self.engine, self.name, self.sha256)
|
||||
@@ -229,7 +229,15 @@ a:link, a:visited, a:hover, a:active {
|
||||
|
||||
#content a:not(.TitleLink) {
|
||||
font-weight: 100;
|
||||
color: #8bbad6;
|
||||
color: #8BBAD6;
|
||||
}
|
||||
|
||||
#content a:is(.redlink) {
|
||||
color: #FF4D4D;
|
||||
}
|
||||
|
||||
#content a:is(.unstarred) {
|
||||
color: #808080;
|
||||
}
|
||||
|
||||
#content h1, h2, h3 {
|
||||
|
||||
+4
-3
@@ -42,7 +42,6 @@ urlpatterns = [
|
||||
django.urls.path(r'events/', OpenBench.views.events),
|
||||
django.urls.path(r'events/<int:page>/', OpenBench.views.events),
|
||||
django.urls.path(r'machines/', OpenBench.views.machines),
|
||||
django.urls.path(r'networks/', OpenBench.views.networks),
|
||||
|
||||
# Links for viewing and managing tests (maintain Legacy)
|
||||
django.urls.path(r'test/<int:id>/', OpenBench.views.test),
|
||||
@@ -50,8 +49,10 @@ urlpatterns = [
|
||||
django.urls.path(r'test/<int:id>/<str:action>', OpenBench.views.test),
|
||||
django.urls.path(r'newTest/', OpenBench.views.newTest),
|
||||
|
||||
django.urls.path(r'newNetwork/', OpenBench.views.newNetwork),
|
||||
django.urls.path(r'networks/<str:sha256>', OpenBench.views.downloadNetwork),
|
||||
# Links for viewing and managing Networks
|
||||
django.urls.path(r'networks/', OpenBench.views.networks),
|
||||
django.urls.path(r'networks/<str:action>/', OpenBench.views.networks),
|
||||
django.urls.path(r'networks/<str:action>/<str:sha256>', OpenBench.views.networks),
|
||||
|
||||
# Links for the Client to work with the Server
|
||||
django.urls.path(r'clientGetFiles/', OpenBench.views.clientGetFiles),
|
||||
|
||||
+73
-41
@@ -18,7 +18,7 @@
|
||||
# #
|
||||
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
|
||||
|
||||
import os, hashlib, mimetypes
|
||||
import os, hashlib, mimetypes, datetime
|
||||
|
||||
import django.http
|
||||
import django.shortcuts
|
||||
@@ -331,17 +331,6 @@ def machines(request):
|
||||
data = {'machines' : OpenBench.utils.getRecentMachines()}
|
||||
return render(request, 'machines.html', data)
|
||||
|
||||
def networks(request):
|
||||
|
||||
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
|
||||
# #
|
||||
# GET : Return information about all of the Networks that have been #
|
||||
# uploaded to the Framework at any point in time #
|
||||
# #
|
||||
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
|
||||
|
||||
networks = Network.objects.all().order_by('-id')
|
||||
return render(request, 'networks.html', {'networks' : networks})
|
||||
|
||||
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
|
||||
# TEST MANAGEMENT VIEWS #
|
||||
@@ -426,7 +415,8 @@ def newTest(request):
|
||||
return django.http.HttpResponseRedirect('/index/')
|
||||
|
||||
if request.method == 'GET':
|
||||
return render(request, 'newTest.html')
|
||||
data = { 'networks' : Network.objects.all() }
|
||||
return render(request, 'newTest.html', data)
|
||||
|
||||
test, errors = OpenBench.utils.createNewTest(request)
|
||||
if errors != [] and errors != None:
|
||||
@@ -457,7 +447,43 @@ def newTest(request):
|
||||
|
||||
return django.http.HttpResponseRedirect('/index/')
|
||||
|
||||
def newNetwork(request):
|
||||
|
||||
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
|
||||
# NETWORK MANAGEMENT VIEWS #
|
||||
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
|
||||
|
||||
def networks(request, action=None, sha256=None):
|
||||
|
||||
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
|
||||
# #
|
||||
# GET : There are three possible GET lookups. [1] Fetch a view of all #
|
||||
# the Networks on the framework. [2] Fetch a template to upload a #
|
||||
# new Network to the framework. [3] Download the contents of a #
|
||||
# Network that is already saved on the framework. #
|
||||
# #
|
||||
# POST : There are four possible POST lookups. Download, Upload, Default #
|
||||
# and Delete. Downloads are open, but all others are gated by the #
|
||||
# credentials of the User. #
|
||||
# #
|
||||
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
|
||||
|
||||
if action == None:
|
||||
networks = Network.objects.all().order_by('-id')
|
||||
return render(request, 'networks.html', {'networks' : networks})
|
||||
|
||||
if action.upper() == 'DOWNLOAD':
|
||||
|
||||
if not Network.objects.filter(sha256=sha256):
|
||||
return index(request, error='No Network found with matching SHA256')
|
||||
|
||||
netfile = os.path.join(MEDIA_ROOT, sha256)
|
||||
fwrapper = FileWrapper(open(netfile, 'rb'), 8192)
|
||||
response = FileResponse(fwrapper, content_type='application/octet-stream')
|
||||
|
||||
response['Expires'] = (datetime.datetime.utcnow() + datetime.timedelta(days=7)).ctime()
|
||||
response['Content-Length'] = os.path.getsize(netfile)
|
||||
response['Content-Disposition'] = 'attachment; filename=' + sha256
|
||||
return response
|
||||
|
||||
if not request.user.is_authenticated:
|
||||
return django.http.HttpResponseRedirect('/login/')
|
||||
@@ -465,44 +491,50 @@ def newNetwork(request):
|
||||
if not Profile.objects.get(user=request.user).approver:
|
||||
return django.http.HttpResponseRedirect('/index/')
|
||||
|
||||
if request.method == 'GET':
|
||||
return render(request, 'uploadnet.html', {})
|
||||
if action.upper() == 'UPLOAD':
|
||||
|
||||
engine = request.POST['engine']
|
||||
netfile = request.FILES['netfile']
|
||||
sha256 = hashlib.sha256(netfile.file.read()).hexdigest()[:8].upper()
|
||||
if request.method == 'GET':
|
||||
return render(request, 'uploadnet.html', {})
|
||||
|
||||
if Network.objects.filter(sha256=sha256):
|
||||
return index(request, error='Network with that hash already exists')
|
||||
engine = request.POST['engine']
|
||||
netfile = request.FILES['netfile']
|
||||
sha256 = hashlib.sha256(netfile.file.read()).hexdigest()[:8].upper()
|
||||
|
||||
if engine not in OpenBench.utils.OPENBENCH_CONFIG['engines'].keys():
|
||||
return index(request, error='No Engine found with matching name')
|
||||
if Network.objects.filter(sha256=sha256):
|
||||
return index(request, error='Network with that hash already exists')
|
||||
|
||||
fsystem = FileSystemStorage()
|
||||
fname = fsystem.save(sha256, netfile)
|
||||
if engine not in OpenBench.utils.OPENBENCH_CONFIG['engines'].keys():
|
||||
return index(request, error='No Engine found with matching name')
|
||||
|
||||
Network.objects.create(
|
||||
sha256=sha256, name=request.POST['name'],
|
||||
engine=engine, author=request.user.username);
|
||||
FileSystemStorage().save(sha256, netfile)
|
||||
|
||||
return index(request)
|
||||
Network.objects.create(
|
||||
sha256=sha256, name=request.POST['name'],
|
||||
engine=engine, author=request.user.username);
|
||||
|
||||
def downloadNetwork(request, sha256):
|
||||
return index(request)
|
||||
|
||||
if not Network.objects.filter(sha256=sha256):
|
||||
return index(request, error='No Network found with matching SHA256')
|
||||
if action.upper() == 'DEFAULT':
|
||||
|
||||
netfile = os.path.join(MEDIA_ROOT, sha256)
|
||||
fwrapper = FileWrapper(open(netfile, 'rb'), 8192)
|
||||
response = FileResponse(fwrapper, content_type='application/octet-stream')
|
||||
if not Network.objects.filter(sha256=sha256):
|
||||
return index(request, error='No Network found with matching SHA256')
|
||||
|
||||
network = Network.objects.get(sha256=sha256)
|
||||
network.downloads = network.downloads + 1
|
||||
network.save()
|
||||
network = Network.objects.get(sha256=sha256)
|
||||
Network.objects.filter(engine=network.engine).update(default=False)
|
||||
network.default = True; network.save()
|
||||
|
||||
return django.http.HttpResponseRedirect('/networks/')
|
||||
|
||||
if action.upper() == 'DELETE':
|
||||
|
||||
if not Network.objects.filter(sha256=sha256):
|
||||
return index(request, error='No Network found with matching SHA256')
|
||||
|
||||
Network.objects.get(sha256=sha256).delete()
|
||||
FileSystemStorage().delete(sha256)
|
||||
|
||||
return django.http.HttpResponseRedirect('/networks/')
|
||||
|
||||
response['Content-Length'] = os.path.getsize(netfile)
|
||||
response['Content-Disposition'] = 'attachment; filename=' + sha256
|
||||
return response
|
||||
|
||||
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
|
||||
# CLIENT HOOK VIEWS #
|
||||
|
||||
Reference in new issue
Block a user