diff --git a/digiplan/map/forms.py b/digiplan/map/forms.py index 76a43513..e0652de0 100644 --- a/digiplan/map/forms.py +++ b/digiplan/map/forms.py @@ -4,7 +4,7 @@ from django.utils.safestring import mark_safe from django_mapengine import legend -from . import charts +from . import charts, menu from .widgets import SwitchWidget @@ -49,7 +49,19 @@ def __init__(self, layer: legend.LegendLayer, *args, **kwargs) -> None: # noqa: self.layer = layer -class PanelForm(TemplateForm): # noqa: D101 +class TemplateExtraContentForm(TemplateForm): + """Add extra content to template form.""" + + extra_content = {} + + def __str__(self) -> str: # noqa: D105 + if self.template_name: + renderer = renderers.get_default_renderer() + return mark_safe(renderer.render(self.template_name, {"form": self, **self.extra_content})) # noqa: S308 + return super().__str__() + + +class PanelForm(TemplateExtraContentForm): # noqa: D101 def __init__(self, parameters, additional_parameters=None, **kwargs) -> None: # noqa: D107, ANN001 super().__init__(**kwargs) self.fields = {item["name"]: item["field"] for item in self.generate_fields(parameters, additional_parameters)} @@ -103,6 +115,13 @@ def generate_fields(parameters, additional_parameters=None): # noqa: ANN001, AN class EnergyPanelForm(PanelForm): # noqa: D101 template_name = "forms/panel_energy.html" + def __init__(self, parameters, additional_parameters=None, **kwargs) -> None: # noqa: D107, ANN001 + super().__init__(parameters, additional_parameters, **kwargs) + for technology in ("wind_2018", "wind_2024", "wind_2027", "pv_ground", "pv_roof"): + key_results = menu.detail_key_results(technology) + for key, value in key_results.items(): + self.extra_content[f"{technology}_key_result_{key}"] = value + class HeatPanelForm(PanelForm): # noqa: D101 template_name = "forms/panel_heat.html" diff --git a/digiplan/map/menu.py b/digiplan/map/menu.py new file mode 100644 index 00000000..f90dfa52 --- /dev/null +++ b/digiplan/map/menu.py @@ -0,0 +1,16 @@ +"""Add calculations for menu items.""" + + +def detail_key_results(technology: str, **kwargs: dict) -> dict: # noqa: ARG001 + """Calculate detail key results for given technology.""" + if technology == "wind_2018": + return {"area": 300, "turbines": 20, "energy": 2000} + if technology == "wind_2024": + return {"area": 400, "turbines": 30, "energy": 3000} + if technology == "wind_2027": + return {"area": 500, "turbines": 50, "energy": 4000} + if technology == "pv_ground": + return {"area": 33, "energy": 2001} + if technology == "pv_roof": + return {"area": 34, "energy": 2002} + raise KeyError(f"Unknown technology '{technology}'.") diff --git a/digiplan/map/urls.py b/digiplan/map/urls.py index 2a3d8b28..9bdb13f8 100644 --- a/digiplan/map/urls.py +++ b/digiplan/map/urls.py @@ -12,4 +12,5 @@ path("choropleth//", views.get_choropleth, name="choropleth"), path("popup//", views.get_popup, name="popup"), path("charts", views.get_charts, name="charts"), + path("detail_key_results", views.DetailKeyResultsView.as_view(), name="detail_key_results"), ] diff --git a/digiplan/map/views.py b/digiplan/map/views.py index f47778ad..47374fc0 100644 --- a/digiplan/map/views.py +++ b/digiplan/map/views.py @@ -6,12 +6,12 @@ from django.conf import settings from django.http import HttpRequest, response +from django.utils.translation import gettext_lazy as _ from django.views.generic import TemplateView from django_mapengine import views -from django.utils.translation import gettext_lazy as _ from digiplan import __version__ -from digiplan.map import config +from digiplan.map import config, menu from . import charts, choropleths, forms, map_config, popups, utils @@ -185,3 +185,16 @@ def get_charts(request: HttpRequest) -> response.JsonResponse: return response.JsonResponse( {lookup: charts.CHARTS[lookup](simulation_id=simulation_id).render() for lookup in lookups}, ) + + +class DetailKeyResultsView(TemplateView): + """Return HTMX-partial for requested detail key results.""" + + template_name = "forms/panel_energy.html#key_results" + + def get_context_data(self, **kwargs) -> dict: # noqa: ARG002 + """Get detail key results for requested technology.""" + technology = self.request.GET["technology"] + return { + f"key_result_{key}": value for key, value in menu.detail_key_results(technology, **self.request.GET).items() + } diff --git a/digiplan/templates/forms/panel_energy.html b/digiplan/templates/forms/panel_energy.html index 06456819..2352e5d5 100644 --- a/digiplan/templates/forms/panel_energy.html +++ b/digiplan/templates/forms/panel_energy.html @@ -2,12 +2,12 @@ {% load static %} {% load partials %} -{% partialdef wind_key_results %} +{% partialdef key_results %}
- {{ wind_key_result_area }} ha + {{ key_result_area }} ha
@@ -16,49 +16,22 @@
-
-
- - {{ wind_key_result_turbines }} - -
-
- {% trans "Anlagen werden errichtet" %} -
-
-
-
- - {{ wind_key_result_energy }} - -
-
- - {% trans "können gewonnen werden" %} - -
-
-
-{% endpartialdef %} - -{% partialdef pv_key_results %} -
-
-
- - {{ pv_key_result_area }} ha - -
-
- - {% trans "werden genutzt" %} - + {% if key_result_turbines %} +
+
+ + {{ key_result_turbines }} + +
+
+ {% trans "Anlagen werden errichtet" %} +
-
+ {% endif %}
- {{ pv_key_result_energy }} TWh + {{ key_result_energy }}
@@ -140,7 +113,9 @@

