def choices_units(units): """ populate form multi-select choices from Units entries """ choices = OrderedDict() for each_unit, each_info in UNITS.items(): if each_info['name']: value = '{unit}'.format( unit=each_unit) unit = '' if each_info['unit']: unit = ' ({})'.format(each_info['unit']) display = '{name}{unit}'.format( name=each_info['name'], unit=unit) choices.update({value: display}) for each_unit in units: value = '{unit}'.format( unit=each_unit.name_safe) display = '{name} ({unit})'.format( name=each_unit.name, unit=each_unit.unit) choices.update({value: display}) # Sort dictionary by keys sorted_keys = sorted(list(choices), key=lambda s: s.casefold()) sorted_dict_choices = OrderedDict() for each_key in sorted_keys: sorted_dict_choices[each_key] = choices[each_key] return sorted_dict_choices
def choices_units(units): """ populate form multi-select choices from Units entries """ choices = [] for each_unit, each_info in UNITS.items(): if each_info['name']: value = '{unit}'.format( unit=each_unit) unit = '' if each_info['unit']: unit = ' ({})'.format(each_info['unit']) display = '{name}{unit}'.format( name=each_info['name'], unit=unit) choices.append({'value': value, 'item': display}) for each_unit in units: value = '{unit}'.format( unit=each_unit.name_safe) display = '{name} ({unit})'.format( name=each_unit.name, unit=each_unit.unit) choices.append({'value': value, 'item': display}) choices.sort(key=lambda item: item.get("item")) return choices
def add_custom_units(units): return_units = UNITS.copy() for each_unit in units: return_units.update( {each_unit.name_safe: { 'unit': each_unit.unit, 'name': each_unit.name}}) # Sort dictionary by keys, ignoring case sorted_keys = sorted(list(return_units), key=lambda s: s.casefold()) sorted_dict_units = OrderedDict() for each_key in sorted_keys: sorted_dict_units[each_key] = return_units[each_key] return sorted_dict_units