def get_reporting_instance_usage_file(self): conn = self.get_connection(conn_type='ec2reports') if not self.is_csrf_valid(): return JSONResponse(status=400, message="missing CSRF token") granularity = self.request.params.get('granularity') group_by = self.request.params.get('groupBy') dates = self.dates_from_params(self.request.params) filters = self.request.params.get('filters') or '[]' filters = json.loads(filters) with boto_error_handler(self.request): # use "ViewInstanceUsageReport" call to fetch usage information ret = conn.view_instance_usage_report( dates.from_date, dates.to_date, filters, group_by, report_granularity=granularity ) filename = 'EucalyptusInstanceUsage-{0}-{1}-{2}.csv'.format( self.request.session.get('account'), dates.from_date, dates.to_date ) response = Response(content_type='text/csv') response.text = ret.get('usageReport') response.content_disposition = 'attachment; filename="{name}"'.format(name=filename) response.cache_control = 'no-store' response.pragma = 'no-cache' return response
def captcha(request): form_id = request.matchdict.get('form_id', u'') # 2 routes: with and without form_id image, ctype = Captcha(request.client_addr, form_id).render() response = Response(body=image, content_type=ctype) response.content_length = len(image) response.cache_control = 'no-cache, no-store' return response
def __call__(self): image = PIL.Image.new('RGB', (self.width,self.height),(255,255,255)) draw = PIL.ImageDraw.Draw(image) draw.rectangle([(0,0),(float(self.width)*self.max_count/self.max_count,10)], fill=self.color2) draw.rectangle([(0,0),(float(self.width)*self.count/self.max_count,10)], fill=self.color1) output = StringIO.StringIO() image.save(output, format='PNG') response = Response() response.content_type = 'image/png' response.cache_control = 'max-age=86400' response.body = output.getvalue() output.close() return response
def __call__(self): image = PIL.Image.new('RGB', (self.width,self.height),(255,255,255)) draw = PIL.ImageDraw.Draw(image) barheight = float(self.height)/len(self.values) for i,bar in enumerate(self.values): draw.rectangle([(0,i*barheight),(float(self.width)*bar[1]/self.max,(i+1)*barheight)], fill=self.color2) draw.rectangle([(0,i*barheight),(float(self.width)*bar[0]/self.max,(i+1)*barheight)], fill=self.color1) output = io.BytesIO() image.save(output, format='PNG') response = Response() response.content_type = 'image/png' response.cache_control = 'max-age=86400' response.body = output.getvalue() output.close() return response
def _cache_cors(response: Response, request: Request) -> Response: """Cornice filter that fixes the Cache-Control header for pre-flight requests (OPTIONS).""" try: if request.method == "OPTIONS" and "Access-Control-Max-Age" in response.headers: response.cache_control = {"max-age": int(response.headers["Access-Control-Max-Age"])} if response.vary is None: response.vary = ["Origin"] elif "Origin" not in response.vary: response.vary.append("Origin") except Exception: # cornice catches exceptions from filters, and tries call back the filter with only the request. # This leads to a useless message in case of error... LOG.error("Failed fixing cache headers for CORS", exc_info=True) raise return response
def file_download(request): if not(BaseView.is_csrf_valid_static(request)): return JSONResponse(status=400, message="missing CSRF token") session = request.session if session.get('file_cache'): (filename, mime_type, contents) = session['file_cache'] # Clean the session information regrading the new keypair del session['file_cache'] response = Response(content_type=mime_type) response.body = str(contents) response.content_disposition = 'attachment; filename="{name}"'.format(name=filename) response.cache_control = 'no-store' response.pragma = 'no-cache' return response # no file found ... # this isn't handled on on client anyway, so we can return pretty much anything return Response(body='BaseView:file not found', status=500)
def get_reporting_monthly_usage_file(self): if not self.is_csrf_valid(): return JSONResponse(status=400, message="missing CSRF token") year = int(self.request.params.get('year')) month = int(self.request.params.get('month')) # use "ViewMontlyUsage" call to fetch usage information ret = self.conn.view_monthly_usage(year, month) filename = 'EucalyptusMonthlyUsage-{0}-{1}-{2}.csv'.format( self.request.session.get('account'), year, month ) response = Response(content_type='text/csv') response.text = ret.get('data') response.content_disposition = 'attachment; filename="{name}"'.format(name=filename) response.cache_control = 'no-store' response.pragma = 'no-cache' return response
def __call__(self): image = PIL.Image.new('RGB', (self.width, self.height), (255, 255, 255)) draw = PIL.ImageDraw.Draw(image) draw.rectangle( [(0, 0), (float(self.width) * self.max_count / self.max_count, 10)], fill=self.color2) draw.rectangle([(0, 0), (float(self.width) * self.count / self.max_count, 10)], fill=self.color1) output = StringIO.StringIO() image.save(output, format='PNG') response = Response() response.content_type = 'image/png' response.cache_control = 'max-age=86400' response.body = output.getvalue() output.close() return response
def get_feed(request): url = request.matchdict['url'] headers = {'User-Agent': USER_AGENT, 'Accept': ', '.join(ALLOWED_CONTENT_TYPES)} try: with closing(requests.get(url, headers=headers, stream=True)) as r: ct = r.headers.get('content-type', '').split(';')[0].lower() if ct in ALLOWED_CONTENT_TYPES: res = Response() res.content_type = ct if r.headers.get('cache-control'): res.cache_control = r.headers.get('cache-control') for x in r.iter_content(1024 * 1024): if x: res.body_file.write(x) return res else: return Response("Invalid content type: " + ct, status_code=400) except requests.exceptions.RequestException as e: return Response(str(e), status_code=400)
def __call__(self): image = PIL.Image.new("RGB", (self.width, self.height), (255, 255, 255)) draw = PIL.ImageDraw.Draw(image) barheight = float(self.height) / len(self.values) for i, bar in enumerate(self.values): draw.rectangle( [(0, i * barheight), (float(self.width) * bar[1] / self.max, (i + 1) * barheight)], fill=self.color2 ) draw.rectangle( [(0, i * barheight), (float(self.width) * bar[0] / self.max, (i + 1) * barheight)], fill=self.color1 ) output = StringIO.StringIO() image.save(output, format="PNG") response = Response() response.content_type = "image/png" response.cache_control = "max-age=86400" response.body = output.getvalue() output.close() return response
def get_reporting_service_usage_file(self): if not self.is_csrf_valid(): return JSONResponse(status=400, message="missing CSRF token") service = self.request.params.get('service') usage_type = self.request.params.get('usageType') granularity = self.request.params.get('granularity') dates = self.dates_from_params(self.request.params) # use "ViewUsage" call to fetch usage information ret = self.conn.view_usage( service, usage_type, 'all', dates.from_date, dates.to_date, report_granularity=granularity ) filename = 'EucalyptusServiceUsage-{0}-{1}-{2}.csv'.format( self.request.session.get('account'), service, usage_type ) response = Response(content_type='text/csv') response.text = ret.get('data') response.content_disposition = 'attachment; filename="{name}"'.format(name=filename) response.cache_control = 'no-store' response.pragma = 'no-cache' return response