def _get_metazone_cities(metazone, limit=5): zones = [tz for mz, tz in METAZONE_MAPPING_TO_TZ.items() if mz[0] == metazone] cities = sorted([territory[1] for tz, territory in TIMEZONE_NAMES.items() if tz in zones]) if len(cities) > limit: return ", ".join(map(force_text, cities[:limit])) + ", ..." else: return ", ".join(map(force_text, cities))
def get_timezones_cities(timezones=None, exclude=None): """Returns a list of choices with (timezone code, exemplar city)-pairs, grouped by their territory. Only timezones present in the timezones argument, and not present in the exclude argument, are returned. """ # We require sorting for the groupby timezones = COMMON_TIMEZONES if timezones is None else timezones exclude = exclude if exclude else [] values = sorted(TIMEZONE_NAMES.items(), key=lambda item: orig_str(item[1][0])) result = [] for territory, zones in itertools.groupby(values, lambda item: item[1][0]): items = [(k, v[1]) for k, v in zones if k in timezones and k not in exclude] if items: result.append((territory, items)) return result