Skip to content

Commit

Permalink
Set up detail key results from dummy calculation using partials
Browse files Browse the repository at this point in the history
  • Loading branch information
henhuy committed Mar 26, 2024
1 parent 99f9b40 commit 8b368ec
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 50 deletions.
23 changes: 21 additions & 2 deletions digiplan/map/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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)}
Expand Down Expand Up @@ -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"
Expand Down
16 changes: 16 additions & 0 deletions digiplan/map/menu.py
Original file line number Diff line number Diff line change
@@ -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}'.")
1 change: 1 addition & 0 deletions digiplan/map/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@
path("choropleth/<str:lookup>/<str:layer_id>", views.get_choropleth, name="choropleth"),
path("popup/<str:lookup>/<int:region>", views.get_popup, name="popup"),
path("charts", views.get_charts, name="charts"),
path("detail_key_results", views.DetailKeyResultsView.as_view(), name="detail_key_results"),
]
17 changes: 15 additions & 2 deletions digiplan/map/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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()
}
75 changes: 29 additions & 46 deletions digiplan/templates/forms/panel_energy.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
{% load static %}
{% load partials %}

{% partialdef wind_key_results %}
{% partialdef key_results %}
<div class="sidepanel__key-numbers">
<div class="sidepanel__key-number">
<div class="sidepanel__key-number-value sidepanel__key-number-value--wind">
<span>
{{ wind_key_result_area }} ha
{{ key_result_area }} ha
</span>
</div>
<div class="sidepanel__key-number-description">
Expand All @@ -16,49 +16,22 @@
</span>
</div>
</div>
<div class="sidepanel__key-number">
<div class="sidepanel__key-number-value sidepanel__key-number-value--wind">
<span>
{{ wind_key_result_turbines }}
</span>
</div>
<div class="sidepanel__key-number-description">
{% trans "Anlagen werden errichtet" %}
</div>
</div>
<div class="sidepanel__key-number">
<div class="sidepanel__key-number-value sidepanel__key-number-value--wind">
<span>
{{ wind_key_result_energy }}
</span>
</div>
<div class="sidepanel__key-number-description">
<span>
{% trans "können gewonnen werden" %}
</span>
</div>
</div>
</div>
{% endpartialdef %}

{% partialdef pv_key_results %}
<div class="sidepanel__key-numbers">
<div class="sidepanel__key-number">
<div class="sidepanel__key-number-value sidepanel__key-number-value--wind">
<span>
{{ pv_key_result_area }} ha
</span>
</div>
<div class="sidepanel__key-number-description">
<span>
{% trans "werden genutzt" %}
</span>
{% if key_result_turbines %}
<div class="sidepanel__key-number">
<div class="sidepanel__key-number-value sidepanel__key-number-value--wind">
<span>
{{ key_result_turbines }}
</span>
</div>
<div class="sidepanel__key-number-description">
{% trans "Anlagen werden errichtet" %}
</div>
</div>
</div>
{% endif %}
<div class="sidepanel__key-number">
<div class="sidepanel__key-number-value sidepanel__key-number-value--wind">
<span>
{{ pv_key_result_energy }} TWh
{{ key_result_energy }}
</span>
</div>
<div class="sidepanel__key-number-description">
Expand Down Expand Up @@ -140,7 +113,9 @@ <h2 class="panel__title title">{% trans "Generation" %}</h2>
</div>
</div>
<div class="sidepanel__block" id="wind_key_results_2018">
{% 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 %}
</div>
<div class="sidepanel__block">
<div class="sidepanel__more">
Expand Down Expand Up @@ -184,7 +159,9 @@ <h2 class="panel__title title">{% trans "Generation" %}</h2>
</div>
</div>
<div class="sidepanel__block" id="wind_key_results_2024">
{% 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 %}
</div>
<div class="sidepanel__block">
<div class="sidepanel__more">
Expand Down Expand Up @@ -240,7 +217,9 @@ <h2 class="panel__title title">{% trans "Generation" %}</h2>
</div>
</div>
<div class="sidepanel__block" id="wind_key_results_2027">
{% 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 %}
</div>
<div class="sidepanel__block">
<div class="sidepanel__more">
Expand Down Expand Up @@ -326,7 +305,9 @@ <h2 class="panel__title title">{% trans "Generation" %}</h2>
</div>
</div>
<div class="sidepanel__block" id="pv_ground_key_results">
{% 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 %}
</div>
<div class="sidepanel__block">
<div class="sidepanel__more">
Expand Down Expand Up @@ -369,7 +350,9 @@ <h2 class="panel__title title">{% trans "Generation" %}</h2>
</div>
</div>
<div class="sidepanel__block" id="pv_roof_key_results">
{% 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 %}
</div>
<div class="sidepanel__block">
<div class="sidepanel__more">
Expand Down

0 comments on commit 8b368ec

Please sign in to comment.