def _filter_select_dateds(context: Context, dateds: Iterable[Dated], date: Optional[Datey]) -> Iterator[Dated]: if date is None: date = context.resolve_or_missing('today') return filter( lambda dated: dated.date is None or dated.date.comparable and dated. date in date, dateds)
def _filter_url(context: Context, resource: Any, media_type: Optional[str] = None, locale: Optional[str] = None, **kwargs) -> str: media_type = 'text/html' if media_type is None else media_type locale = locale if locale else context.resolve_or_missing('locale') return context.environment.app.localized_url_generator.generate( resource, media_type, locale=locale, **kwargs)
def _filter_json(context: Context, data: Any, indent: Optional[int] = None) -> str: """ Converts a value to a JSON string. """ return stdjson.dumps(data, indent=indent, cls=JSONEncoder.get_factory( context.environment.app, context.resolve_or_missing('locale')))
def _filter_select_localizeds( context: Context, localizeds: Iterable[Localized], include_unspecified: bool = False) -> Iterable[Localized]: locale = context.resolve_or_missing('locale') for localized in localizeds: if include_unspecified and localized.locale in { None, 'mis', 'mul', 'und', 'zxx' }: yield localized if localized.locale is not None and negotiate_locale( locale, [localized.locale]) is not None: yield localized
def _filter_sort_localizeds(context: Context, localizeds: Iterable[Localized], localized_attribute: str, sort_attribute: str) -> Iterable[Localized]: locale = context.resolve_or_missing('locale') get_localized_attr = make_attrgetter(context.environment, localized_attribute) get_sort_attr = make_attrgetter(context.environment, sort_attribute) def _get_sort_key(x): return get_sort_attr( negotiate_localizeds(locale, get_localized_attr(x))) return sorted(localizeds, key=_get_sort_key)
def _filter_format_date(context: Context, date: Datey) -> str: locale = context.resolve_or_missing('locale') return format_datey(date, locale)
def _filter_negotiate_localizeds( context: Context, localizeds: Iterable[Localized]) -> Optional[Localized]: locale = context.resolve_or_missing('locale') return negotiate_localizeds(locale, list(localizeds))