示例#1
0
    async def test_empty(self):
        with TemporaryDirectory() as output_directory_path:
            configuration = Configuration(
                output_directory_path, 'https://example.com')
            configuration.locales['en-US'] = LocaleConfiguration('en-US', 'en')
            configuration.locales['nl-NL'] = LocaleConfiguration('nl-NL', 'nl')
            async with App(configuration) as app:
                indexed = [item for item in Index(app).build()]

        self.assertEquals([], indexed)
示例#2
0
 def _init_globals(self) -> None:
     self.globals['app'] = self.app
     self.globals['locale'] = self.app.locale
     today = datetime.date.today()
     self.globals['today'] = Date(today.year, today.month, today.day)
     self.globals['extensions'] = _Extensions(self.app.extensions)
     self.globals['citer'] = _Citer()
     self.globals['search_index'] = lambda: Index(self.app).build()
     self.globals['html_providers'] = list([
         extension for extension in self.app.extensions.values()
         if isinstance(extension, HtmlProvider)
     ])
     self.globals['path'] = os.path
示例#3
0
文件: jinja2.py 项目: patlx/betty
 def _init_globals(self) -> None:
     self.globals['site'] = self.site
     self.globals['locale'] = self.site.locale
     today = datetime.date.today()
     self.globals['today'] = Date(today.year, today.month, today.day)
     self.globals['plugins'] = _Plugins(self.site.plugins)
     self.globals['citer'] = _Citer()
     self.globals['search_index'] = lambda: Index(self.site).build()
     self.globals['html_providers'] = list([
         plugin for plugin in self.site.plugins.values()
         if isinstance(plugin, HtmlProvider)
     ])
     self.globals['path'] = os.path
示例#4
0
    async def test_person_without_names(self):
        person_id = 'P1'
        person = Person(person_id)

        with TemporaryDirectory() as output_directory_path:
            configuration = Configuration(
                output_directory_path, 'https://example.com')
            configuration.locales['en-US'] = LocaleConfiguration('en-US', 'en')
            configuration.locales['nl-NL'] = LocaleConfiguration('nl-NL', 'nl')
            async with App(configuration) as app:
                app.ancestry.people[person_id] = person
                indexed = [item for item in Index(app).build()]

        self.assertEquals([], indexed)
示例#5
0
    async def test_file_without_description(self):
        file_id = 'F1'
        file = File(file_id, __file__)

        with TemporaryDirectory() as output_directory_path:
            configuration = Configuration(
                output_directory_path, 'https://example.com')
            configuration.locales['en-US'] = LocaleConfiguration('en-US', 'en')
            configuration.locales['nl-NL'] = LocaleConfiguration('nl-NL', 'nl')
            async with App(configuration) as app:
                app.ancestry.files[file_id] = file
                indexed = [item for item in Index(app).build()]

        self.assertEquals([], indexed)
示例#6
0
    async def test_place(self, expected: str, locale: str):
        place_id = 'P1'
        place = Place(place_id, [PlaceName('Netherlands', 'en'), PlaceName('Nederland', 'nl')])

        with TemporaryDirectory() as output_directory_path:
            configuration = Configuration(
                output_directory_path, 'https://example.com')
            configuration.locales['en-US'] = LocaleConfiguration('en-US', 'en')
            configuration.locales['nl-NL'] = LocaleConfiguration('nl-NL', 'nl')
            async with App(configuration).with_locale(locale) as app:
                app.ancestry.places[place_id] = place
                indexed = [item for item in Index(app).build()]

        self.assertEquals('netherlands nederland', indexed[0]['text'])
        self.assertIn(expected, indexed[0]['result'])
示例#7
0
    async def test_file(self, expected: str, locale: str):
        file_id = 'F1'
        file = File(file_id, __file__)
        file.description = '"file" is Dutch for "traffic jam"'

        with TemporaryDirectory() as output_directory_path:
            configuration = Configuration(
                output_directory_path, 'https://example.com')
            configuration.locales['en-US'] = LocaleConfiguration('en-US', 'en')
            configuration.locales['nl-NL'] = LocaleConfiguration('nl-NL', 'nl')
            async with App(configuration).with_locale(locale) as app:
                app.ancestry.files[file_id] = file
                indexed = [item for item in Index(app).build()]

        self.assertEquals('"file" is dutch for "traffic jam"', indexed[0]['text'])
        self.assertIn(expected, indexed[0]['result'])
示例#8
0
    async def test_person_with_affiliation_name(self, expected: str, locale: str):
        person_id = 'P1'
        affiliation_name = 'Doughnut'
        person = Person(person_id)
        person.names.append(PersonName(None, affiliation_name))

        with TemporaryDirectory() as output_directory_path:
            configuration = Configuration(
                output_directory_path, 'https://example.com')
            configuration.locales['en-US'] = LocaleConfiguration('en-US', 'en')
            configuration.locales['nl-NL'] = LocaleConfiguration('nl-NL', 'nl')
            async with App(configuration).with_locale(locale) as app:
                app.ancestry.people[person_id] = person
                indexed = [item for item in Index(app).build()]

        self.assertEquals('doughnut', indexed[0]['text'])
        self.assertIn(expected, indexed[0]['result'])
示例#9
0
    async def test_private_person(self):
        person_id = 'P1'
        individual_name = 'Jane'
        person = Person(person_id)
        person.names.append(PersonName(individual_name))
        person.private = True

        with TemporaryDirectory() as output_directory_path:
            configuration = Configuration(
                output_directory_path, 'https://example.com')
            configuration.locales['en-US'] = LocaleConfiguration('en-US', 'en')
            configuration.locales['nl-NL'] = LocaleConfiguration('nl-NL', 'nl')
            async with App(configuration) as app:
                app.ancestry.people[person_id] = person
                indexed = [item for item in Index(app).build()]

        self.assertEquals([], indexed)
