Bugfix: More than one Extension using ViewModels should be possible (#2059)

* Bugfix: Make ViewModels to work for more than 1 ext

* same fix for WeclomeVM

* Don't use Exception but SpecterInternalException

Co-authored-by: Manolis Mandrapilias <70536101+moneymanolis@users.noreply.github.com>
This commit is contained in:
k9ert 2023-01-19 11:48:11 +01:00 committed by GitHub
parent f68eba63fe
commit d8af9d109e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 39 additions and 11 deletions

View file

@ -146,7 +146,9 @@ A reasonable `mywalletdetails.jinja` would look like this:
![](./images/extensions/add_wallettabs.png)
## Extending certain pages or complete endpoints
## Extending certain pages or complete endpoints (don't use this for now)
Unfortunately this method is only able to be used once per Extension. So it's more or less unusable right now as there are already two extensions which are using those.
For some endpoints, there is the possibility to extend/change parts of a page or the complete page. This works by declaring the `callback_adjust_view_model` method in your extension and modify the ViewModel which got passed into the callback. As there is only one callback for all types of ViewModels, you will need to check for the type that you're expecting and only adjust this type. Here is an example:
@ -161,11 +163,13 @@ class ExtensionidService(Service):
# view_model.about_redirect=url_for("spectrum_endpoint.some_enpoint_here")
# but we do it small here and only replace a specific component:
view_model.get_started_include = "spectrum/welcome/components/get_started.jinja"
return view_model
return view_model
return None
```
Make sure to return `None` if the `view_model` is not the type you're interested in.
In this example, a certain part of the page gets replaced. As you can read in the comments, you could also trigger a complete redirect to a different endpoint.
Currently, only two `ViewModels` are existing. Check them out. Don't hesitate to create an issue if you'd like to modify something where no ViewModel exists yet:
- cryptoadvance.specter.server_endpoints.welcome.welcome_vm
- cryptoadvance.specter.server_endpoints.wallets.wallets_vm
- cryptoadvance.specter.server_endpoints.wallets.wallets_vm

View file

@ -77,15 +77,25 @@ def wallets_overview():
wallets_overview_vm_dict = app.specter.service_manager.execute_ext_callbacks(
adjust_view_model, WalletsOverviewVm()
)
if len(wallets_overview_vm_dict.values()) > 1:
raise logger.error(
"Seems that we have more than one WalletsOverviewVm Extension"
number_of_wallets_overview_vm = len(
[
wallets_overview_vm
for wallets_overview_vm in wallets_overview_vm_dict.values()
if type(wallets_overview_vm) == WalletsOverviewVm
]
)
if number_of_wallets_overview_vm > 1:
raise Exception(
f"Seems that we have more than one WalletsOverviewVm Extension: {wallets_overview_vm_dict} "
)
if len(wallets_overview_vm_dict.values()) == 1:
if number_of_wallets_overview_vm == 1:
wallets_overview_vm = list(wallets_overview_vm_dict.values())[0]
else:
wallets_overview_vm = WalletsOverviewVm()
if wallets_overview_vm.wallets_overview_redirect != None:
logger.info(
f"Extension {list(wallets_overview_vm_dict.keys())[0]} redirects to {wallets_overview_vm.wallets_overview_redirect}"
)
return redirect(wallets_overview_vm.wallets_overview_redirect)
for wallet in list(app.specter.wallet_manager.wallets.values()):

View file

@ -12,7 +12,7 @@ from ...helpers import notify_upgrade
from ...managers.wallet_manager import purposes
from ...server_endpoints import flash
from ...services.callbacks import adjust_view_model
from ...specter_error import SpecterError
from ...specter_error import SpecterError, SpecterInternalException
from .welcome_vm import WelcomeVm
logger = logging.getLogger(__name__)
@ -56,13 +56,25 @@ def about():
welcome_vm_dict = app.specter.service_manager.execute_ext_callbacks(
adjust_view_model, WelcomeVm()
)
if len(welcome_vm_dict.values()) > 1:
raise SpecterError("Seems that we have more than one Welcome Extension")
if len(welcome_vm_dict.values()) == 1:
number_of_welcome_vm = len(
[
wallets_overview_vm
for wallets_overview_vm in welcome_vm_dict.values()
if type(wallets_overview_vm) == WelcomeVm
]
)
if number_of_welcome_vm > 1:
raise SpecterInternalException(
f"Seems that we have more than one WelcomeVm Extension: {welcome_vm_dict} "
)
if number_of_welcome_vm == 1:
welcome_vm = list(welcome_vm_dict.values())[0]
else:
welcome_vm = WelcomeVm()
if welcome_vm.about_redirect != None:
logger.info(
f"Extension {list(welcome_vm_dict.keys())[0]} redirects to {welcome_vm_dict.wallets_overview_redirect}"
)
return redirect(welcome_vm.about_redirect)
if request.method == "POST":

View file

@ -159,4 +159,6 @@ class SpectrumService(Service):
view_model.get_started_include = (
"spectrum/welcome/components/get_started.jinja"
)
else:
return None
return view_model