From 8d61a7c153bbe52f7681c63321590f2b680b6805 Mon Sep 17 00:00:00 2001 From: AbdulRehman Muhammad Younis Date: Thu, 30 Sep 2021 11:57:07 +0400 Subject: [PATCH 1/3] fix: added File `on_update` doc_event to `hooks.py` to enable use of auto thumbnail and watermark generation --- renovation_core/hooks.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/renovation_core/hooks.py b/renovation_core/hooks.py index 85f9e1b..5c63eef 100644 --- a/renovation_core/hooks.py +++ b/renovation_core/hooks.py @@ -134,6 +134,9 @@ "on_change": "renovation_core.doc_events.system_settings.on_change", "before_update": "renovation_core.doc_events.system_settings.before_update" }, + "File": { + "on_update": "renovation_core.doc_events.file.on_update" + }, "Renovation Sidebar": { "on_change": "renovation_core.utils.renovation.clear_sidebar_cache" }, From a5dc27b2ddba1da055b964436552308c35f4843e Mon Sep 17 00:00:00 2001 From: AbdulRehman Muhammad Younis Date: Thu, 30 Sep 2021 12:05:46 +0400 Subject: [PATCH 2/3] feat: added thumbnail helper methods in `utils.images` Co-authored-by: Elton Lobo --- renovation_core/utils/images.py | 40 +++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/renovation_core/utils/images.py b/renovation_core/utils/images.py index 6c908ef..de05bc8 100644 --- a/renovation_core/utils/images.py +++ b/renovation_core/utils/images.py @@ -185,3 +185,43 @@ def saveImage(im, filename, extn): def get_attachments(doctype, name, only_images=False, ignore_permissions=False): from .files import get_attachments as get_attach return get_attach(doctype, name, only_images, ignore_permissions) + + +def get_thumbnail_url(dt, dn, df): + """ + Fetches the thumbnail url (if it exists) of an image field given: + + dt: `str` + The Doc-type it is defined on + dn: `str` + The name of Doc it is attached to + df: `str` + The field on the Doc it is attached to i.e: the "Attach" field + """ + file_url = frappe.get_value(dt, dn, df) + if frappe.is_table(dt): + dt, dn = frappe.get_value(dt, dn, ("parenttype", "parent")) + + attachments = get_attachments_by_docfield(dt, dn, df, file_url) + return attachments[0].get("thumbnail_url") if len(attachments) else None + + +def get_attachments_by_docfield(dt, dn, df, file_url): + fields = ["thumbnail_url"] + filters = {"attached_to_name": dn, + "file_url": file_url, + "attached_to_doctype": dt, + "attached_to_field": df} + files = frappe.get_all( + "File", + fields=fields, + filters=filters) + + if not files: + # lets try again to find the image just by looking at file_url + files = frappe.get_all( + "File", + fields=fields, + filters={"file_url": file_url, + "thumbnail_url": ("!=", "")}) + return files From ebc311672433ddc03db87db519017c995829e3fc Mon Sep 17 00:00:00 2001 From: AbdulRehman Muhammad Younis Date: Mon, 4 Oct 2021 10:15:29 +0400 Subject: [PATCH 3/3] fix: split `get_thumbnail` helper function --- renovation_core/utils/images.py | 38 ++++++++++++++++++++++----------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/renovation_core/utils/images.py b/renovation_core/utils/images.py index de05bc8..72a8a56 100644 --- a/renovation_core/utils/images.py +++ b/renovation_core/utils/images.py @@ -198,15 +198,23 @@ def get_thumbnail_url(dt, dn, df): df: `str` The field on the Doc it is attached to i.e: the "Attach" field """ + file_url = frappe.get_value(dt, dn, df) + if not file_url: + return + if frappe.is_table(dt): dt, dn = frappe.get_value(dt, dn, ("parenttype", "parent")) - attachments = get_attachments_by_docfield(dt, dn, df, file_url) - return attachments[0].get("thumbnail_url") if len(attachments) else None + thumbnail_url = get_thumbnail_url_by_docfield(dt, dn, df, file_url) + if not thumbnail_url: + thumbnail_url = get_thumbnail_url_by_file_url(file_url) + + return thumbnail_url -def get_attachments_by_docfield(dt, dn, df, file_url): +def get_thumbnail_url_by_docfield(dt, dn, df, file_url): + '''Will try to find the thumbnail url by the field the image is attached to''' fields = ["thumbnail_url"] filters = {"attached_to_name": dn, "file_url": file_url, @@ -215,13 +223,17 @@ def get_attachments_by_docfield(dt, dn, df, file_url): files = frappe.get_all( "File", fields=fields, - filters=filters) - - if not files: - # lets try again to find the image just by looking at file_url - files = frappe.get_all( - "File", - fields=fields, - filters={"file_url": file_url, - "thumbnail_url": ("!=", "")}) - return files + filters=filters + ) + return files[0].get("thumbnail_url") if len(files) else None + + +def get_thumbnail_url_by_file_url(file_url): + '''Will try to find a thumbnail url by file_url''' + files = frappe.get_all( + "File", + fields=["thumbnail_url"], + filters={"file_url": file_url, + "thumbnail_url": ("!=", "")} + ) + return files[0].get("thumbnail_url") if len(files) else None