示例#10
0
    async def test_person_with_individual_name(self, expected: str,
                                               locale: str):
        person_id = 'P1'
        individual_name = 'Jane'
        person = Person(person_id)
        PersonName(person, individual_name)

        with TemporaryDirectory() as output_directory_path:
            configuration = Configuration(output_directory_path,
                                          'https://example.com')
            configuration.locales.replace([
                LocaleConfiguration('en-US', 'en'),
                LocaleConfiguration('nl-NL', 'nl'),
            ])
            async with App(configuration).with_locale(locale) as app:
                app.ancestry.entities.append(person)
                indexed = [item for item in Index(app).build()]

        self.assertEquals('jane', indexed[0]['text'])
        self.assertIn(expected, indexed[0]['result'])
示例#11
0
    def __init__(self, site: Site):
        template_directory_paths = [
            join(path, 'templates') for path in site.assets.paths
        ]

        Environment.__init__(
            self,
            enable_async=True,
            loader=FileSystemLoader(template_directory_paths),
            undefined=StrictUndefined,
            autoescape=select_autoescape(['html']),
            trim_blocks=True,
            extensions=[
                'jinja2.ext.do',
                'jinja2.ext.i18n',
            ],
        )

        self.site = site

        if site.configuration.mode == 'development':
            self.add_extension('jinja2.ext.debug')

        def _gettext(*args, **kwargs):
            return gettext(*args, **kwargs)

        def _ngettext(*args, **kwargs):
            return ngettext(*args, **kwargs)

        self.install_gettext_callables(_gettext, _ngettext)
        self.policies['ext.i18n.trimmed'] = True
        self.globals['site'] = site
        self.globals['locale'] = site.locale
        today = datetime.date.today()
        self.globals['today'] = Date(today.year, today.month, today.day)
        self.globals['plugins'] = _Plugins(site.plugins)
        self.globals['urlparse'] = urlparse
        self.filters['map'] = _filter_map
        self.filters['flatten'] = _filter_flatten
        self.filters['walk'] = _filter_walk
        self.filters['selectwhile'] = _filter_selectwhile
        self.filters['locale_get_data'] = lambda locale: Locale.parse(
            locale, '-')
        self.filters['negotiate_localizeds'] = _filter_negotiate_localizeds
        self.filters['sort_localizeds'] = _filter_sort_localizeds
        self.filters['select_localizeds'] = _filter_select_localizeds
        self.filters['negotiate_dateds'] = _filter_negotiate_dateds
        self.filters['select_dateds'] = _filter_select_dateds

        # A filter to convert any value to JSON.
        @contextfilter
        def _filter_json(context, data, indent=None):
            return stdjson.dumps(data,
                                 indent=indent,
                                 cls=JSONEncoder.get_factory(
                                     site,
                                     resolve_or_missing(context, 'locale')))

        self.filters['json'] = _filter_json

        # Override Jinja2's built-in JSON filter, which escapes the JSON for use in HTML, to use Betty's own encoder.
        @contextfilter
        def _filter_tojson(context, data, indent=None):
            return htmlsafe_json_dumps(data,
                                       indent=indent,
                                       dumper=lambda *args, **kwargs:
                                       _filter_json(context, *args, **kwargs))

        self.filters['tojson'] = _filter_tojson
        self.tests['resource'] = lambda x: isinstance(x, Resource)

        def _build_test_resource_type(resource_type: Type[Resource]):
            def _test_resource(x):
                return isinstance(x, resource_type)

            return _test_resource

        for resource_type in RESOURCE_TYPES:
            self.tests[
                '%s_resource' %
                resource_type.resource_type_name] = _build_test_resource_type(
                    resource_type)
        self.tests['identifiable'] = lambda x: isinstance(x, Identifiable)
        self.tests['has_links'] = lambda x: isinstance(x, HasLinks)
        self.tests['has_files'] = lambda x: isinstance(x, HasFiles)
        self.tests['startswith'] = str.startswith
        self.tests['subject_role'] = lambda x: isinstance(x, Subject)
        self.tests['witness_role'] = lambda x: isinstance(x, Witness)
        self.tests['date_range'] = lambda x: isinstance(x, DateRange)
        self.filters['paragraphs'] = _filter_paragraphs

        @contextfilter
        def _filter_format_date(context, date: Datey):
            locale = resolve_or_missing(context, 'locale')
            return format_datey(date, locale)

        self.filters['format_date'] = _filter_format_date
        self.filters['format_degrees'] = _filter_format_degrees
        self.globals['citer'] = _Citer()

        @contextfilter
        def _filter_url(context,
                        resource,
                        media_type=None,
                        locale=None,
                        **kwargs):
            media_type = media_type if media_type else 'text/html'
            locale = locale if locale else resolve_or_missing(
                context, 'locale')
            return site.localized_url_generator.generate(resource,
                                                         media_type,
                                                         locale=locale,
                                                         **kwargs)

        self.filters['url'] = _filter_url
        self.filters['static_url'] = site.static_url_generator.generate
        self.filters['file'] = lambda *args: _filter_file(site, *args)
        self.filters['image'] = lambda *args, **kwargs: _filter_image(
            site, *args, **kwargs)
        self.globals['search_index'] = lambda: Index(site).build()
        self.globals['html_providers'] = list([
            plugin for plugin in site.plugins.values()
            if isinstance(plugin, HtmlProvider)
        ])
        self.globals['path'] = os.path
        for plugin in site.plugins.values():
            if isinstance(plugin, Jinja2Provider):
                self.globals.update(plugin.globals)
                self.filters.update(plugin.filters)