def __process_form_data(request: HttpRequest, data: Dict) -> Dict: if not isinstance(request.data, dict): raise TypeError( "Request data is expected to be a dict, but it is {}".format( type(request.data) ) ) for (key, value) in request.data.items(): if isinstance(value, UploadedFile): file_type, _ = mimetypes.guess_type(value.name) logger.info( "uploading filetype %s/%s for %s", file_type, value.name, key, ) if file_type and file_type.startswith("image"): data[key] = as_datalink(value.read(), file_type) elif file_type and file_type.startswith("text"): data[key] = value.read().decode() else: raise ValueError("Only image or text is permitted") else: data[key] = value return data
def load_data(parent: Path) -> Dict: data = {} for i in parent.glob("*"): file_type, _ = mimetypes.guess_type(i.name) key = (i.name[:-len(i.suffix)] if not 'css' in i.suffix else "{}_css".format(i.name[:-len(i.suffix)])) if i.is_dir(): data = {**data, **load_data(i)} elif file_type and file_type.startswith('image'): data[key] = as_datalink(i.read_bytes(), file_type) elif file_type and file_type.startswith('text'): data[key] = i.read_text() elif file_type and file_type == 'application/json': data = {**data, **json.loads(i.read_text())} else: sys.stderr.write("skipping {} -> {}".format(i, file_type)) return data
def test_generate_format_editor_html_report(): def upload(key, data): cache_url = reverse('store_cache') to_send = {"key": key, "value": data} response = APIClient().post(cache_url, data=to_send, format='json') assert response.status_code == 200 def upload_image(key, name, content): cache_url = reverse('store_cache') to_send = { "key": key, "value": { "name": name, "content": content, }, "append": True, } response = APIClient().post(cache_url + '?append_image=true', data=to_send, format='json') assert response.status_code == 200 images = [ as_datalink("p1".encode(), "png"), as_datalink("p2".encode(), "jpg"), ] client = APIClient() url = reverse('transform') report = { 'report': { 'report': gen_report( generate('host', 10), generate('oid', 5), name='report_format_editor_test', ) } } response = client.post(url, data=report, format='xml') assert response.status_code == 200 key = response.data report_url = reverse('report', kwargs={'name': key}) html_template = """ <html> <head> <style>{{ css }}</style> </head> <body> <p>{{ name }}</p> <img src="{{ images.0 }}"/> <img src="{{ images.1 }}"/> </body> </html> """ html_css = "body { background-color: #000; }" for i, content in enumerate(images): upload_image("{}images".format(key), str(i), content) upload("{}html_template".format(key), html_template) upload("{}html_css".format(key), html_css) # upload(images) html_report = client.get(report_url, HTTP_ACCEPT='text/html+report_format_editor') assert html_report.status_code == 200 report = str(html_report.getvalue()) assert 'report_format_editor_test' in report assert 'background-color: #000' in report for content in images: assert "<img src=\"{}\"/>".format(content) in report
def test_generate_format_editor_html_report(): def upload(key, data): cache_url = reverse("store_cache") to_send = {"key": key, "value": data} response = APIClient().post(cache_url, data=to_send, format="json") assert response.status_code == 200 def upload_image(key, name, content): cache_url = reverse("store_cache") to_send = { "key": key, "value": { "name": name, "content": content, }, "append": True, } response = APIClient().post(cache_url + "?append_image=true", data=to_send, format="json") assert response.status_code == 200 images = [ as_datalink("p1".encode(), "png"), as_datalink("p2".encode(), "jpg"), ] client = APIClient() url = reverse("transform") report = { "report": { "report": gen_report( generate("host", 10), generate("oid", 5), name="report_format_editor_test", ) } } response = client.post(url, data=report, format="xml") assert response.status_code == 200 key = response.data report_url = reverse("report", kwargs={"name": key}) html_template = """ <html> <head> <style>{{ css }}</style> </head> <body> <p>{{ name }}</p> <img src="{{ images.0 }}"/> <img src="{{ images.1 }}"/> </body> </html> """ html_css = "body { background-color: #000; }" for i, content in enumerate(images): upload_image(f"{key}images", str(i), content) upload(f"{key}html_template", html_template) upload(f"{key}html_css", html_css) # upload(images) html_report = client.get(report_url, HTTP_ACCEPT="text/html+report_format_editor") assert html_report.status_code == 200 report = str(html_report.getvalue()) assert "report_format_editor_test" in report assert "background-color: #000" in report for content in images: assert f'<img src="{content}"/>' in report