From 4aed6fa6d5b2a4251a135800612684372bcaf511 Mon Sep 17 00:00:00 2001 From: Faris Ansari Date: Tue, 23 Jul 2019 19:11:19 +0530 Subject: [PATCH] fix: Check is_private when checking for duplicates (#7957) * fix: Check is_private when checking for duplicates * fix: Fallback file_size --- frappe/core/doctype/file/file.py | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/frappe/core/doctype/file/file.py b/frappe/core/doctype/file/file.py index f93b7ea733..3c3543e1dd 100755 --- a/frappe/core/doctype/file/file.py +++ b/frappe/core/doctype/file/file.py @@ -188,14 +188,20 @@ class File(NestedSet): # check duplicate name # check duplicate assignement - n_records = frappe.db.sql("""select name from `tabFile` - where content_hash=%s - and name!=%s - and attached_to_doctype=%s - and attached_to_name=%s""", (self.content_hash, self.name, self.attached_to_doctype, - self.attached_to_name)) - if len(n_records) > 0: - self.duplicate_entry = n_records[0][0] + filters = { + 'content_hash': self.content_hash, + 'is_private': self.is_private, + 'name': ('!=', self.name) + } + if self.attached_to_doctype and self.attached_to_name: + filters.update({ + 'attached_to_doctype': self.attached_to_doctype, + 'attached_to_name': self.attached_to_name + }) + duplicate_file = frappe.db.get_value('File', filters) + + if duplicate_file: + self.duplicate_entry = duplicate_file frappe.throw(_("Same file has already been attached to the record"), frappe.DuplicateEntryError) @@ -451,7 +457,7 @@ class File(NestedSet): return self.file_url = unquote(self.file_url) - self.file_size = frappe.form_dict.file_size + self.file_size = frappe.form_dict.file_size or self.file_size def get_uploaded_content(self):