def convert(request, webargs=None): ''' Form ''' # request.session.clear() # NEW if (request.method == 'POST' and not webargs): form = ConvertForm(request.POST, request.FILES) # instantiating form if form.is_valid(): baseDir = os.path.join( settings.MEDIA_ROOT, 'tmp', strftime('formUpload%a%d%b%Y_%H.%M.%S/', localtime())) saveDir = os.path.join( baseDir, 'upload') # Save location of original uploads convertFileSaveLoc = os.path.join( baseDir, 'converted') # Save location of converted data if not (os.path.exists(convertFileSaveLoc)): os.makedirs(convertFileSaveLoc) savedFile = os.path.join(saveDir, request.FILES['fileObj'].name) saveFileToDisk(request.FILES['fileObj'], savedFile) # If zip is uploaded if os.path.splitext( request.FILES['fileObj'].name)[1].strip() == '.zip': uploadedFiles = zipper.unzip(savedFile, saveDir) # Delete zip os.remove(savedFile) else: uploadedFiles = [savedFile] isCorrectFileFormat, isCorrectFileType = convertFiles(uploadedFiles, form.cleaned_data['Select_file_type'], \ form.cleaned_data['Select_conversion_format'], convertFileSaveLoc) if not (isCorrectFileFormat): err_msg = "You did not upload any files with the correct extension for conversion!" return render_to_response( 'convertupload.html', { 'convertForm': form, 'err_msg': err_msg }, context_instance=RequestContext(request)) baseurl = request.META['HTTP_HOST'] host = request.META['wsgi.url_scheme'] rooturl = host + '://' + baseurl # Originally was: 'http://mrbrain.cs.jhu.edu' # Done for http & https dwnldLoc = rooturl + convertFileSaveLoc.replace( ' ', '%20') # TODO: Verify this works return HttpResponseRedirect(dwnldLoc) # Programmtic API elif (request.method == 'POST' and webargs): # webargs is {fileType}/{toFormat} fileType = webargs.split('/')[0] # E.g 'cc', 'deg', 'triangle' toFormat = (webargs.split('/')[1]).split( ',') # E.g 'mat', 'npy' or 'mat,csv' toFormat = list(set(toFormat)) # Eliminate duplicates if any exist # Make sure filetype is valid before doing any work if (fileType not in settings.VALID_FILE_TYPES.keys() and fileType not in settings.VALID_FILE_TYPES.values()): return HttpResponse( 'Invalid conversion type. Make sure toFormat is a valid type') # In case to format does not start with a '.'. Add if not for idx in range(len(toFormat)): if not toFormat[idx].startswith('.'): toFormat[idx] = '.' + toFormat[idx] baseDir = os.path.join( settings.MEDIA_ROOT, 'tmp', strftime('progUpload%a%d%b%Y_%H.%M.%S/', localtime())) saveDir = os.path.join(baseDir, 'upload') # Save location of original uploads convertFileSaveLoc = os.path.join( baseDir, 'converted') # Save location of converted data if not os.path.exists(saveDir): os.makedirs(saveDir) if not os.path.exists(convertFileSaveLoc): os.makedirs(convertFileSaveLoc) uploadedFiles = writeBodyToDisk(request.body, saveDir) isCorrectFileFormat, isCorrectFileType = convertFiles( uploadedFiles, fileType, toFormat, convertFileSaveLoc) if not (isCorrectFileType): # request.session.clear() return HttpResponse("[ERROR]: You did not enter a valid FileType.") if not (isCorrectFileFormat): # request.session.clear() return HttpResponse( "[ERROR]: You do not have any files with the correct extension for conversion" ) dwnldLoc = "http://mrbrain.cs.jhu.edu" + convertFileSaveLoc.replace( ' ', '%20') # request.session.clear() return HttpResponse( "Converted files available for download at " + dwnldLoc + " . The directory " + "may be empty if you try to convert to the same format the file is already in." ) # change to render of a page with a link to data result else: form = ConvertForm() # An empty, unbound form # Render the form return render_to_response('convertupload.html', {'convertForm': form}, context_instance=RequestContext(request))
def graphLoadInv(request, webargs=None): ''' Form ''' from glob import glob # Move # request.session.clear() # NEW if request.method == 'POST' and not webargs: form = GraphUploadForm(request.POST, request.FILES) # instantiating form if form.is_valid(): data = form.files['fileObj'] # get data request.session['invariants'] = form.cleaned_data[ 'Select_Invariants_you_want_computed'] request.session['graphsize'] = form.cleaned_data[ 'Select_graph_size'] dataDir = os.path.join( settings.MEDIA_ROOT, 'tmp', strftime("projectStamp%a%d%b%Y_%H.%M.%S/", localtime())) request.session['graphInvariants'] = os.path.join( dataDir, 'graphInvariants') makeDirIfNone([dataDir]) # We got a zip if os.path.splitext(data.name)[1] == '.zip': writeBodyToDisk(data.read(), dataDir) # Get all graphs in the directory graphs = glob(os.path.join(dataDir, '*_fiber.mat')) graphs.extend(glob(os.path.join(dataDir, '*_bggr.mat'))) graphs.extend(glob(os.path.join(dataDir, '*_smgr.mat'))) else: # View only accepts .mat & zip as regulated by template graphs = [os.path.join(dataDir, data.name)] saveFileToDisk(data, graphs[0]) for graph_fn in graphs: if request.session['graphsize'] == 'big': request.session['bgGrfn'] = graph_fn lcc_fn = graph_fn.split('_')[0] + '_concomp.mat' elif request.session['graphsize'] == 'small': graph_fn = request.session['smGrfn'] = graph_fn lcc_fn = None runInvariants(request.session['invariants'], graph_fn, request.session['graphInvariants'], lcc_fn, request.session['graphsize']) print 'Invariants for annoymous project %s complete...' % graph_fn return HttpResponseRedirect( "http://mrbrain.cs.jhu.edu" + dataDir.replace( ' ', '%20')) # All spaces are replaced with %20 for urls elif request.method == 'POST' and webargs: if (re.match(re.compile('(b|big)', re.IGNORECASE), webargs.split('/')[0])): request.session['graphsize'] = 'big' elif (re.match(re.compile('(s|small)', re.IGNORECASE), webargs.split('/')[0])): request.session['graphsize'] = 'small' else: return django.http.HttpResponseBadRequest( "The graph size is required as a web argument") dataDir = os.path.join( settings.MEDIA_ROOT, 'tmp', strftime("projectStamp%a%d%b%Y_%H.%M.%S/", localtime())) makeDirIfNone([dataDir]) uploadedZip = writeBodyToDisk(request.body, dataDir)[0] zipper.unzip(uploadedZip, dataDir) # Unzip the zip os.remove(uploadedZip) # Delete the zip) request.session['invariants'] = webargs.split('/')[1].split(',') graphs = glob(os.path.join(dataDir, '*_fiber.mat')) graphs.extend(glob(os.path.join(dataDir, '*_bggr.mat'))) graphs.extend(glob(os.path.join(dataDir, '*_smgr.mat'))) request.session['graphInvariants'] = os.path.join( dataDir, 'graphInvariants') for graph_fn in graphs: if request.session['graphsize'] == 'big': request.session['bgGrfn'] = graph_fn lcc_fn = graph_fn.split('_')[0] + '_concomp.mat' elif request.session['graphsize'] == 'small': request.session['smGrfn'] = graph_fn lcc_fn = None runInvariants(request.session['invariants'], graph_fn, request.session['graphInvariants'], lcc_fn, request.session['graphsize']) print 'Invariants for annoymous project %s complete...' % graph_fn # request.session.clear() dwnldLoc = "http://mrbrain.cs.jhu.edu" + dataDir.replace(' ', '%20') return HttpResponse("View Data at: " + dwnldLoc) else: form = GraphUploadForm() # An empty, unbound form # Render the form return render_to_response( 'graphupload.html', {'graphUploadForm': form}, context_instance=RequestContext( request ) # Some failure to input data & returns a key signaling what is requested )
def graphLoadInv(request, webargs=None): ''' Form ''' from glob import glob # Move # request.session.clear() # NEW if request.method == 'POST' and not webargs: form = GraphUploadForm(request.POST, request.FILES) # instantiating form if form.is_valid(): data = form.files['fileObj'] # get data request.session['invariants'] = form.cleaned_data['Select_Invariants_you_want_computed'] request.session['graphsize'] = form.cleaned_data['Select_graph_size'] dataDir = os.path.join(settings.MEDIA_ROOT, 'tmp', strftime("projectStamp%a%d%b%Y_%H.%M.%S/", localtime())) request.session['graphInvariants'] = os.path.join(dataDir, 'graphInvariants') makeDirIfNone([dataDir]) # We got a zip if os.path.splitext(data.name)[1] == '.zip': writeBodyToDisk(data.read(), dataDir) # Get all graphs in the directory graphs = glob(os.path.join(dataDir,'*_fiber.mat')) graphs.extend(glob(os.path.join(dataDir,'*_bggr.mat'))) graphs.extend(glob(os.path.join(dataDir,'*_smgr.mat'))) else: # View only accepts .mat & zip as regulated by template graphs = [os.path.join(dataDir, data.name)] saveFileToDisk(data, graphs[0]) for graph_fn in graphs: if request.session['graphsize'] == 'big': request.session['bgGrfn'] = graph_fn lcc_fn = graph_fn.split('_')[0] + '_concomp.mat' elif request.session['graphsize'] == 'small': graph_fn = request.session['smGrfn'] = graph_fn lcc_fn = None runInvariants(request.session['invariants'], graph_fn, request.session['graphInvariants'], lcc_fn, request.session['graphsize']) print 'Invariants for annoymous project %s complete...' % graph_fn return HttpResponseRedirect("http://mrbrain.cs.jhu.edu"+ dataDir.replace(' ','%20')) # All spaces are replaced with %20 for urls elif request.method == 'POST' and webargs: if (re.match(re.compile('(b|big)', re.IGNORECASE), webargs.split('/')[0])): request.session['graphsize'] = 'big' elif (re.match(re.compile('(s|small)', re.IGNORECASE), webargs.split('/')[0])): request.session['graphsize'] = 'small' else: return django.http.HttpResponseBadRequest("The graph size is required as a web argument") dataDir = os.path.join(settings.MEDIA_ROOT, 'tmp', strftime("projectStamp%a%d%b%Y_%H.%M.%S/", localtime())) makeDirIfNone([dataDir]) uploadedZip = writeBodyToDisk(request.body, dataDir)[0] zipper.unzip(uploadedZip, dataDir) # Unzip the zip os.remove(uploadedZip) # Delete the zip) request.session['invariants'] = webargs.split('/')[1].split(',') graphs = glob(os.path.join(dataDir,'*_fiber.mat')) graphs.extend(glob(os.path.join(dataDir,'*_bggr.mat'))) graphs.extend(glob(os.path.join(dataDir,'*_smgr.mat'))) request.session['graphInvariants'] = os.path.join(dataDir, 'graphInvariants') for graph_fn in graphs: if request.session['graphsize'] == 'big': request.session['bgGrfn'] = graph_fn lcc_fn = graph_fn.split('_')[0] + '_concomp.mat' elif request.session['graphsize'] == 'small': request.session['smGrfn'] = graph_fn lcc_fn = None runInvariants(request.session['invariants'], graph_fn, request.session['graphInvariants'], lcc_fn, request.session['graphsize']) print 'Invariants for annoymous project %s complete...' % graph_fn # request.session.clear() dwnldLoc = "http://mrbrain.cs.jhu.edu"+ dataDir.replace(' ','%20') return HttpResponse("View Data at: " + dwnldLoc) else: form = GraphUploadForm() # An empty, unbound form # Render the form return render_to_response( 'graphupload.html', {'graphUploadForm': form}, context_instance=RequestContext(request) # Some failure to input data & returns a key signaling what is requested )
def convert(request, webargs=None): ''' Form ''' # request.session.clear() # NEW if (request.method == 'POST' and not webargs): form = ConvertForm(request.POST, request.FILES) # instantiating form if form.is_valid(): baseDir = os.path.join(settings.MEDIA_ROOT, 'tmp', strftime('formUpload%a%d%b%Y_%H.%M.%S/', localtime())) saveDir = os.path.join(baseDir,'upload') # Save location of original uploads convertFileSaveLoc = os.path.join(baseDir,'converted') # Save location of converted data if not (os.path.exists(convertFileSaveLoc)): os.makedirs(convertFileSaveLoc) savedFile = os.path.join(saveDir, request.FILES['fileObj'].name) saveFileToDisk(request.FILES['fileObj'], savedFile) # If zip is uploaded if os.path.splitext(request.FILES['fileObj'].name)[1].strip() == '.zip': uploadedFiles = zipper.unzip(savedFile, saveDir) # Delete zip os.remove(savedFile) else: uploadedFiles = [savedFile] isCorrectFileFormat, isCorrectFileType = convertFiles(uploadedFiles, form.cleaned_data['Select_file_type'], \ form.cleaned_data['Select_conversion_format'], convertFileSaveLoc) if not (isCorrectFileFormat): err_msg = "You did not upload any files with the correct extension for conversion!" return render_to_response( 'convertupload.html', {'convertForm': form, 'err_msg': err_msg}, context_instance=RequestContext(request)) baseurl = request.META['HTTP_HOST'] host = request.META['wsgi.url_scheme'] rooturl = host + '://' + baseurl # Originally was: 'http://mrbrain.cs.jhu.edu' # Done for http & https dwnldLoc = rooturl + convertFileSaveLoc.replace(' ','%20') # TODO: Verify this works return HttpResponseRedirect(dwnldLoc) # Programmtic API elif(request.method == 'POST' and webargs): # webargs is {fileType}/{toFormat} fileType = webargs.split('/')[0] # E.g 'cc', 'deg', 'triangle' toFormat = (webargs.split('/')[1]).split(',') # E.g 'mat', 'npy' or 'mat,csv' toFormat = list(set(toFormat)) # Eliminate duplicates if any exist # Make sure filetype is valid before doing any work if (fileType not in settings.VALID_FILE_TYPES.keys() and fileType not in settings.VALID_FILE_TYPES.values()): return HttpResponse('Invalid conversion type. Make sure toFormat is a valid type') # In case to format does not start with a '.'. Add if not for idx in range (len(toFormat)): if not toFormat[idx].startswith('.'): toFormat[idx] = '.'+toFormat[idx] baseDir = os.path.join(settings.MEDIA_ROOT, 'tmp', strftime('progUpload%a%d%b%Y_%H.%M.%S/', localtime())) saveDir = os.path.join(baseDir,'upload') # Save location of original uploads convertFileSaveLoc = os.path.join(baseDir,'converted') # Save location of converted data if not os.path.exists(saveDir): os.makedirs(saveDir) if not os.path.exists(convertFileSaveLoc): os.makedirs(convertFileSaveLoc) uploadedFiles = writeBodyToDisk(request.body, saveDir) isCorrectFileFormat, isCorrectFileType = convertFiles(uploadedFiles, fileType, toFormat, convertFileSaveLoc) if not (isCorrectFileType): # request.session.clear() return HttpResponse("[ERROR]: You did not enter a valid FileType.") if not (isCorrectFileFormat): # request.session.clear() return HttpResponse("[ERROR]: You do not have any files with the correct extension for conversion") dwnldLoc = "http://mrbrain.cs.jhu.edu"+ convertFileSaveLoc.replace(' ','%20') # request.session.clear() return HttpResponse ( "Converted files available for download at " + dwnldLoc + " . The directory " + "may be empty if you try to convert to the same format the file is already in.") # change to render of a page with a link to data result else: form = ConvertForm() # An empty, unbound form # Render the form return render_to_response( 'convertupload.html', {'convertForm': form}, context_instance=RequestContext(request))
def graphLoadInv(request, webargs=None): ''' Form ''' from glob import glob # Move if request.method == 'POST' and not webargs: form = GraphUploadForm(request.POST, request.FILES) # instantiating form if form.is_valid(): request.session['graphsize'] = form.cleaned_data['Select_graph_size'] request.session['graphsize'] = 'small' if not request.session['graphsize'] else request.session['graphsize'] data = form.files['fileObj'] # get data request.session['invariants'] = form.cleaned_data['Select_Invariants_you_want_computed'] dataDir = os.path.join(settings.MEDIA_ROOT, 'tmp', strftime("projectStamp%a%d%b%Y_%H.%M.%S/", localtime())) request.session['graphInvariants'] = os.path.join(dataDir, 'graphInvariants') makeDirIfNone([dataDir]) # We got a zip if os.path.splitext(data.name)[1] == '.zip': writeBodyToDisk(data.read(), dataDir) # Get all graphs in the directory graphs = glob(os.path.join(dataDir,'*_fiber.mat')) graphs.extend(glob(os.path.join(dataDir,'*_bggr.mat'))) graphs.extend(glob(os.path.join(dataDir,'*_smgr.mat'))) else: # View only accepts .mat & zip as regulated by template graphs = [os.path.join(dataDir, data.name)] saveFileToDisk(data, graphs[0]) request.session['uploaded_graphs'] = graphs request.session['invConvertToFormats'] = form.cleaned_data['Convert_result'] request.session['dataDir'] = dataDir if request.session['graphsize'] == 'big': # Launch thread for big graphs & email user request.session['email'] = form.cleaned_data['Email'] sendJobBeginEmail(request.session['email'], request.session['invariants'], genGraph=False) thr = threading.Thread(target=asyncInvCompute, args=(request,)) thr.start() #asyncInvCompute(request) request.session['success_msg'] = "Your job was successfully launched. You should receive an email when your " request.session['success_msg'] += "job begins and another one when it completes. The process may take ~3hrs if you selected to compute all invariants" return HttpResponseRedirect(get_script_prefix()+'success') else: for graph_fn in graphs: graph_fn = request.session['smGrfn'] = graph_fn lcc_fn = None invariant_fns = runInvariants(request.session['invariants'], graph_fn, request.session['graphInvariants'], lcc_fn, request.session['graphsize']) print 'Invariants for annoymous project %s complete...' % graph_fn invConvertToFormats = form.cleaned_data['Convert_result'] # TODO: Make function for this. Duplicate of buildgraph code for fileFormat in invConvertToFormats: # Conversion of all files for inv in invariant_fns.keys(): if isinstance(invariant_fns[inv], list): # Case of eigs for fn in invariant_fns[inv]: convertTo.convertAndSave(fn, fileFormat, os.path.dirname(fn), inv) else: # case of all other invariants convertTo.convertAndSave(invariant_fns[inv], fileFormat, \ os.path.dirname(invariant_fns[inv]), inv) return HttpResponseRedirect("http://mrbrain.cs.jhu.edu"+ dataDir.replace(' ','%20')) # All spaces are replaced with %20 for urls elif request.method == 'POST' and webargs: if (re.match(re.compile('(b|big)', re.IGNORECASE), webargs.split('/')[0])): request.session['graphsize'] = 'big' elif (re.match(re.compile('(s|small)', re.IGNORECASE), webargs.split('/')[0])): request.session['graphsize'] = 'small' else: return django.http.HttpResponseBadRequest("The graph size is required as a web argument") dataDir = os.path.join(settings.MEDIA_ROOT, 'tmp', strftime("projectStamp%a%d%b%Y_%H.%M.%S/", localtime())) makeDirIfNone([dataDir]) uploadedZip = writeBodyToDisk(request.body, dataDir)[0] zipper.unzip(uploadedZip, dataDir) # Unzip the zip os.remove(uploadedZip) # Delete the zip) request.session['invariants'] = webargs.split('/')[1].split(',') graphs = glob(os.path.join(dataDir,'*_fiber.mat')) graphs.extend(glob(os.path.join(dataDir,'*_bggr.mat'))) graphs.extend(glob(os.path.join(dataDir,'*_smgr.mat'))) request.session['graphInvariants'] = os.path.join(dataDir, 'graphInvariants') for graph_fn in graphs: if request.session['graphsize'] == 'big': request.session['bgGrfn'] = graph_fn lcc_fn = graph_fn.split('_')[0] + '_concomp.mat' elif request.session['graphsize'] == 'small': request.session['smGrfn'] = graph_fn lcc_fn = None runInvariants(request.session['invariants'], graph_fn, request.session['graphInvariants'], lcc_fn, request.session['graphsize']) print 'Invariants for annoymous project %s complete...' % graph_fn # request.session.clear() dwnldLoc = "http://mrbrain.cs.jhu.edu"+ dataDir.replace(' ','%20') return HttpResponse("View Data at: " + dwnldLoc) else: form = GraphUploadForm() # An empty, unbound form # Render the form return render_to_response( 'graphupload.html', {'graphUploadForm': form}, context_instance=RequestContext(request) # Some failure to input data & returns a key signaling what is requested )