def galleryProcess(request): ''' Process a request to save a rendered image to the gallery. Keyword arguments: request -- the request object. Return: Redirect user to the gallery is the submission was sucessful or display an approriate error message. ''' if request.method == 'POST': CAPTCHA_PUBLIC_KEY = settings.AF_RECAPTCHA_PUBLIC_KEY form = gallerySubmitForm(request.POST) if form.is_valid(): # Check if CAPTCHA response was valid. response = captcha.submit( request.POST.get('recaptcha_challenge_field'), request.POST.get('recaptcha_response_field'), settings.AF_RECAPTCHA_PRIVATE_KEY, request.META['REMOTE_ADDR'], ) if not response.is_valid: captchaWrong = True submitError = True requestID = request.POST['image'] return render_to_response( 'render.html', locals(), context_instance=RequestContext(request)) # Copy the temporary rendered image into the gallery - so that # it doesn't get pruned (_cleanFiles). staticPath = os.path.join(settings.PROJECT_PATH, "app/static/") renderedFile = staticPath + "rendered/" + request.session[ 'requestID'] + ".gif" outputFile = staticPath + "gallery/" + request.session[ 'requestID'] + ".gif" copyfile(renderedFile, outputFile) # Generate and save a thumbnail of this image. _generateThumbnail(staticPath, request.session['requestID']) # Insert the image details into the gallery database. form.save() return redirect('/gallery?s=1') else: # Validation error. requestID = request.POST['image'] submitError = True return render_to_response('render.html', locals(), context_instance=RequestContext(request)) else: return redirect('/process')
def galleryProcess(request): ''' Process a request to save a rendered image to the gallery. Keyword arguments: request -- the request object. Return: Redirect user to the gallery is the submission was sucessful or display an approriate error message. ''' if request.method == 'POST': CAPTCHA_PUBLIC_KEY = settings.AF_RECAPTCHA_PUBLIC_KEY form = gallerySubmitForm(request.POST) if form.is_valid(): # Check if CAPTCHA response was valid. response = captcha.submit( request.POST.get('recaptcha_challenge_field'), request.POST.get('recaptcha_response_field'), settings.AF_RECAPTCHA_PRIVATE_KEY, request.META['REMOTE_ADDR'],) if not response.is_valid: captchaWrong = True submitError = True requestID = request.POST['image'] return render_to_response('render.html', locals(), context_instance=RequestContext(request)) # Copy the temporary rendered image into the gallery - so that # it doesn't get pruned (_cleanFiles). staticPath = os.path.join(settings.PROJECT_PATH, "app/static/") renderedFile = staticPath + "rendered/" + request.session['requestID'] + ".gif" outputFile = staticPath + "gallery/" + request.session['requestID'] + ".gif" copyfile(renderedFile, outputFile) # Generate and save a thumbnail of this image. _generateThumbnail(staticPath, request.session['requestID']) # Insert the image details into the gallery database. form.save() return redirect('/gallery?s=1') else: # Validation error. requestID = request.POST['image'] submitError = True return render_to_response('render.html', locals(), context_instance=RequestContext(request)) else: return redirect('/process')
def _render(request, parsedData, loggly=False, logglyData=None): ''' Processes a request and attempts to render a graph. Keyword arguments: request -- the request object. parsedData -- boolean, True if the incoming data is parsed data (log/Loggly) loggly -- boolean, True if the incoming data is from Loggly. logglyData -- A string data returned from a Loggly search. Return: A rendered graph from the request if successful or an error message on the view. ''' # Generate a session-key if one isn't already active. if hasattr(request, 'session') and hasattr(request.session, \ 'session_key') and \ getattr(request.session, 'session_key') is None: request.session.create() # Generate a unique request ID hash from the session key for this particular # request. requestID = md5(request.session.session_key + str(datetime.now())).hexdigest() # Clean up old user resource files which are older than four hours. _cleanFiles() # If this rendering request's data has been fetched from Loggly, then # retrieve the rendering settings/configurations from the session (where it # has been previously saved). if loggly: POSTdata = request.session["logglyForm"] else: POSTdata = request.POST # Flag to check and alert of any errors encountered while attempting to # render. retVal = 0 if parsedData: if loggly: # If data from Loggly (after a search) is empty, alert the user. if not logglyData: emptyData = True return render_to_response( 'render.html', locals(), context_instance=RequestContext(request)) # Parse the data from Loggly's search into a CSV file. retVal = _parseToCsv(logglyData, requestID, POSTdata, True) else: # Parse the log file into a CSV file with the expression given. retVal = _parseToCsv(request.FILES['dataFile'], requestID, POSTdata) # If the user submitted a custom regex expression and indicated to # save it; insert the data into the database and alert an administrator # through email. if not retVal and POSTdata[ 'regExType'] == '1' and "saveRegEx" in POSTdata: expression = Expressions(name=POSTdata['saveRegExName'], \ description=POSTdata['saveRegExDescription'], \ regex=POSTdata['regEx']) expression.save() message = "Hello, a new expression has been submitted.\n" message += "Exp name: " + POSTdata["saveRegExName"] \ + "\n\nDescription: " + POSTdata["saveRegExDescription"] \ + "\n\nExpression: " + POSTdata["regEx"] \ + "\n\n" from_email = settings.AF_FROM_EMAIL try: send_mail("Expression submit @AfterGlow", message, from_email, settings.AF_TO_EMAILS) except BadHeaderError: return HttpResponse('Invalid header found. Please try again.') else: # A CSV file has been uploaded, write it as-is to the server. _writeDataFile(request.FILES['dataFile'], requestID) # If retVal has been changed from 0 to any other value from its point of # declaration we have an error. Alert the user. if retVal: return render_to_response('render.html', locals(), context_instance=RequestContext(request)) else: _writeConfigFile(POSTdata['propertyConfig'], requestID) #Build up parameters to be sent to the shell script. param = _buildParameters(POSTdata) if parsedData: dataFile = "user_logs_parsed/" + requestID + ".log" else: dataFile = "user_data/" + requestID + ".csv" propertyFile = "user_config/" + requestID + ".property" outputFile = "afterglow_cloud/app/static/rendered/" + requestID + ".gif" afPath = "../afterglow/src/afterglow.pl" #Try rendering a graph, store the return code from the shell script. status = _renderGraph(dataFile, propertyFile, outputFile, afPath, POSTdata['renderingFilter'], param) CAPTCHA_PUBLIC_KEY = settings.AF_RECAPTCHA_PUBLIC_KEY # Form instance to save the rendered image to the gallery. form = gallerySubmitForm(initial={'image': requestID}) # Store the unique request-ID used in this render request (to be used # if the user opts in to submit the image to the gallery). request.session['requestID'] = requestID #Construct a rendered graph response. response = render_to_response('render.html', locals(), context_instance=RequestContext(request)) #Check if the user wanted to save/create a cookie to save their #settings for future use. if ("saveConfigCookie" in POSTdata): expiry = datetime.now() + timedelta(days=3) response.set_cookie(key="afConfig", value=_buildCookie(POSTdata), expires=expiry) return response
def _render(request, parsedData, loggly=False, logglyData=None): ''' Processes a request and attempts to render a graph. Keyword arguments: request -- the request object. parsedData -- boolean, True if the incoming data is parsed data (log/Loggly) loggly -- boolean, True if the incoming data is from Loggly. logglyData -- A string data returned from a Loggly search. Return: A rendered graph from the request if successful or an error message on the view. ''' # Generate a session-key if one isn't already active. if hasattr(request, 'session') and hasattr(request.session, \ 'session_key') and \ getattr(request.session, 'session_key') is None: request.session.create() # Generate a unique request ID hash from the session key for this particular # request. requestID = md5(request.session.session_key + str(datetime.now())).hexdigest() # Clean up old user resource files which are older than four hours. _cleanFiles() # If this rendering request's data has been fetched from Loggly, then # retrieve the rendering settings/configurations from the session (where it # has been previously saved). if loggly: POSTdata = request.session["logglyForm"] else: POSTdata = request.POST # Flag to check and alert of any errors encountered while attempting to # render. retVal = 0 if parsedData: if loggly: # If data from Loggly (after a search) is empty, alert the user. if not logglyData: emptyData = True return render_to_response('render.html', locals(), context_instance=RequestContext(request)) # Parse the data from Loggly's search into a CSV file. retVal = _parseToCsv(logglyData, requestID, POSTdata, True) else: # Parse the log file into a CSV file with the expression given. retVal = _parseToCsv(request.FILES['dataFile'], requestID, POSTdata) # If the user submitted a custom regex expression and indicated to # save it; insert the data into the database and alert an administrator # through email. if not retVal and POSTdata['regExType'] == '1' and "saveRegEx" in POSTdata: expression = Expressions(name=POSTdata['saveRegExName'], \ description=POSTdata['saveRegExDescription'], \ regex=POSTdata['regEx']) expression.save() message = "Hello, a new expression has been submitted.\n" message += "Exp name: " + POSTdata["saveRegExName"] \ + "\n\nDescription: " + POSTdata["saveRegExDescription"] \ + "\n\nExpression: " + POSTdata["regEx"] \ + "\n\n" from_email = settings.AF_FROM_EMAIL try: send_mail("Expression submit @AfterGlow", message, from_email, settings.AF_TO_EMAILS) except BadHeaderError: return HttpResponse('Invalid header found. Please try again.') else: # A CSV file has been uploaded, write it as-is to the server. _writeDataFile(request.FILES['dataFile'], requestID) # If retVal has been changed from 0 to any other value from its point of # declaration we have an error. Alert the user. if retVal: return render_to_response('render.html', locals(), context_instance=RequestContext(request)) else: _writeConfigFile(POSTdata['propertyConfig'], requestID) #Build up parameters to be sent to the shell script. param = _buildParameters(POSTdata) if parsedData: dataFile = "user_logs_parsed/" + requestID + ".log" else: dataFile = "user_data/" + requestID + ".csv" propertyFile = "user_config/" + requestID + ".property" outputFile = "afterglow_cloud/app/static/rendered/" + requestID + ".gif" afPath = "../afterglow/src/afterglow.pl" #Try rendering a graph, store the return code from the shell script. status = _renderGraph(dataFile, propertyFile, outputFile, afPath, POSTdata['renderingFilter'], param) CAPTCHA_PUBLIC_KEY = settings.AF_RECAPTCHA_PUBLIC_KEY # Form instance to save the rendered image to the gallery. form = gallerySubmitForm(initial = {'image' : requestID}) # Store the unique request-ID used in this render request (to be used # if the user opts in to submit the image to the gallery). request.session['requestID'] = requestID #Construct a rendered graph response. response = render_to_response('render.html', locals(), context_instance=RequestContext(request)) #Check if the user wanted to save/create a cookie to save their #settings for future use. if("saveConfigCookie" in POSTdata): expiry = datetime.now() + timedelta(days = 3) response.set_cookie(key = "afConfig", value = _buildCookie(POSTdata), expires = expiry) return response