From f474bc3b5c10880a4db42b68c2e9fc095faba70f Mon Sep 17 00:00:00 2001 From: Anand Doshi Date: Fri, 23 Oct 2015 15:25:08 +0530 Subject: [PATCH] [fix] better message when file not found in make thumbnail --- frappe/core/doctype/file/file.py | 19 ++++++++++++++----- frappe/utils/nestedset.py | 10 +++++++++- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/frappe/core/doctype/file/file.py b/frappe/core/doctype/file/file.py index cff12ba877..6f28e4fbd8 100644 --- a/frappe/core/doctype/file/file.py +++ b/frappe/core/doctype/file/file.py @@ -16,6 +16,11 @@ from frappe.utils.nestedset import NestedSet from frappe.utils import strip import json import urllib +from PIL import Image, ImageOps +import os +import requests +import requests.exceptions +import StringIO class FolderNotEmpty(frappe.ValidationError): pass @@ -119,9 +124,6 @@ class File(NestedSet): self.delete_file() def make_thumbnail(self): - from PIL import Image, ImageOps - import os - if self.file_url: if self.file_url.startswith("/files"): try: @@ -132,10 +134,16 @@ class File(NestedSet): else: # downlaod - import requests, StringIO file_url = frappe.utils.get_url(self.file_url) r = requests.get(file_url, stream=True) - r.raise_for_status() + try: + r.raise_for_status() + except requests.exceptions.HTTPError, e: + if "404" in e.args[0]: + frappe.msgprint(_("File '{0}' not found").format(self.file_url)) + + raise + image = Image.open(StringIO.StringIO(r.content)) filename, extn = self.file_url.rsplit("/", 1)[1].rsplit(".", 1) filename = "/files/" + strip(urllib.unquote(filename)) @@ -192,6 +200,7 @@ class File(NestedSet): delete_file_data_content(self, only_thumbnail=True) def on_rollback(self): + self.flags.on_rollback = True self.on_trash() def on_doctype_update(): diff --git a/frappe/utils/nestedset.py b/frappe/utils/nestedset.py index da69c5a47e..b1e86197dc 100644 --- a/frappe/utils/nestedset.py +++ b/frappe/utils/nestedset.py @@ -201,7 +201,15 @@ class NestedSet(Document): frappe.throw(_("Cannot delete {0} as it has child nodes").format(self.name), NestedSetChildExistsError) self.set(self.nsm_parent_field, "") - update_nsm(self) + + try: + update_nsm(self) + except frappe.DoesNotExistError: + if self.flags.on_rollback: + pass + frappe.message_log.pop() + else: + raise def before_rename(self, olddn, newdn, merge=False, group_fname="is_group"): if merge: