Browse Source

Circular file validtion

master
venkataakhil 1 year ago
parent
commit
8272b936c7
  1. 27
      smart_service/add_ons/doctype/circular/circular.js
  2. 4
      smart_service/add_ons/doctype/circular/circular.json
  3. 136
      smart_service/add_ons/doctype/circular/circular.py

27
smart_service/add_ons/doctype/circular/circular.js

@ -11,5 +11,32 @@ frappe.ui.form.on('Circular', {
frappe.msgprint("<b>Description:</b> Only <b> 500 </b> character's are allowed")
validated = false;
}
if (frm.doc.circular_image) {
console.log("calling")
frappe.call({
method: "smart_service.add_ons.doctype.circular.circular.file_validation",
args: {
image: frm.doc.circular_image,
name: frm.doc.name
},
callback: function (r) {
console.log(r.message, "r.message")
if (r.message && r.message[0] == false) {
frm.doc.circular_image = ""
frm.refresh_fields("circular_image")
if (r.message[1] == 1) {
frappe.validated = false;
frappe.msgprint("Invalid file type")
}
if (r.message[1] == 2) {
frappe.validated = false;
frappe.msgprint("Invalid file size")
}
}
}
})
}
}
});

4
smart_service/add_ons/doctype/circular/circular.json

@ -68,13 +68,13 @@
{
"description": "Upload *.png,*.jpg",
"fieldname": "circular_image",
"fieldtype": "Attach Image",
"fieldtype": "Attach",
"label": "Circular Image"
}
],
"index_web_pages_for_search": 1,
"links": [],
"modified": "2023-06-28 13:20:10.845730",
"modified": "2023-06-28 14:46:47.661680",
"modified_by": "Administrator",
"module": "Add Ons",
"name": "Circular",

136
smart_service/add_ons/doctype/circular/circular.py

@ -7,4 +7,138 @@ from frappe.model.document import Document
class Circular(Document):
def validate(self):
if self.description and len(self.description)>500:
frappe.throw("Description should be less than or equal to 850 characters ")
frappe.throw("Description should be less than or equal to 850 characters ")
@frappe.whitelist()
def file_validation(image, name):
if image:
res = check_img_ext(image)
res1 = check_png_ext(image)
res2 = check_pdf_ext(image)
if res == True or res1 == True or res2 == True:
width, height, size = details_of_image(image)
if width > 49 or height > 49 or size > 65 and image:
val = checking_image(image, "Circular",
"circular_image", name)
if val == True:
return False, 2
else:
return True
else:
val = checking_image(image, "Circular",
"circular_image", name)
if res == False:
ret = res
elif res1 == False:
ret = res1
else:
ret = res2
return ret, 1
@frappe.whitelist()
def check_pdf_ext(path):
if path:
ext = path.split()[-1]
if ext.lower().endswith('.pdf'):
return True
else:
return False
else:
return False
@frappe.whitelist()
def check_img_ext(path):
if path:
ext = path.split()[-1]
if ext.lower().endswith('.jpg') or ext.lower().endswith('.jpeg'):
return True
else:
return False
else:
return False
@frappe.whitelist()
def check_png_ext(path):
if path:
ext = path.split()[-1]
if ext.lower().endswith('.png'):
return True
else:
return False
else:
return False
def details_of_image(image_file):
try:
# Image dimension validation
import os
from PIL import Image
if image_file.startswith('/private/'):
filepath = get_absolute_path(image_file, True)
else:
filepath = get_absolute_path(image_file)
img = Image.open(filepath)
width = img.width
height = img.height
size = os.stat(filepath)
size=size[6]/1024
return width, height,size
except Exception as e:
raise e
def get_absolute_path(file_name, is_private=False):
try:
if file_name.startswith('/files/'):
file_name = file_name[7:]
else:
file_name = file_name[15:]
return frappe.utils.get_bench_path() + "/sites/" + frappe.utils.get_path('private' if is_private else 'public',
'files', file_name)[2:]
except Exception as e:
frappe.log_error("get_absolute_path", frappe.get_traceback())
def checking_image(file_url, attached_to_doctype, attached_to_field, docname, is_child=None):
try:
import time
time.sleep(1)
if docname.startswith('new'):
image_name = frappe.db.get_list("File", fields={"name"}, filters={"file_url": file_url,
"attached_to_doctype": attached_to_doctype,
"attached_to_field": attached_to_field,
"attached_to_name": docname})
else:
image_name = frappe.db.get_list("File", fields={"name"}, filters={"file_url": file_url,
"attached_to_doctype": attached_to_doctype,
"attached_to_field": attached_to_field})
image_name = frappe.db.sql(f"""select name from `tabFile` where file_url='{file_url}' and attached_to_doctype='{attached_to_doctype}' and
attached_to_field='{attached_to_field}'""",as_dict=1)
# frappe.msgprint("data-"+str(file_url)+"-"+str(attached_to_doctype)+"-"+str(attached_to_field)+"-"+str(docname))
if not attached_to_doctype == "Symbol Scanner" and is_child != 1:
frappe.db.sql(
'''UPDATE `tab{0}` SET {2} = '' where name = "{1}"'''.format(attached_to_doctype, docname,
attached_to_field))
frappe.db.commit()
if attached_to_doctype == "Symbol Scanner":
frappe.db.set_value(
"Symbol Scanner", "Symbol Scanner", attached_to_field, "")
if len(image_name) > 0:
frappe.delete_doc('File', image_name[0]['name'])
return True
except Exception as e:
frappe.log_error("Checking images", frappe.get_traceback())

Loading…
Cancel
Save