fix: Handle empty image

This commit is contained in:
Faris Ansari 2020-04-25 15:48:05 +05:30
parent c4891b47be
commit dbf180f368
3 changed files with 64 additions and 56 deletions

View file

@ -1,4 +1,4 @@
{%- set res = frappe.utils.get_thumbnail_base64_for_image(src) -%}
{%- set res = frappe.utils.get_thumbnail_base64_for_image(src) if src else false -%}
{%- if res and res['base64'].startswith('data:') -%}
<img src="{{ res['base64'] }}" class="blur-md transition-all duration-300 ease-in-out {{ resolve_class(class) }}"
alt="{{ alt or '' }}" width="{{ res['width'] }}" height="{{ res['height'] }}" data-src="{{ src or '' }}" />

View file

@ -658,6 +658,9 @@ def get_thumbnail_base64_for_image(src):
from frappe.core.doctype.file.file import get_local_image
from frappe import safe_decode, cache
if not src:
frappe.throw('Invalid source for image: {0}'.format(src))
if not src.startswith('/files'):
return

View file

@ -33,14 +33,70 @@ class TestWebTemplate(unittest.TestCase):
self.assertTrue("/test" == button.attrs["href"])
def test_web_page_with_page_builder(self):
if not frappe.db.exists("Web Page", "test-page"):
self.create_web_page()
set_request(method="GET", path="test-web-template")
response = render()
self.assertEquals(response.status_code, 200)
html = frappe.safe_decode(response.get_data())
soup = BeautifulSoup(html, "html.parser")
sections = soup.find("main").find_all("section")
self.assertEqual(len(sections), 2)
self.assertEqual(sections[0].find("h2").text, "Test Title")
self.assertEqual(sections[0].find("p").text, "test lorem ipsum")
self.assertEqual(len(sections[1].find_all("a")), 3)
def test_tailwind_styles_in_developer_mode(self):
self.create_web_page()
theme = self.create_tailwind_theme()
theme.set_as_default()
set_request(method="GET", path="test-web-template")
response = render()
self.assertEquals(response.status_code, 200)
html = frappe.safe_decode(response.get_data())
soup = BeautifulSoup(html, "html.parser")
stylesheet = soup.select_one('link[rel="stylesheet"]')
self.assertEqual(stylesheet.attrs['href'], '/assets/css/tailwind.css')
frappe.get_doc('Website Theme', 'Standard').set_as_default()
def test_tailwind_styles_in_production(self):
self.create_web_page()
theme = self.create_tailwind_theme()
theme.set_as_default()
frappe.conf.developer_mode = 0
set_request(method="GET", path="test-web-template")
response = render()
self.assertEquals(response.status_code, 200)
html = frappe.safe_decode(response.get_data())
soup = BeautifulSoup(html, "html.parser")
style = soup.select_one("style[data-tailwind]")
self.assertTrue(style)
self.assertTrue('py-20' in style.text)
self.assertTrue('text-gray-900' in style.text)
frappe.get_doc('Website Theme', 'Standard').set_as_default()
def create_web_page(self):
if not frappe.db.exists("Web Page", "test-web-template"):
frappe.get_doc(
{
"doctype": "Web Page",
"title": "test-page",
"name": "test-page",
"title": "test-web-template",
"name": "test-web-template",
"published": 1,
"route": "testpage",
"route": "test-web-template",
"content_type": "Page Builder",
"page_blocks": [
{
@ -72,57 +128,6 @@ class TestWebTemplate(unittest.TestCase):
}
).insert()
set_request(method="GET", path="testpage")
response = render()
self.assertEquals(response.status_code, 200)
html = frappe.safe_decode(response.get_data())
soup = BeautifulSoup(html, "html.parser")
sections = soup.find("main").find_all("section")
self.assertEqual(len(sections), 2)
self.assertEqual(sections[0].find("h2").text, "Test Title")
self.assertEqual(sections[0].find("p").text, "test lorem ipsum")
self.assertEqual(len(sections[1].find_all("a")), 3)
def test_tailwind_styles_in_developer_mode(self):
theme = self.create_tailwind_theme()
theme.set_as_default()
set_request(method="GET", path="testpage")
response = render()
self.assertEquals(response.status_code, 200)
html = frappe.safe_decode(response.get_data())
soup = BeautifulSoup(html, "html.parser")
stylesheet = soup.select_one('link[rel="stylesheet"]')
self.assertEqual(stylesheet.attrs['href'], '/assets/css/tailwind.css')
frappe.get_doc('Website Theme', 'Standard').set_as_default()
def test_tailwind_styles_in_production(self):
theme = self.create_tailwind_theme()
theme.set_as_default()
frappe.conf.developer_mode = 0
set_request(method="GET", path="testpage")
response = render()
self.assertEquals(response.status_code, 200)
html = frappe.safe_decode(response.get_data())
soup = BeautifulSoup(html, "html.parser")
style = soup.select_one("style[data-tailwind]")
self.assertTrue(style)
self.assertTrue('py-20' in style.text)
self.assertTrue('text-gray-900' in style.text)
frappe.get_doc('Website Theme', 'Standard').set_as_default()
def create_tailwind_theme(self):
if not frappe.db.exists('Website Theme', 'Tailwind'):
theme = frappe.get_doc({