Merge pull request #1118 from pdvyas/print-format-extract-images

[fix] Extract images from HTML fields for print format
This commit is contained in:
Rushabh Mehta 2015-05-21 21:27:06 +05:30
commit b837efaf71
3 changed files with 22 additions and 5 deletions

View file

@ -545,10 +545,10 @@ class BaseDocument(object):
return val
def _extract_images_from_text_editor(self):
from frappe.utils.file_manager import extract_images_from_html
from frappe.utils.file_manager import extract_images_from_doc
if self.doctype != "DocType":
for df in self.meta.get("fields", {"fieldtype":"Text Editor"}):
extract_images_from_html(self, df.fieldname)
extract_images_from_doc(self, df.fieldname)
def _filter(data, filters, limit=None):
"""pass filters as:

View file

@ -4,6 +4,7 @@
from __future__ import unicode_literals
import frappe
import frappe.utils
import json
from jinja2 import TemplateSyntaxError
from frappe.model.document import Document
@ -20,6 +21,8 @@ class PrintFormat(Document):
self.old_doc_type = frappe.db.get_value('Print Format',
self.name, 'doc_type')
self.extract_images()
if self.html:
jenv = frappe.get_jenv()
try:
@ -28,6 +31,15 @@ class PrintFormat(Document):
frappe.msgprint('Line {}: {}'.format(e.lineno, e.message))
frappe.throw(frappe._("Syntax error in Jinja template"))
def extract_images(self):
from frappe.utils.file_manager import extract_images_from_html
if self.format_data:
data = json.loads(self.format_data)
for df in data:
if df.get('fieldtype') and df['fieldtype'] in ('HTML', 'Custom HTML') and df.get('options'):
df['options'] = extract_images_from_html(self, df['options'])
self.format_data = json.dumps(data)
def on_update(self):
if hasattr(self, 'old_doc_type') and self.old_doc_type:
frappe.clear_cache(doctype=self.old_doc_type)

View file

@ -83,8 +83,13 @@ def get_uploaded_content():
frappe.msgprint(_('No file attached'))
return None, None
def extract_images_from_html(doc, fieldname):
def extract_images_from_doc(doc, fieldname):
content = doc.get(fieldname)
content = extract_images_from_html(doc, content)
if frappe.flags.has_dataurl:
doc.set(fieldname, content)
def extract_images_from_html(doc, content):
frappe.flags.has_dataurl = False
def _save_file(match):
@ -110,8 +115,8 @@ def extract_images_from_html(doc, fieldname):
if content:
content = re.sub('<img[^>]*src\s*=\s*["\'](?=data:)(.*?)["\']', _save_file, content)
if frappe.flags.has_dataurl:
doc.set(fieldname, content)
return content
def get_random_filename(extn=None, content_type=None):
if extn: