gigafibre-fsm/apps/dispatch/scripts/fix_search3.py
louispaulb 7da22ff132 merge: import dispatch-app into apps/dispatch/ (17 commits preserved)
Integrates the Dispatch PWA (Vue/Quasar) into the gigafibre-fsm monorepo.
Full git history accessible via `git log -- apps/dispatch/`.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-28 08:08:51 -04:00

44 lines
1.3 KiB
Python

import frappe, os
os.chdir('/home/frappe/frappe-bench')
frappe.init('frontend', sites_path='/home/frappe/frappe-bench/sites')
frappe.connect()
ss = frappe.get_doc('Server Script', 'Address Autocomplete')
ss.script = """
query = frappe.form_dict.get("q", "")
if not query or len(query) < 3:
frappe.response["results"] = []
else:
words = query.strip().split()
conditions = []
params = {}
idx = 0
for w in words:
k = "w" + str(idx)
params[k] = "%" + w + "%"
conditions.append(
"(f_unaccent(address_full) ILIKE f_unaccent(%%(%s)s) "
"OR f_unaccent(rue) ILIKE f_unaccent(%%(%s)s) "
"OR f_unaccent(ville) ILIKE f_unaccent(%%(%s)s) "
"OR numero ILIKE %%(%s)s)" % (k, k, k, k)
)
idx = idx + 1
where = " AND ".join(conditions)
sql = ("SELECT address_full, ville, code_postal, latitude, longitude "
"FROM rqa_addresses "
"WHERE " + where + " "
"ORDER BY "
"CASE WHEN code_postal LIKE 'J0L%%%%' THEN 0 WHEN code_postal LIKE 'J0S%%%%' THEN 1 ELSE 2 END, "
"length(address_full) "
"LIMIT 10")
results = frappe.db.sql(sql, params, as_dict=True)
frappe.response["results"] = results
"""
ss.save(ignore_permissions=True)
frappe.db.commit()
print('Fixed: no .format(), using % operator')
frappe.destroy()