Skip to content

Commit

Permalink
feat: error message and validation fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
AshishBarvaliya committed Jan 11, 2024
1 parent e77e0ab commit 7654231
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# For license information, please see license.txt

import frappe
from frappe import _
from frappe.model.document import Document
import base64
import requests
Expand Down Expand Up @@ -53,7 +54,7 @@ def ask_gpt(self, prompt):
)
return chat_completion.choices[0].message.content.strip()
except Exception as e:
frappe.throw("Please enter a valid OpenAI API key in AutoInscribe Settings")
frappe.throw(_("Please enter a valid OpenAI API key in AutoInscribe Settings"), title=_("Error"))

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'''
Expand All @@ -65,15 +66,15 @@ def extract_text_from_img(self, img_url):
token_uri = self.get_vision_token_uri()

if not project_id or not private_key or not client_email or not token_uri:
return frappe.throw("Missing required fields in AutoInscribe Settings")
return frappe.throw(_("Missing required fields in AutoInscribe Settings"), title=_("Error"))

response = requests.get(img_url)

if response.status_code != 200:
if response.status_code == 403:
return frappe.throw("You don't have permission to access this file. Make sure you upload file with public access.")
return frappe.throw(_("You don't have permission to access this file. Make sure you upload file with public access."), title=_("Error"))
else:
return frappe.throw("Failed to fetch image from URL")
return frappe.throw(_("Failed to fetch image from URL"), title=_("Error"))

try:
credentials = service_account.Credentials.from_service_account_info({
Expand All @@ -95,7 +96,7 @@ def extract_text_from_img(self, img_url):
response = client.text_detection(image=image)
texts = response.text_annotations
except Exception as e:
return frappe.throw("Invalid Google Vision credentials. Please check your AutoInscribe Settings and try again")
return frappe.throw(_("Invalid Google Vision credentials. Please check your AutoInscribe Settings and try again"), title=_("Error"))

# Extracting detected text
if texts:
Expand All @@ -104,7 +105,7 @@ def extract_text_from_img(self, img_url):
reply = self.ask_gpt(prompt)
return reply
else:
return frappe.throw("No information extracted from image. Please try again with a different image")
return frappe.throw(_("No information extracted from image. Please try again with a different image"), title=_("Error"))

def create_address(self, address):
'''Given an address string, extract city, state, postal_code, country and create an address if country exists & return the inserted doc. Return None otherwise.'''
Expand Down Expand Up @@ -149,9 +150,3 @@ def create_address(address):
doc = frappe.get_single("AutoInscribe Integration")
return doc.create_address(address)


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 and not contact.last_name):
contact.cancel()
5 changes: 0 additions & 5 deletions autoinscribe/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,6 @@
# }
# }

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

# Scheduled Tasks
# ---------------
Expand Down
24 changes: 8 additions & 16 deletions autoinscribe/public/js/contact.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
$(document).on('change', 'input[type="file"]', function() {
$(".btn.btn-secondary.btn-sm.btn-modal-secondary").click()
$(document).on("change", 'input[type="file"]', function () {
$(".btn.btn-secondary.btn-sm.btn-modal-secondary").click();
});

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 @@ -92,19 +89,14 @@ 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
validate(frm) {
if (frm.selected_doc.custom_upload_image && !frm.selected_doc.first_name) {
frappe.validated = false;
} else {
frappe.validated = true;
}
}
},
});

0 comments on commit 7654231

Please sign in to comment.