Skip to content

Commit

Permalink
feat: Enable watermarking and thumbnails and add helper methods (#74)
Browse files Browse the repository at this point in the history
* 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 <elton1231@gmail.com>

* fix: split `get_thumbnail` helper function

Co-authored-by: Elton Lobo <elton1231@gmail.com>
  • Loading branch information
Abadulrehman and e-lobo authored Oct 4, 2021
1 parent 497adb4 commit 7c9d64d
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
3 changes: 3 additions & 0 deletions renovation_core/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
52 changes: 52 additions & 0 deletions renovation_core/utils/images.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 7c9d64d

Please sign in to comment.