feat: refactor GMaps

This commit is contained in:
Himanshu Warekar 2019-06-29 14:23:33 +05:30
parent b6e8af06fe
commit 15633907be
5 changed files with 56 additions and 17 deletions

View file

@ -94,8 +94,8 @@ def get_data():
},
{
"type": "doctype",
"name": "Google Maps Settings",
"description": _("Google Maps integration"),
"name": "Google Maps",
"description": _("Google Maps Integration"),
},
{
"type": "doctype",

View file

@ -1,22 +1,46 @@
{
"creation": "2019-06-28 23:49:38.545917",
"autoname": "format:GM-{entity}",
"creation": "2019-06-29 13:03:45.935466",
"doctype": "DocType",
"editable_grid": 1,
"engine": "InnoDB",
"field_order": [
"enable"
"enabled",
"entity_type",
"address",
"cb_00",
"entity"
],
"fields": [
{
"default": "0",
"description": "Google Maps integration is used to optimize routes.",
"fieldname": "enable",
"fieldname": "enabled",
"fieldtype": "Check",
"label": "Enable"
"label": "Enabled"
},
{
"fieldname": "entity_type",
"fieldtype": "Select",
"label": "Entity Type"
},
{
"fieldname": "address",
"fieldtype": "Link",
"label": "Address",
"options": "Address"
},
{
"fieldname": "cb_00",
"fieldtype": "Column Break"
},
{
"fieldname": "entity",
"fieldtype": "Dynamic Link",
"label": "Entity",
"options": "entity_type"
}
],
"issingle": 1,
"modified": "2019-06-28 23:49:38.545917",
"modified": "2019-06-29 13:23:16.714233",
"modified_by": "Administrator",
"module": "Integrations",
"name": "Google Maps",

View file

@ -8,18 +8,21 @@ from frappe.model.document import Document
class GoogleMaps(Document):
def validate(self):
if self.enable and not frappe.db.get_single_value("Google Settings", "enable"):
frappe.throw(_("Enable Google API in Google Settings."))
if self.enabled:
if not frappe.db.get_single_value("Google Settings", "enable"):
frappe.throw(_("Enable Google Settings for Google Maps Integration."))
if not frappe.db.get_single_value("Google Settings", "api_key"):
frappe.throw(_("Enter API Key for Google Maps Integration in Google Settings."))
def get_client(self):
if not self.enable:
frappe.throw(_("Google Maps integration is not enabled"))
if not self.enabled:
frappe.throw(_("Google Maps Integration is not enabled."))
import googlemaps
try:
client_id = frappe.db.get_single_value("Google Settings", "client_id")
client = googlemaps.Client(key=client_id)
client = googlemaps.Client(key=frappe.db.get_single_value("Google Settings", "api_key"))
except Exception as e:
frappe.throw(e.message)

View file

@ -6,7 +6,8 @@
"enable",
"google_credentials",
"client_id",
"client_secret"
"client_secret",
"api_key"
],
"fields": [
{
@ -32,10 +33,15 @@
"fieldtype": "Password",
"in_list_view": 1,
"label": "Client Secret"
},
{
"fieldname": "api_key",
"fieldtype": "Data",
"label": "API Key"
}
],
"issingle": 1,
"modified": "2019-06-19 15:28:05.957380",
"modified": "2019-06-29 13:26:33.201060",
"modified_by": "Administrator",
"module": "Integrations",
"name": "Google Settings",

View file

@ -0,0 +1,6 @@
import frappe
def execute():
key = frappe.db.get_single_value("Google Maps Settings", "client_key")
frappe.db.set_value("Google Settings", None, "api_key", key)
frappe.db.commit()