Narrative web: Sort problem with places. (#854)

* Narrative web: Sort problem with places.

Fixes #10868.

Places list is not sorted depending on the selected language.

If you start gramps in english or another language then try to generated
a narrative web report in another language, the navigation alphabet is
incorrect. This is true for the place list and the person list.
The problem was related to pyICU.

* Narrative web: some pylint improvement.
This commit is contained in:
Serge Noiraud 2019-08-01 15:31:39 +02:00 committed by Paul Culley
parent 7e7dd68b58
commit 96fcef2a84
2 changed files with 45 additions and 40 deletions

View File

@ -316,6 +316,29 @@ def sort_people(dbase, handle_list, rlocale=glocale):
return sorted_lists return sorted_lists
def sort_places(dbase, handle_list, rlocale=glocale):
"""
will sort the database place
"""
pname_sub = defaultdict(list)
sortnames = {}
for place_handle in handle_list:
place = dbase.get_place_from_handle(place_handle)
pname = _pd.display(dbase, place)
sortnames[place_handle] = pname
pname_sub[pname].append(place_handle)
sorted_lists = []
temp_list = sorted(pname_sub, key=rlocale.sort_key)
for name in temp_list:
if isinstance(name, bytes):
name = name.decode('utf-8')
sorted_lists.append((name, pname_sub[name][0]))
return sorted_lists
def sort_event_types(dbase, event_types, event_handle_list, rlocale): def sort_event_types(dbase, event_types, event_handle_list, rlocale):
""" """
sort a list of event types and their associated event handles sort a list of event types and their associated event handles
@ -493,35 +516,23 @@ def first_letter(string, rlocale=glocale):
# no special case # no special case
return norm_unicode[0].upper() return norm_unicode[0].upper()
try: def primary_difference(prev_key, new_key, rlocale=glocale):
import PyICU # pylint : disable=wrong-import-position """
PRIM_COLL = PyICU.Collator.createInstance(PyICU.Locale(COLLATE_LANG)) The PyICU collation doesn't work if you want to sort in another language
PRIM_COLL.setStrength(PRIM_COLL.PRIMARY) So we use this method to do the work correctly.
def primary_difference(prev_key, new_key, rlocale=glocale): Returns true if there is a primary difference between the two parameters
""" See http://www.gramps-project.org/bugs/view.php?id=2933#c9317 if
Try to use the PyICU collation. letter[i]+'a' < letter[i+1]+'b' and letter[i+1]+'a' < letter[i]+'b' is
""" true then the letters should be grouped together
return PRIM_COLL.compare(prev_key, new_key) != 0 The test characters here must not be any that are used in contractions.
"""
except: return rlocale.sort_key(prev_key + "e") >= \
def primary_difference(prev_key, new_key, rlocale=glocale): rlocale.sort_key(new_key + "f") or \
""" rlocale.sort_key(new_key + "e") >= \
The PyICU collation is not available. rlocale.sort_key(prev_key + "f")
Returns true if there is a primary difference between the two parameters
See http://www.gramps-project.org/bugs/view.php?id=2933#c9317 if
letter[i]+'a' < letter[i+1]+'b' and letter[i+1]+'a' < letter[i]+'b' is
true then the letters should be grouped together
The test characters here must not be any that are used in contractions.
"""
return rlocale.sort_key(prev_key + "e") >= \
rlocale.sort_key(new_key + "f") or \
rlocale.sort_key(new_key + "e") >= \
rlocale.sort_key(prev_key + "f")
def get_first_letters(dbase, handle_list, key, rlocale=glocale): def get_first_letters(dbase, handle_list, key, rlocale=glocale):
""" """
@ -565,12 +576,12 @@ def get_first_letters(dbase, handle_list, key, rlocale=glocale):
index_list.sort(key=rlocale.sort_key) index_list.sort(key=rlocale.sort_key)
first = True first = True
prev_index = None prev_index = None
for key in index_list[:]: #iterate over a slice copy of the list for nkey in index_list[:]: #iterate over a slice copy of the list
if first or primary_difference(prev_index, key, rlocale): if first or primary_difference(prev_index, nkey, rlocale):
first = False first = False
prev_index = key prev_index = nkey
else: else:
index_list.remove(key) index_list.remove(nkey)
# return menu set letters for alphabet_navigation # return menu set letters for alphabet_navigation
return index_list return index_list

View File

@ -64,7 +64,7 @@ from gramps.plugins.webreport.common import (get_first_letters, first_letter,
primary_difference, _KEYPLACE, primary_difference, _KEYPLACE,
get_index_letter, FULLCLEAR, get_index_letter, FULLCLEAR,
MARKER_PATH, OSM_MARKERS, MARKERS, MARKER_PATH, OSM_MARKERS, MARKERS,
html_escape) html_escape, sort_places)
_ = glocale.translation.sgettext _ = glocale.translation.sgettext
LOG = logging.getLogger(".NarrativeWeb") LOG = logging.getLogger(".NarrativeWeb")
@ -181,21 +181,15 @@ class PlacePages(BasePage):
] ]
) )
# bug 9495 : incomplete display of place hierarchy labels handle_list = sort_places(self.r_db, place_handles,
def sort_by_place_name(obj): self.rlocale)
""" sort by lower case place name. """
name = self.report.obj_dict[Place][obj][1]
return name.lower()
handle_list = sorted(place_handles,
key=lambda x: sort_by_place_name(x))
first = True first = True
# begin table body # begin table body
tbody = Html("tbody") tbody = Html("tbody")
table += tbody table += tbody
for place_handle in handle_list: for (dummy_pname, place_handle) in handle_list:
place = self.r_db.get_place_from_handle(place_handle) place = self.r_db.get_place_from_handle(place_handle)
if place: if place:
if place.get_change_time() > ldatec: if place.get_change_time() > ldatec: