def get_doc(doctype, name): """ Builds the response to be sent back Fills in with doc data """ try: doc = frappe.get_doc(doctype, name) if not doc.has_permission("read"): raise frappe.PermissionError user_lang = frappe.get_request_header('Accept-Language') if user_lang: frappe.local.lang = user_lang.split('-')[0] update_http_response({ "data": update_transalte(doc.doctype, doc.as_dict()), "status": "success" }) except Exception as ex: update_http_response({ "data": "failed", "status": "failed", "exception": ex, "traceback": traceback.format_exc() })
def post_doc(doctype): try: # if modifying, update save_submit_doc too # fetch defaults here and update data = frappe._dict(get_request_body_doc()) apply_docdefaults(data, data, doctype) doc = frappe.new_doc(doctype) # update doctype field since it wont be passed in data doc.update(data) doc.insert() apply_docdefaults(doc, doc, doctype, evaluation_time="After Save") doc.save(ignore_permissions=True) # check submittable # check_submittable(doc, "submit") frappe.db.commit() update_http_response({"data": doc.as_dict(), "status": "success"}) except Exception as ex: frappe.db.rollback() update_http_response({ "data": "failed", "status": "failed", "exception": ex, "traceback": traceback.format_exc() })
def generate_sms_pin(): mobile = frappe.local.form_dict.mobile newPIN = cint(frappe.local.form_dict.newPIN or "0") # If mobile needs to automatically the received hash = frappe.local.form_dict.hash if not mobile: frappe.throw("No Mobile Number") # check cache for pin pin = frappe.safe_decode(frappe.cache().get("sms:" + mobile)) user = get_linked_user(mobile) if not pin and user: # check if available in db pin = frappe.db.get_value("User", user, "renovation_sms_pin") if not pin or newPIN: # generate a pin pin = frappe.safe_decode(str(get_pin())) frappe.cache().set("sms:" + mobile, pin) # save in User doc if mobile linked to any User if user: frappe.db.set_value("User", user, "renovation_sms_pin", pin, update_modified=False) msg = u"Your verification OTP is: " + pin if hash: msg = msg + u". " + hash sms = send_sms([mobile], msg, success_msg=False) status = "fail" if sms and isinstance(sms, list) and mobile in sms: status = "success" update_http_response({"status": status, "mobile": mobile})
def verify_otp_deprecated(): medium = frappe.local.form_dict.medium or "sms" mobile = frappe.local.form_dict.mobile email = frappe.local.form_dict.email pin = frappe.local.form_dict.pin login = cint(frappe.local.form_dict.loginToUser or "0") r = verify_otp(medium=medium, medium_id=mobile or email, otp=pin, login_to_user=login) # Response Compatibility if r.status == "no__for_mobile": r.status = "no_pin_for_mobile" elif r.status == "invalid_otp": r.status = "invalid_pin" update_http_response(r)
def get_doc(doctype, name): """ Builds the response to be sent back Fills in with doc data """ try: doc = frappe.get_doc(doctype, name) if not doc.has_permission("read"): raise frappe.PermissionError update_http_response({"data": doc.as_dict(), "status": "success"}) except Exception as ex: update_http_response({ "data": "failed", "status": "failed", "exception": ex, "traceback": traceback.format_exc() })
def get_report(): req = frappe._dict(get_request_body(as_json=True)) if not req.get("report"): frappe.throw("No report specified") report = frappe.get_doc("Report", req.report) out = {} if report.report_type in ["Query Report", "Script Report"]: out = query_report(req.report, req.filters, req.user) elif report.report_type in ["Report Builder"]: out = {"report_type": "Report Builder"} else: frappe.throw("Invalid report type") out["columns"] = objectify_columns(out.get("columns")) out["result"] = array_result(out.get("columns"), out.get("result")) update_http_response(out)
def delete_doc(doctype, name): try: # TODO # Decide if on delete doc = frappe.get_doc(doctype, name) # check_submittable(doc, "cancel") doc.delete() frappe.db.commit() update_http_response({"status": "success"}) except Exception as ex: frappe.db.rollback() update_http_response({ "data": "failed", "status": "failed", "exception": ex, "traceback": traceback.format_exc() })
def verify_sms_pin(): mobile = frappe.local.form_dict.mobile pin = frappe.local.form_dict.pin login = cint(frappe.local.form_dict.loginToUser or "0") if not mobile: frappe.throw("No Mobile Number") verify_pin = frappe.safe_decode(frappe.cache().get("sms:" + mobile)) user = get_linked_user(mobile) if user: # try to get from User pin_from_db = frappe.db.get_value("User", user, "renovation_sms_pin") if (not pin_from_db or len(pin_from_db) < 2) and verify_pin: frappe.db.set_value("User", user, "renovation_sms_pin", verify_pin) elif pin_from_db != verify_pin: # preference for db pin frappe.cache().set("sms:" + mobile, pin) verify_pin = pin_from_db out = "no_pin_for_mobile" if login: out = "no_linked_user" if verify_pin: out = "invalid_pin" if verify_pin and pin == verify_pin: out = "verified" if login == 1: if user: l = LoginManager() l.login_as(user) l.resume = False l.run_trigger('on_session_creation') else: out = "user_not_found" update_http_response({"status": out, "mobile": mobile})
def put_doc(doctype, name): """ DELETE Existing, PUT UP NEW ---------------- Submitted -> Cancelled Amend -> New DOC Return the new name renovation points to -> SINV-00001-1 """ try: doc = frappe.get_doc(doctype, name) if frappe.get_meta(doctype).is_submittable and 1 == 2: check_submittable(doc, "cancel") new_doc = frappe.copy_doc(doc) new_doc.update(get_request_body_doc()) new_doc.amended_from = doc.name new_doc.status = "Draft" new_doc.insert() check_submittable(new_doc, "submit") doc = new_doc else: doc.update(get_request_body_doc()) # mandatory call before saving for new __islocal docs set_local_name(doc) doc.save() frappe.db.commit() update_http_response({"data": doc.as_dict(), "status": "success"}) except Exception as ex: frappe.db.rollback() update_http_response({ "data": "failed", "status": "failed", "exception": ex, "traceback": traceback.format_exc() })
def generate_otp_deprecated(): medium = frappe.local.form_dict.medium or "sms" mobile = frappe.local.form_dict.mobile email = frappe.local.form_dict.email sms_hash = frappe.local.form_dict.hash update_http_response(generate_otp(medium, mobile or email, sms_hash))