Skip to content

Commit

Permalink
Fix a bug where the demo server would not show the correct front page. (
Browse files Browse the repository at this point in the history
  • Loading branch information
bartfeenstra authored Dec 27, 2023
1 parent 935e675 commit 3c27ff6
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 19 deletions.
11 changes: 9 additions & 2 deletions betty/extension/cotton_candy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import logging
import re
from collections.abc import Sequence
from pathlib import Path
from shutil import copy2
from typing import Any, Callable, Iterable, cast, Self
Expand All @@ -24,7 +25,7 @@
from betty.locale import Date, Datey, Str
from betty.model import Entity, UserFacingEntity
from betty.model.ancestry import Event, Person, Presence, is_public
from betty.project import EntityReferenceSequence
from betty.project import EntityReferenceSequence, EntityReference
from betty.serde.dump import minimize, Dump, VoidableDump
from betty.serde.load import AssertionFailed, Fields, Assertions, OptionalField, Asserter

Expand Down Expand Up @@ -86,9 +87,15 @@ class CottonCandyConfiguration(Configuration):
DEFAULT_LINK_INACTIVE_COLOR = '#149988'
DEFAULT_LINK_ACTIVE_COLOR = '#2a615a'

def __init__(self):
def __init__(
self,
*,
featured_entities: Sequence[EntityReference[UserFacingEntity & Entity]] | None = None,
):
super().__init__()
self._featured_entities = EntityReferenceSequence['UserFacingEntity & Entity']()
if featured_entities is not None:
self.featured_entities.append(*featured_entities)
self._featured_entities.react(self)
self._primary_inactive_color = _ColorConfiguration(self.DEFAULT_PRIMARY_INACTIVE_COLOR)
self._primary_inactive_color.react(self)
Expand Down
42 changes: 27 additions & 15 deletions betty/extension/demo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
from betty import load, generate
from betty.app import App
from betty.app.extension import Extension
from betty.extension.cotton_candy import CottonCandyConfiguration
from betty.load import Loader
from betty.locale import Date, DateRange, Str
from betty.model import Entity
from betty.model.ancestry import Place, PlaceName, Person, Presence, Subject, PersonName, Link, Source, Citation, Event, \
Enclosure
from betty.model.event_type import Marriage, Birth, Death
from betty.project import LocaleConfiguration, ExtensionConfiguration, EntityReference
from betty.project import LocaleConfiguration, ExtensionConfiguration, EntityReference, ProjectConfiguration
from betty.serve import Server, AppServer, NoPublicUrlBecauseServerNotStartedError


Expand All @@ -27,9 +28,29 @@ def depends_on(cls) -> set[type[Extension]]:
def _load(self, *entities: Entity) -> None:
self._app.project.ancestry.add(*entities)

async def load(self) -> None:
from betty.extension import CottonCandy
@classmethod
def project_configuration(cls) -> ProjectConfiguration:
from betty.extension import CottonCandy, Demo

configuration = ProjectConfiguration()
configuration.extensions.append(ExtensionConfiguration(Demo))
configuration.extensions.append(ExtensionConfiguration(CottonCandy, True, CottonCandyConfiguration(
featured_entities=[
EntityReference(Place, 'betty-demo-amsterdam'),
EntityReference(Person, 'betty-demo-liberta-lankester'),
EntityReference(Place, 'betty-demo-netherlands'),
],
)))
# Include all of the translations Betty ships with.
configuration.locales.replace(
LocaleConfiguration('en-US', 'en'),
LocaleConfiguration('nl-NL', 'nl'),
LocaleConfiguration('fr-FR', 'fr'),
LocaleConfiguration('uk', 'uk'),
)
return configuration

async def load(self) -> None:
netherlands = Place(
id='betty-demo-netherlands',
names=[
Expand Down Expand Up @@ -355,23 +376,14 @@ async def load(self) -> None:
))
self._load(bart_feenstra)

theme = self.app.extensions[CottonCandy]
theme.configuration.featured_entities.append(EntityReference(Person, 'betty-demo-liberta-lankester'))
theme.configuration.featured_entities.append(EntityReference(Place, 'betty-demo-amsterdam'))


class DemoServer(Server):
def __init__(self):
from betty.extension import Demo

self._app = App()
super().__init__(self._app.localizer)
self._app.project.configuration.extensions.append(ExtensionConfiguration(_Demo))
# Include all of the translations Betty ships with.
self._app.project.configuration.locales.replace(
LocaleConfiguration('en-US', 'en'),
LocaleConfiguration('nl-NL', 'nl'),
LocaleConfiguration('fr-FR', 'fr'),
LocaleConfiguration('uk', 'uk'),
)
self._app.project.configuration.update(Demo.project_configuration())
self._server: Server | None = None
self._exit_stack = AsyncExitStack()

Expand Down
4 changes: 2 additions & 2 deletions betty/tests/extension/demo/test___init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from pytest_mock import MockerFixture

from betty.app import App
from betty.extension.demo import _Demo
from betty.extension import Demo
from betty.load import load
from betty.model.ancestry import Person, Place, Event, Source, Citation
from betty.project import ExtensionConfiguration
Expand All @@ -11,7 +11,7 @@ class TestDemo:
async def test_load(self, mocker: MockerFixture) -> None:
mocker.patch('webbrowser.open_new_tab')
app = App()
app.project.configuration.extensions.append(ExtensionConfiguration(_Demo))
app.project.configuration.extensions.append(ExtensionConfiguration(Demo))
await load(app)
assert 0 != len(app.project.ancestry[Person])
assert 0 != len(app.project.ancestry[Place])
Expand Down

0 comments on commit 3c27ff6

Please sign in to comment.