Skip to content

Commit

Permalink
feat: add Nomatim as geolocation provider
Browse files Browse the repository at this point in the history
  • Loading branch information
barredterra committed May 19, 2024
1 parent 97db7aa commit 3933725
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"field_order": [
"enable_address_autocompletion",
"provider",
"base_url",
"api_key"
],
"fields": [
Expand All @@ -14,24 +15,33 @@
"fieldtype": "Select",
"label": "Provider",
"mandatory_depends_on": "enable_address_autocompletion",
"options": "Geoapify"
"options": "Geoapify\nNomatim"
},
{
"depends_on": "eval: doc.provider === \"Geoapify\"",
"fieldname": "api_key",
"fieldtype": "Password",
"label": "API Key",
"mandatory_depends_on": "enable_address_autocompletion"
"mandatory_depends_on": "eval: doc.enable_address_autocompletion && doc.provider === \"Geoapify\""
},
{
"default": "0",
"fieldname": "enable_address_autocompletion",
"fieldtype": "Check",
"label": "Enable Address Autocompletion"
},
{
"depends_on": "eval: doc.provider === \"Nomatim\"",
"fieldname": "base_url",
"fieldtype": "Data",
"label": "Base URL",
"mandatory_depends_on": "eval: doc.provider === \"Nomatim\" && doc.enable_address_autocompletion",
"options": "URL"
}
],
"issingle": 1,
"links": [],
"modified": "2024-05-19 20:43:49.711209",
"modified": "2024-05-19 22:00:01.118978",
"modified_by": "Administrator",
"module": "Integrations",
"name": "Geolocation Settings",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
import frappe
from frappe import _
from frappe.model.document import Document
from frappe.utils import get_url

from .providers.geoapify import Geoapify
from .providers.nomatim import Nomatim


class GeolocationSettings(Document):
Expand All @@ -18,8 +20,9 @@ class GeolocationSettings(Document):
from frappe.types import DF

api_key: DF.Password | None
base_url: DF.Data | None
enable_address_autocompletion: DF.Check
provider: DF.Literal["Geoapify"]
provider: DF.Literal["Geoapify", "Nomatim"]
# end: auto-generated types

pass
Expand All @@ -36,6 +39,12 @@ def autocomplete(txt: str) -> list[dict]:

if settings.provider == "Geoapify":
provider = Geoapify(settings.get_password("api_key"), frappe.local.lang)
elif settings.provider == "Nomatim":
provider = Nomatim(
base_url=settings.base_url,
referer=get_url(),
lang=frappe.local.lang,
)
else:
frappe.throw(_("This geolocation provider is not supported yet."))

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import json

import requests

import frappe


class Nomatim:
def __init__(self, base_url: str, referer: str, lang: str | None = None):
self.lang = lang
self.referer = referer
self.base_url = base_url

def autocomplete(self, query: str):
params = {
"q": query,
"format": "json",
"limit": 5,
"addressdetails": 1,
"accept-language": self.lang,
"layer": "address",
}
response = requests.get(
f"{self.base_url}/search",
params=params,
headers={"Referer": self.referer},
)
response.raise_for_status()

results = response.json()
for result in results:
if "address" not in result:
continue

address = result["address"]
yield {
"label": result["display_name"],
"value": json.dumps(
{
"address_line1": f'{address.get("road")} {address.get("house_number", "")}'.strip(),
"city": address.get("city") or address.get("town") or address.get("village"),
"state": address.get("state"),
"pincode": address.get("postcode"),
"country": frappe.db.get_value("Country", {"code": address.get("country_code")}),
}
),
}

0 comments on commit 3933725

Please sign in to comment.