Skip to content

Commit

Permalink
feat: fixed error messages display while contact creation & preventin…
Browse files Browse the repository at this point in the history
…g auto save
  • Loading branch information
deepak-redsoftware committed Jan 10, 2024
1 parent 4941139 commit 43de788
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def get_vision_private_key(self):

def ask_gpt(self, prompt):
'''Returns response from OpenAI API given a prompt'''

try:
gpt_client = OpenAI(api_key=self.get_openai_gpt_key())
chat_completion = gpt_client.chat.completions.create(
Expand All @@ -57,6 +57,7 @@ def ask_gpt(self, prompt):

def extract_text_from_img(self, img_url):
'''Extracts and returns first_name, middle_name, last_name, gender, salutation, designation contact_numbers, email_ids, company_name, website, address, mobile_number, phone_number, city, state and country from an image given the image URL'''

texts = None
project_id = self.get_vision_project_id()
private_key = self.get_vision_private_key().strip().replace('\\n', '\n')
Expand Down Expand Up @@ -149,8 +150,8 @@ def create_address(address):
return doc.create_address(address)


def display_contact_error_after_insert(contact, event):
def display_contact_error_before_insert(contact, event):
'''Displays error message (if any) to user after contact insert'''

if contact.custom_upload_image and not contact.first_name:
frappe.msgprint("Please check your AutoInscribe Settings and try again", title="Invalid AutoInscribe Settings", indicator="red")
if contact.custom_upload_image and (not contact.first_name and not contact.last_name):
contact.cancel()
2 changes: 1 addition & 1 deletion autoinscribe/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@

doc_events = {
"Contact": {
"after_insert": "autoinscribe.autoinscribe.doctype.autoinscribe_integration.autoinscribe_integration.display_contact_error_after_insert",
"before_insert": "autoinscribe.autoinscribe.doctype.autoinscribe_integration.autoinscribe_integration.display_contact_error_before_insert",
},
}

Expand Down
15 changes: 15 additions & 0 deletions autoinscribe/public/js/contact.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
let once_refreshed = false
let error_message = ''

frappe.ui.form.on("Contact", {
custom_upload_image(frm) {
if (frm.selected_doc.custom_upload_image) {
Expand Down Expand Up @@ -82,7 +85,19 @@ frappe.ui.form.on("Contact", {
});
}
},

error: function(r) {
console.log(r.exception.split(':')[1].trim())
error_message = r.exception.split(':')[1].trim()
}
});
}
},

refresh(frm) {
if (error_message?.length > 0 && !once_refreshed) {
frappe.throw(error_message, title="Error")
once_refreshed = true
}
}
});

0 comments on commit 43de788

Please sign in to comment.