From fd6d77783bf5e3a72497e4c8de97428e03518a38 Mon Sep 17 00:00:00 2001 From: Anand Doshi Date: Tue, 19 Aug 2014 11:48:39 +0530 Subject: [PATCH] [fix] Preserve sequence of values when performing unique for autosuggest --- frappe/utils/data.py | 7 +++++++ frappe/widgets/search.py | 4 ++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/frappe/utils/data.py b/frappe/utils/data.py index 9c2f3e7cd7..1d053f804a 100644 --- a/frappe/utils/data.py +++ b/frappe/utils/data.py @@ -585,3 +585,10 @@ def quote_urls(html): return re.sub('(href|src){1}([\s]*=[\s]*[\'"]?)((?:http)[^\'">]+)([\'"]?)', _quote_url, html) +def unique(seq): + """use this instead of list(set()) to preserve order of the original list. + Thanks to Stackoverflow: http://stackoverflow.com/questions/480214/how-do-you-remove-duplicates-from-a-list-in-python-whilst-preserving-order""" + + seen = set() + seen_add = seen.add + return [ x for x in seq if not (x in seen or seen_add(x)) ] diff --git a/frappe/widgets/search.py b/frappe/widgets/search.py index ec92920da9..1d8b92c564 100644 --- a/frappe/widgets/search.py +++ b/frappe/widgets/search.py @@ -5,7 +5,7 @@ from __future__ import unicode_literals import frappe import frappe.widgets.reportview -from frappe.utils import cstr +from frappe.utils import cstr, unique # this is called by the Link Field @frappe.whitelist() @@ -99,7 +99,7 @@ def get_std_fields_list(meta, key): def build_for_autosuggest(res): results = [] for r in res: - out = {"value": r[0], "description": ", ".join(list(set(cstr(d) for d in r[1:])))} + out = {"value": r[0], "description": ", ".join(unique(cstr(d) for d in r)[1:])} results.append(out) return results