def attach(doctype, name, filedata): response = {} req = frappe.local.form_dict if (req == None): return {} now = frappe.utils.now() req.filename = "{}_{}_{}.jpg".format(doctype, name, now) req.filedata = filedata req.name = name try: uploaded = upload(doctype, req.name, 1) response["code"] = 200 response["data"] = uploaded except Exception as e: response["code"] = 400 response["error"] = e.message except UnboundLocalError as e: response["code"] = 401 response["error"] = e.message return response
def attach_file(): response = {} req = frappe.local.form_dict hash = hashlib.sha1() hash.update(str(time.time())) hash_now = hash.hexdigest()[:10] data = json.loads(req.data) req.filename = "attachment_{}_{}".format(hash_now, data['filename']) req.filedata = data['filedata'] req.name = data['name'] try: uploaded = upload(data['doctype'], req.name, 1) response["code"] = 200 response["message"] = "Success" response["data"] = uploaded except Exception as e: response["code"] = 400 response["message"] = e.message response["data"] = "" except UnboundLocalError as e: response["code"] = 400 response["message"] = e.message response["data"] = "" return response
def upload_image_profile(): response = {} req = frappe.local.form_dict if (req == None): return {} user_id = get_user_id_by_session() if (user_id == ''): response['code'] = 417 response['message'] = 'Session user invalid' response['data'] = None return response req.filename = "profile_{}.jpg".format(user_id) data = json.loads(req.data) req.filedata = data['filedata'] req.role = data['role'] req.name = data['name'] # try: frappe.db.sql( "DELETE FROM `tabFile` WHERE attached_to_name='{}' AND attached_to_doctype='{}'" .format(req.name, req.role)) frappe.db.commit() uploaded = upload(req.role, req.name, 1) response["code"] = 200 response["message"] = "Success" response["data"] = uploaded doc_user = frappe.get_doc(req.role, req.name) doc_user.profile_photo = uploaded['file_url'] doc_user.submit() frappe.db.commit() # except Exception as e: # response["code"] = 400 # response["message"] = e.message # response["data"] = "" # except UnboundLocalError as e: # response["code"] = 401 # response["message"] = e.message # response["data"] = "" return response
def image(): response = {} validate = validate_method(frappe.local.request.method,["POST"]) if validate != True: return validate req = frappe.local.form_dict req.filename = "checkpoint.jpg" # validate = validate_param_exist([req.job_order_update, req.filedata],"job_order_update, filedata") data = json.loads(req.data) req.filedata = data['filedata'] req.job_order_update = data['job_order_update'] try: uploaded = upload("Job Order Update",req.job_order_update,1) data = { "doctype": "Job Order Update Image", "docstatus": 0, "parent": req.job_order_update, "parenttype": "Job Order Update", "parentfield": "job_order_update_images", "attach": uploaded["file_url"] } result = frappe.get_doc(data).insert().as_dict() response["code"] = 200 response["message"] = "Success" response["data"] = result except Exception as e: response["code"] = 400 response["message"] = e.message response["data"] = "" except UnboundLocalError as e: response["code"] = 400 response["message"] = e.message response["data"] = "" return response
def attach_image_to_expense_claim(): response = {} validate = validate_method(frappe.local.request.method, ["POST"]) if validate != True: return validate req = frappe.local.form_dict hash = hashlib.sha1() hash.update(str(time.time())) hash_now = hash.hexdigest()[:10] req.filename = "attachment_{}.jpg".format(hash_now) data = json.loads(req.data) req.filedata = data['filedata'] req.expense_claim = data['expense_claim'] try: uploaded = upload("Expense Claim", req.expense_claim, 1) response["code"] = 200 response["message"] = "Success" response["data"] = uploaded except Exception as e: response["code"] = 400 response["message"] = e.message response["data"] = "" except UnboundLocalError as e: response["code"] = 400 response["message"] = e.message response["data"] = "" return response
def generate_thumbnail(filename, doctype, name): # generate thumbnail using Image PIL sitename = cstr(frappe.local.site) cd = os.getcwd() if 'private' not in filename: img = Image.open(os.path.join(cd, sitename, 'public/') + filename) # with open(os.path.join(cd,sitename,'public/') + filename, 'rb') as f: # img = f.read() else: img = Image.open(os.path.join(cd, sitename, '/') + filename) # with open(os.path.join(cd,sitename,'/') + filename, 'rb') as f: # img = f.read() width, height = img.size optimum_width = 1600 # if width<basewidth: # basewidth = width # else: # if basewidth < width/3: # basewidth = width/3 if ".png" in filename.lower(): img = img.convert('RGBA') hsize = get_height(width, height, optimum_width) if hsize == height: optimum_width = width img = img.resize((optimum_width, hsize), PIL.Image.ANTIALIAS) buffered = BytesIO() img.save(buffered, format="PNG", quality=100) img_str = base64.b64encode(buffered.getvalue()) else: img = img.convert('RGB') hsize = get_height(width, height, optimum_width) if hsize == height: optimum_width = width img = img.resize((optimum_width, hsize), PIL.Image.ANTIALIAS) buffered = BytesIO() img.save(buffered, format="JPEG", quality=100) img_str = base64.b64encode(buffered.getvalue()) #upload thumnail image req = frappe.local.form_dict req.filedata = img_str req.name = name filename_attr = filename.split('/') req.filename = "thumbnail_{}".format(filename_attr[len(filename_attr) - 1]) response = {} # try: uploaded = upload(doctype, req.name, 0) response["code"] = 200 response["message"] = "Success" response["data"] = uploaded frappe.db.commit() # except Exception as e: # response["code"] = 400 # response["message"] = e.message # response["data"] = "" # except UnboundLocalError as e: # response["code"] = 401 # response["message"] = e.message # response["data"] = "" return response