def capture(request): # Merge both QueryDict into dict parameters = dict([(k,v) for k,v in request.GET.items()]) parameters.update(dict([(k,v) for k,v in request.POST.items()])) url = parameters.get('url') if not url: return HttpResponseForbidden() method = parameters.get('method', request.method) selector = parameters.get('selector') data = parameters.get('data') try: width = int(parameters.get('width', '')) except ValueError: width = None try: height = int(parameters.get('height', '')) except ValueError: height = None try: validate = URLValidator() validate(url) except ValidationError: try: url = request.build_absolute_uri(reverse(url)) except NoReverseMatch: raise Http404(_("Cannot reverse URL '%s'") % url) response = HttpResponse(mimetype='image/png') casperjs_capture(response, url, method=method.lower(), width=width, height=height, selector=selector, data=data) return response
def capture(request): # Merge both QueryDict into dict parameters = dict([(k, v) for k, v in request.GET.items()]) parameters.update(dict([(k, v) for k, v in request.POST.items()])) url = parameters.get('url') if not url: return HttpResponseBadRequest(_('Missing url parameter')) try: url = parse_url(request, url) except NoReverseMatch: error_msg = _("URL '%s' invalid (could not reverse)") % url return HttpResponseBadRequest(error_msg) method = parameters.get('method', request.method) selector = parameters.get('selector') data = parameters.get('data') waitfor = parameters.get('waitfor') wait = parameters.get('wait') render = parameters.get('render', 'png') size = parameters.get('size') crop = parameters.get('crop') try: width = int(parameters.get('width', '')) except ValueError: width = None try: height = int(parameters.get('height', '')) except ValueError: height = None stream = StringIO() try: casperjs_capture(stream, url, method=method.lower(), width=width, height=height, selector=selector, data=data, size=size, waitfor=waitfor, crop=crop, render=render, wait=wait) except CaptureError as e: return HttpResponseBadRequest(e) except ImportError: error_msg = _('Resize not supported (PIL not available)') return HttpResponseBadRequest(error_msg) except UnsupportedImageFormat: error_msg = _('Unsupported image format: %s' % render) return HttpResponseBadRequest(error_msg) if render == "html": response = HttpResponse(mimetype='text/html') body = """<html><body onload="window.print();"> <img src="data:image/png;base64,%s"/></body></html> """ % base64.encodestring(stream.getvalue()) response.write(body) else: response = HttpResponse(mimetype=image_mimetype(render)) response.write(stream.getvalue()) return response
def capture(request): # Merge both QueryDict into dict parameters = dict([(k, v) for k, v in request.GET.items()]) parameters.update(dict([(k, v) for k, v in request.POST.items()])) url = parameters.get('url') if not url: return HttpResponseForbidden() method = parameters.get('method', request.method) selector = parameters.get('selector') data = parameters.get('data') waitfor = parameters.get('waitfor') render = parameters.get('render', 'png') try: width = int(parameters.get('width', '')) except ValueError: width = None try: height = int(parameters.get('height', '')) except ValueError: height = None size_raw = parameters.get('size') size = parse_size(size_raw) try: validate = URLValidator() validate(url) except ValidationError: try: url = request.build_absolute_uri(reverse(url)) except NoReverseMatch: raise Http404(_("Cannot reverse URL '%s'") % url) stream = StringIO() try: casperjs_capture(stream, url, method=method.lower(), width=width, height=height, selector=selector, data=data, size=size, waitfor=waitfor) except ImportError: return HttpResponseBadRequest(_('Resize not supported')) if render == "html": response = HttpResponse(mimetype='text/html') body = """<html><body onload="window.print();"><img src="data:image/jpg;base64,%s"/></body></html>""" % base64.encodestring(stream.getvalue()) response.write(body) else: response = HttpResponse(mimetype='image/png') response.write(stream.getvalue()) return response
def capture(request): # Merge both QueryDict into dict parameters = dict([(k, v) for k, v in request.GET.items()]) parameters.update(dict([(k, v) for k, v in request.POST.items()])) url = parameters.get('url') if not url: return HttpResponseBadRequest(_('Missing url parameter')) try: url = parse_url(request, url) except NoReverseMatch: error_msg = _("URL '%s' invalid (could not reverse)") % url return HttpResponseBadRequest(error_msg) method = parameters.get('method', request.method) selector = parameters.get('selector') data = parameters.get('data') waitfor = parameters.get('waitfor') render = parameters.get('render', 'png') size = parameters.get('size') crop = parameters.get('crop') try: width = int(parameters.get('width', '')) except ValueError: width = None try: height = int(parameters.get('height', '')) except ValueError: height = None stream = StringIO() try: casperjs_capture(stream, url, method=method.lower(), width=width, height=height, selector=selector, data=data, size=size, waitfor=waitfor, crop=crop, render=render) except CaptureError as e: return HttpResponseBadRequest(e) except ImportError: error_msg = _('Resize not supported (PIL not available)') return HttpResponseBadRequest(error_msg) except UnsupportedImageFormat: error_msg = _('Unsupported image format: %s' % render) return HttpResponseBadRequest(error_msg) if render == "html": response = HttpResponse(mimetype='text/html') body = """<html><body onload="window.print();"> <img src="data:image/png;base64,%s"/></body></html> """ % base64.encodestring(stream.getvalue()) response.write(body) else: response = HttpResponse(mimetype=image_mimetype(render)) response.write(stream.getvalue()) return response
def test_javascript_errors_are_logged(self): casperjs_capture('/tmp/file.png', '%s/data/test_page.html' % here) utils_logger.error.assert_any_call(' Error: Ha ha')
def test_console_message_are_logged(self): casperjs_capture('/tmp/file.png', '%s/data/test_page.html' % here) utils_logger.info.assert_any_call(' Hey hey')
def base64capture(url, selector): simage = StringIO() casperjs_capture(simage, url, selector=selector) # Convert to base64 encoded = base64.encodestring(simage.getvalue()) return "image/png;base64," + encoded