Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Enable watermarking and thumbnails and add helper methods #74

Merged
merged 3 commits into from
Oct 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
Abadulrehman marked this conversation as resolved.
Show resolved Hide resolved
},
"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)
Abadulrehman marked this conversation as resolved.
Show resolved Hide resolved
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