def buildGraph(request): error_msg = "" if request.method == "POST": form = BuildGraphForm(request.POST, request.FILES) # instantiating form if form.is_valid(): # Acquire proj names userDefProjectName = form.cleaned_data["UserDefprojectName"] site = form.cleaned_data["site"] subject = form.cleaned_data["subject"] session = form.cleaned_data["session"] scanId = form.cleaned_data["scanId"] # Private project error checking if (form.cleaned_data["Project_Type"] == "private"): if not request.user.is_authenticated(): error_msg = "You must be logged in to make/alter a private project! Please Login or make/alter a public project." """ # Untested TODO: Add join to ensure it a private project elif BuildGraphModel.objects.filter(owner=request.user, project_name=userDefProjectName, \ site=site, subject=subject, session=session, scanId=scanId).exists(): error_msg = "The scanID you requested to create already exists in this project path. Please change any of the form values." """ # TODO DM: Some unaccounted for scenarios here! if error_msg: return render_to_response( "buildgraph.html", {"buildGraphform": form, "error_msg": error_msg}, context_instance=RequestContext(request) ) # If a user is logged in associate the project with thier directory if form.cleaned_data["Project_Type"] == "private": userDefProjectName = os.path.join(request.user.username, userDefProjectName) else: userDefProjectName = os.path.join("public", userDefProjectName) # Adapt project name if necesary on disk userDefProjectName = adaptProjNameIfReq(os.path.join(settings.MEDIA_ROOT, userDefProjectName)) # Fully qualify AND handle identical projects request.session["usrDefProjDir"] = os.path.join(userDefProjectName, site, subject, session, scanId) request.session["scanId"] = scanId """ Define data directory paths """ request.session["derivatives"], request.session["graphs"],\ request.session["graphInvariants"] = defDataDirs(request.session["usrDefProjDir"]) # Create a model object to save data to DB grModObj = BuildGraphModel(project_name = form.cleaned_data["UserDefprojectName"]) grModObj.location = request.session["usrDefProjDir"] # Where the particular scan location is grModObj.site = form.cleaned_data["site"]# set the site grModObj.subject = form.cleaned_data["subject"]# set the subject grModObj.session = form.cleaned_data["session"]# set the session grModObj.scanId = form.cleaned_data["scanId"]# set the scanId if request.user.is_authenticated(): grModObj.owner = request.user # Who created the project request.session["invariants"] = form.cleaned_data["Select_Invariants_you_want_computed"] request.session["graphsize"] = form.cleaned_data["Select_graph_size"] request.session["email"] = form.cleaned_data["Email"] if request.session["graphsize"] == "big" and not request.session["email"]: return render_to_response( "buildgraph.html", {"buildGraphform": form, "error_msg": "Email address must be provided when processing big graphs due to http timeout's possibly occuring."}, context_instance=RequestContext(request) ) """ Acquire fileNames """ fiber_fn = form.cleaned_data["fiber_file"].name # get the name of the file input by user roi_raw_fn = form.cleaned_data["roi_raw_file"].name roi_xml_fn = form.cleaned_data["roi_xml_file"].name print "Uploading files..." """ Save files in appropriate location """ saveFileToDisk(form.cleaned_data["fiber_file"], os.path.join(request.session["derivatives"], fiber_fn)) saveFileToDisk(form.cleaned_data["roi_raw_file"], os.path.join(request.session["derivatives"], roi_raw_fn)) saveFileToDisk(form.cleaned_data["roi_xml_file"], os.path.join(request.session["derivatives"], roi_xml_fn)) grModObj.save() # Save project data to DB after file upload # add entry to owned project if request.user.is_authenticated(): ownedProjModObj = OwnedProjects(project_name=grModObj.project_name, \ owner=grModObj.owner, is_private=form.cleaned_data["Project_Type"] == "private") ownedProjModObj.save() print "\nSaving all files complete..." """ Make appropriate dirs if they dont already exist """ create_dir_struct([request.session["derivatives"],\ request.session["graphs"], request.session["graphInvariants"]]) if request.session["graphsize"] == "big": # Launch thread for big graphs & email user #processInputData(request) sendJobBeginEmail(request.session["email"], request.session["invariants"]) thr = threading.Thread(target=processInputData, args=(request,)) thr.start() request.session["success_msg"] =\ """ Your job was successfully launched. You should receive an email when your job begins and another one when it completes. The process may take several if you selected to compute all invariants """ return HttpResponseRedirect(get_script_prefix()+"success") # Redirect to Processing page return HttpResponseRedirect(get_script_prefix()+"processinput") else: form = BuildGraphForm() # An empty, unbound form # Render the form return render_to_response( "buildgraph.html", {"buildGraphform": form}, context_instance=RequestContext(request) # Some failure to input data & returns a key signaling what is requested )
def upload(request, webargs=None): """ Programmatic interface for uploading data @param request: the request object @param webargs: POST data with userDefProjectName, site, subject, session, scanId, graphsize, [list of invariants to compute] info """ if (webargs and request.method == 'POST'): # Check for malformatted input webargs = webargs[1:] if webargs.startswith('/') else webargs webargs = webargs[:-1] if webargs.endswith('/') else webargs if len(webargs.split('/')) == 7: [userDefProjectName, site, subject, session, scanId, graphsize, request.session['invariants']] = webargs.split('/') request.session['invariants'] = request.session['invariants'].split(',') elif len(webargs.split('/')) == 6: [userDefProjectName, site, subject, session, scanId, graphsize] = webargs.split('/') else: # Some sort of error return HttpResponseBadRequest ("Malformatted programmatic request. Check format of url and data requests") userDefProjectDir = adaptProjNameIfReq(os.path.join(settings.MEDIA_ROOT, 'public', userDefProjectName, site, subject, session, scanId)) ''' Define data directory paths ''' derivatives, graphs, request.session['graphInvariants'] = defDataDirs(userDefProjectDir) ''' Make appropriate dirs if they dont already exist ''' create_dir_struct([derivatives, graphs,request.session["graphInvariants"]]) print 'Directory structure created...' uploadFiles = writeBodyToDisk(request.body, derivatives) # Check which file is which roi_xml_fn, fiber_fn, roi_raw_fn = checkFileExtGengraph(uploadFiles) # Check & sort files ''' Data Processing ''' if graphsize: request.session['Gfn']= call_gengraph(fiber_fn, roi_xml_fn, roi_raw_fn, \ graphs, request.session['graphInvariants'],\ graphsize, run=True) else: return HttpResponseBadRequest ("Missing graph size. You must specify big or small") # Run invariants if request.session.has_key('invariants'): print "Computing invariants ..." invariant_fns = runInvariants(request.session['invariants'],\ request.session['Gfn'], request.session['graphInvariants']) dwnldLoc = request.META['wsgi.url_scheme'] + '://' + \ request.META['HTTP_HOST'] + userDefProjectDir.replace(' ','%20') return HttpResponse ( "Files available for download at " + dwnldLoc) # change to render of a page with a link to data result elif(not webargs): return HttpResponseBadRequest ("Expected web arguments to direct project correctly") else: return HttpResponseBadRequest ("Expected POST data, but none given")