Skip to content

Commit

Permalink
refactor: moved client script code and api code
Browse files Browse the repository at this point in the history
  • Loading branch information
AshishBarvaliya committed Jan 9, 2024
1 parent 3597c0a commit 8211a26
Show file tree
Hide file tree
Showing 5 changed files with 202 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,16 @@ def create_address(self, address):
doc.insert()
return doc
else:
return None
return None

@frappe.whitelist()
def extract_text_from_img(img_url):
doc = frappe.get_single("AutoInscribe Integration")
return doc.extract_text_from_img(img_url)


@frappe.whitelist()
def create_address(address):
doc = frappe.get_single("AutoInscribe Integration")
return doc.create_address(address)

4 changes: 4 additions & 0 deletions autoinscribe/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@

# include js in doctype views
# doctype_js = {"doctype" : "public/js/doctype.js"}

doctype_js = {"Lead" : "public/js/lead.js", "Contact": "public/js/contact.js"}


# doctype_list_js = {"doctype" : "public/js/doctype_list.js"}
# doctype_tree_js = {"doctype" : "public/js/doctype_tree.js"}
# doctype_calendar_js = {"doctype" : "public/js/doctype_calendar.js"}
Expand Down
15 changes: 0 additions & 15 deletions autoinscribe/inscribe/api.py

This file was deleted.

88 changes: 88 additions & 0 deletions autoinscribe/public/js/contact.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
frappe.ui.form.on("Contact", {
custom_upload_image(frm) {
if (frm.selected_doc.custom_upload_image) {
frappe.call({
method:
"autoinscribe.autoinscribe.doctype.autoinscribe_integration.autoinscribe_integration.extract_text_from_img",
args: {
img_url:
window.location.href.split("/app")[0] +
frm.selected_doc.custom_upload_image,
},
callback(res) {
const resArr = res.message.split("\n");

const first_name = resArr[0].split(":")[1].trim();
const middle_name = resArr[1].split(":")[1].trim();
const last_name = resArr[2].split(":")[1].trim();
first_name !== "NULL" && frm.set_value("first_name", first_name);
middle_name !== "NULL" && frm.set_value("middle_name", middle_name);
last_name !== "NULL" && frm.set_value("last_name", last_name);
const company_name = resArr[8].split(":")[1].trim();
company_name !== "NULL" &&
frm.set_value("company_name", company_name);
const designation = resArr[5].split(":")[1].trim();
designation !== "NULL" && frm.set_value("designation", designation);
const email_ids = resArr[7].split(":")[1].trim().split(",");
email_ids.forEach((email) => {
email.trim() !== "NULL" &&
frm.add_child("email_ids", {
email_id: email.trim(),
});
});
frm.refresh_field("email_ids");
const contact_numbers = resArr[6].split(":")[1].trim().split(",");
contact_numbers.forEach((phone_no) => {
phone_no.trim() !== "NULL" &&
frm.add_child("phone_nos", {
phone: phone_no.trim(),
});
});
frm.refresh_field("phone_nos");

let gender = resArr[3].split(":")[1].trim();
gender = gender.charAt(0).toUpperCase() + gender.slice(1);
frappe.db.exists("Gender", gender).then((gender_exists) => {
if (gender_exists) {
frm.set_value("gender", gender);
}
});
frm.refresh_field("gender");
let salutation = resArr[4].split(":")[1].trim();
salutation =
salutation.charAt(0).toUpperCase() +
salutation.slice(
1,
salutation[salutation.length - 1] === "."
? salutation.length - 1
: salutation.length
);
frappe.db
.exists("Salutation", salutation)
.then((salutation_exists) => {
if (salutation_exists) {
frm.set_value("salutation", salutation);
}
});
frm.refresh_field("salutation");

const address = resArr[10].split(":")[1].trim();
if (address !== "NULL") {
frappe.call({
method:
"autoinscribe.autoinscribe.doctype.autoinscribe_integration.autoinscribe_integration.create_address",
args: {
address: address,
},
callback(res) {
if (res.message) {
frm.set_value("address", `${address}-Office`);
}
},
});
}
},
});
}
},
});
97 changes: 97 additions & 0 deletions autoinscribe/public/js/lead.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
frappe.ui.form.on("Lead", {
custom_upload_business_card(frm) {
if (frm.selected_doc.custom_upload_business_card) {
frappe.call({
method:
"autoinscribe.autoinscribe.doctype.autoinscribe_integration.autoinscribe_integration.extract_text_from_img",
args: {
img_url:
window.location.href.split("/app")[0] +
frm.selected_doc.custom_upload_business_card,
},
callback(res) {
const resArr = res.message.split("\n");

const first_name = resArr[0].split(":")[1].trim();
const middle_name = resArr[1].split(":")[1].trim();
const last_name = resArr[2].split(":")[1].trim();
first_name !== "NULL" && frm.set_value("first_name", first_name);
middle_name !== "NULL" && frm.set_value("middle_name", middle_name);
last_name !== "NULL" && frm.set_value("last_name", last_name);
const company_name = resArr[8].split(":")[1].trim();
company_name !== "NULL" &&
frm.set_value("company_name", company_name);
const designation = resArr[5].split(":")[1].trim();
designation !== "NULL" && frm.set_value("job_title", designation);
const email_ids = resArr[7].split(":")[1].trim().split(",");
frm.set_value("email_id", email_ids[0] || "");

const mobile_no = resArr[11].split(":")[1].trim();
mobile_no !== "NULL" && frm.set_value("mobile_no", mobile_no);
const phone = resArr[12].split(":")[1].trim();
phone !== "NULL" && frm.set_value("phone", phone);

const possibleGenders = [
"Male",
"Female",
"Prefer not to say",
"Non-Conforming",
"Genderqueer",
"Transgender",
"Other",
];
let gender = resArr[3].split(":")[1].trim();
gender = gender.charAt(0).toUpperCase() + gender.slice(1);
gender !== "NULL" &&
possibleGenders.includes(gender) &&
frm.set_value("gender", gender);
frm.refresh_field("gender");

const possibleSalutations = [
"Mr",
"Ms",
"Mx",
"Dr",
"Mrs",
"Madam",
"Miss",
"Master",
"Prof",
];
let salutation = resArr[4].split(":")[1].trim();
salutation =
salutation.charAt(0).toUpperCase() +
salutation.slice(
1,
salutation[salutation.length - 1] === "."
? salutation.length - 1
: salutation.length
);
salutation !== "NULL" &&
possibleSalutations.includes(salutation) &&
frm.set_value("salutation", salutation);
frm.refresh_field("salutation");

let website = resArr[9].split(":")[1].trim();
frm.set_value("website", website);

const city = resArr[13].split(":")[1].trim();
city !== "NULL" && frm.set_value("city", city);

const state = resArr[14].split(":")[1].trim();
state !== "NULL" && frm.set_value("state", state);

const country = resArr[15].split(":")[1].trim();
frappe.db.exists("Country", country).then((country_exists) => {
if (country_exists) {
frm.set_value("country", country);
} else {
frm.set_value("country", "");
}
});
frm.refresh_field("country");
},
});
}
},
});

0 comments on commit 8211a26

Please sign in to comment.