{% trans "Generation" %}

- {% partial wind_key_results with wind_key_result_area=wind_key_result_area_2018 wind_key_result_turbines=wind_key_result_turbines_2018 wind_key_result_energy=wind_key_result_energy_2018 %} + {% with key_result_area=wind_2018_key_result_area key_result_turbines=wind_2018_key_result_turbines key_result_energy=wind_2018_key_result_energy %} + {% partial key_results %} + {% endwith %}
@@ -184,7 +159,9 @@

{% trans "Generation" %}

- {% partial wind_key_results with wind_key_result_area=wind_key_result_area_2024 wind_key_result_turbines=wind_key_result_turbines_2024 wind_key_result_energy=wind_key_result_energy_2024 %} + {% with key_result_area=wind_2024_key_result_area key_result_turbines=wind_2024_key_result_turbines key_result_energy=wind_2024_key_result_energy %} + {% partial key_results %} + {% endwith %}
@@ -240,7 +217,9 @@

{% trans "Generation" %}

- {% partial wind_key_results with wind_key_result_area=wind_key_result_area_2027 wind_key_result_turbines=wind_key_result_turbines_2027 wind_key_result_energy=wind_key_result_energy_2027 %} + {% with key_result_area=wind_2027_key_result_area key_result_turbines=wind_2027_key_result_turbines key_result_energy=wind_2027_key_result_energy %} + {% partial key_results %} + {% endwith %}
@@ -326,7 +305,9 @@

{% trans "Generation" %}

- {% partial pv_key_results with pv_key_result_area=pv_ground_key_result_area pv_key_result_energy=pv_ground_key_result_energy %} + {% with key_result_area=pv_ground_key_result_area key_result_energy=pv_ground_key_result_energy %} + {% partial key_results %} + {% endwith %}
@@ -369,7 +350,9 @@

{% trans "Generation" %}

- {% partial pv_key_results with pv_key_result_area=pv_roof_key_result_area pv_key_result_energy=pv_roof_key_result_energy %} + {% with key_result_area=pv_roof_key_result_area key_result_energy=pv_roof_key_result_energy %} + {% partial key_results %} + {% endwith %}