|
|
@ -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()) |
|
|
|
|
|
|
|