From 7c9d64dbb18722ec8d79a1c7ab9d2e5305928dcd Mon Sep 17 00:00:00 2001 From: AbdulRehman Muhammad Younis Date: Mon, 4 Oct 2021 16:42:18 +0400 Subject: [PATCH] feat: Enable watermarking and thumbnails and add helper methods (#74) * fix: added File `on_update` doc_event to `hooks.py` to enable use of auto thumbnail and watermark generation * feat: added thumbnail helper methods in `utils.images` Co-authored-by: Elton Lobo * fix: split `get_thumbnail` helper function Co-authored-by: Elton Lobo --- renovation_core/hooks.py | 3 ++ renovation_core/utils/images.py | 52 +++++++++++++++++++++++++++++++++ 2 files changed, 55 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" }, diff --git a/renovation_core/utils/images.py b/renovation_core/utils/images.py index 6c908ef..72a8a56 100644 --- a/renovation_core/utils/images.py +++ b/renovation_core/utils/images.py @@ -185,3 +185,55 @@ 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 not file_url: + return + + if frappe.is_table(dt): + dt, dn = frappe.get_value(dt, dn, ("parenttype", "parent")) + + 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_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, + "attached_to_doctype": dt, + "attached_to_field": df} + files = frappe.get_all( + "File", + fields=fields, + 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