Skip to content

Commit

Permalink
added view to use multiple prefixes for renovation scenarios
Browse files Browse the repository at this point in the history
  • Loading branch information
josihoppe committed Dec 5, 2024
1 parent 568eb3a commit 753e16f
Show file tree
Hide file tree
Showing 8 changed files with 154 additions and 40 deletions.
12 changes: 10 additions & 2 deletions building_dialouge_webapp/heat/flows.py
Original file line number Diff line number Diff line change
Expand Up @@ -465,11 +465,13 @@ class Flow(TemplateView):

def __init__(self, prefix: str | None = None):
self.prefix = prefix
self.request = None
self.start = None
self.end = None
super().__init__()

def dispatch(self, request, *args, **kwargs) -> HttpResponse:
self.request = request
state_partials = self.start.set()

if request.htmx:
Expand Down Expand Up @@ -975,8 +977,8 @@ class RenovationRequestFlow(SidebarNavigationMixin, Flow):
),
}

def __init__(self):
super().__init__()
def __init__(self, prefix=None):
super().__init__(prefix=prefix)
self.start = FormState(
self,
name="primary_heating",
Expand Down Expand Up @@ -1031,12 +1033,18 @@ def __init__(self):

self.end = EndState(self, url="heat:financial_support")

def dispatch(self, request, *args, **kwargs):
# Retrieve the prefix dynamically
self.prefix = kwargs.get("scenario", self.prefix)
return super().dispatch(request, *args, **kwargs)


class FinancialSupporFlow(SidebarNavigationMixin, Flow):
template_name = "pages/financial_support.html"
extra_context = {
"back_url": "heat:renovation_request",
"next_includes": "#financial_support",
"back_kwargs": "scenario1",
}

def __init__(self):
Expand Down
1 change: 1 addition & 0 deletions building_dialouge_webapp/heat/navigation.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ def get_flows(self):
"name": "Sanierungswunsch",
"object": flows.RenovationRequestFlow,
"url": "heat:renovation_request",
"kwargs": "scenario1",
},
{"name": "Förderung", "object": flows.FinancialSupporFlow, "url": "heat:financial_support"},
],
Expand Down
2 changes: 1 addition & 1 deletion building_dialouge_webapp/heat/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
path("ventilation_system/", flows.VentilationSystemFlow.as_view(), name="ventilation_system"),
# step 3 renovation request
path("intro_renovation/", views.IntroRenovation.as_view(), name="intro_renovation"),
path("renovation_request/", flows.RenovationRequestFlow.as_view(), name="renovation_request"),
path("renovation_request/<str:scenario>", views.renovation_scenario, name="renovation_request"),
path("financial_support/", flows.FinancialSupporFlow.as_view(), name="financial_support"),
path("results/", views.Results.as_view(), name="results"),
path("next_steps/", views.NextSteps.as_view(), name="next_steps"),
Expand Down
50 changes: 50 additions & 0 deletions building_dialouge_webapp/heat/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
from django.http import HttpResponseRedirect
from django.http import JsonResponse
from django.template.loader import render_to_string
from django.urls import reverse
from django.views.generic import TemplateView

from building_dialouge_webapp.heat.flows import RenovationRequestFlow

from .navigation import SidebarNavigationMixin


Expand Down Expand Up @@ -57,9 +63,53 @@ class IntroRenovation(SidebarNavigationMixin, TemplateView):
extra_context = {
"back_url": "heat:ventilation_system",
"next_url": "heat:renovation_request",
"next_kwargs": "scenario1",
}


SCENARIO_MAX = 3


def renovation_scenario(request, scenario=None):
if "scenarios" not in request.session:
request.session["scenarios"] = []

if request.method == "GET":
scenarios = request.session["scenarios"]

if scenario != "new_scenario":
request.session["scenarios"].append(scenario)
request.session.modified = True
flow = RenovationRequestFlow(prefix=scenario)
return flow.dispatch(request)

if len(scenarios) < SCENARIO_MAX:
new_scenario = f"scenario{len(scenarios) + 1}"
request.session["scenarios"].append(new_scenario)
request.session.modified = True
return HttpResponseRedirect(reverse("heat:renovation_request", kwargs={"scenario": new_scenario}))

return JsonResponse({"error": "Maximum number of scenarios reached."}, status=400)

if request.method == "POST" and request.htmx:
if "save_scenario" in request.POST:
# FLow States should save their data automatically
session_data = request.session.get("django_htmx_flow", {})
scenario_data = session_data
# Return partial to be rendered with htmx, including the scenarioX fields
return JsonResponse(
{
"html": render_to_string("partials/scenario_box.html", {"scenario": scenario_data}),
},
)

# For other HTMX requests, continue the flow
flow = RenovationRequestFlow(prefix=scenario)
return flow.dispatch(request)

return JsonResponse({"error": "Invalid request."}, status=400)


class Results(SidebarNavigationMixin, TemplateView):
template_name = "pages/results.html"
extra_context = {
Expand Down
6 changes: 5 additions & 1 deletion building_dialouge_webapp/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,11 @@
</li>
{% for step in category.steps %}
<li class="sidebar-nav__step {{ step.index_state }}">
<a href="{% url step.url %}">{{ step.name }}</a>
{% if step.kwargs %}
<a href="{% url step.url scenario=step.kwargs %}">{{ step.name }}</a>
{% else %}
<a href="{% url step.url %}">{{ step.name }}</a>
{% endif %}
</li>
{% endfor %}
{% endfor %}
Expand Down
85 changes: 49 additions & 36 deletions building_dialouge_webapp/templates/base_footer_button.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,47 +5,60 @@
<div class="container-fluid d-flex justify-content-center py-2 px-4">
<div class="d-flex gap-3">
{# Back button is a simple link to given URL #}
<a class="btn btn-light" href="{% url back_url %}">
<svg xmlns="http://www.w3.org/2000/svg"
width="16"
height="16"
fill="currentColor"
class="bi bi-arrow-left"
viewBox="0 0 16 16">
<path fill-rule="evenodd" d="M15 8a.5.5 0 0 0-.5-.5H2.707l3.147-3.146a.5.5 0 1 0-.708-.708l-4 4a.5.5 0 0 0 0 .708l4 4a.5.5 0 0 0 .708-.708L2.707 8.5H14.5A.5.5 0 0 0 15 8" />
</svg>
<span class="ps-2">Zurück</span>
</a>
{# Next button in flow is always a HTMX call to current page including several form inputs. #}
{% if next_includes %}
<a class="btn btn-primary" hx-post="" hx-include="{{ next_includes }}">
<span class="ps-2">Weiter</span>
{% if back_kwargs %}
<!-- djlint:off H025 -->
<a class="btn btn-light" href="{% url back_url scenario=back_kwargs %}">
<!-- djlint:on -->
{% else %}
<a class="btn btn-light" href="{% url back_url %}">
{% endif %}
<svg xmlns="http://www.w3.org/2000/svg"
width="16"
height="16"
fill="currentColor"
class="bi bi-arrow-right"
class="bi bi-arrow-left"
viewBox="0 0 16 16">
<path fill-rule="evenodd" d="M1 8a.5.5 0 0 1 .5-.5h11.793l-3.147-3.146a.5.5 0 0 1 .708-.708l4 4a.5.5 0 0 1 0 .708l-4 4a.5.5 0 0 1-.708-.708L13.293 8.5H1.5A.5.5 0 0 1 1 8" />
<path fill-rule="evenodd" d="M15 8a.5.5 0 0 0-.5-.5H2.707l3.147-3.146a.5.5 0 1 0-.708-.708l-4 4a.5.5 0 0 0 0 .708l4 4a.5.5 0 0 0 .708-.708L2.707 8.5H14.5A.5.5 0 0 0 15 8" />
</svg>
<span class="ps-2">Zurück</span>
</a>
{% endif %}
{% if next_url %}
<a class="btn btn-primary" href="{% url next_url %}">
<span class="ps-2">Weiter</span>
<svg xmlns="http://www.w3.org/2000/svg"
width="16"
height="16"
fill="currentColor"
class="bi bi-arrow-right"
viewBox="0 0 16 16">
<path fill-rule="evenodd" d="M1 8a.5.5 0 0 1 .5-.5h11.793l-3.147-3.146a.5.5 0 0 1 .708-.708l4 4a.5.5 0 0 1 0 .708l-4 4a.5.5 0 0 1-.708-.708L13.293 8.5H1.5A.5.5 0 0 1 1 8" />
</svg>
</a>
{% else %}
{% endif %}
{# Next button in flow is always a HTMX call to current page including several form inputs. #}
{% if next_includes %}
<a class="btn btn-primary" hx-post="" hx-include="{{ next_includes }}">
<span class="ps-2">Weiter</span>
<svg xmlns="http://www.w3.org/2000/svg"
width="16"
height="16"
fill="currentColor"
class="bi bi-arrow-right"
viewBox="0 0 16 16">
<path fill-rule="evenodd" d="M1 8a.5.5 0 0 1 .5-.5h11.793l-3.147-3.146a.5.5 0 0 1 .708-.708l4 4a.5.5 0 0 1 0 .708l-4 4a.5.5 0 0 1-.708-.708L13.293 8.5H1.5A.5.5 0 0 1 1 8" />
</svg>
</a>
{% endif %}
{% if next_url %}
{% if next_kwargs %}
<!-- djlint:off H025 -->
<a class="btn btn-primary"
href="{% url next_url scenario=next_kwargs %}">
<!-- djlint:on -->
{% else %}
<a class="btn btn-primary" href="{% url next_url %}">
{% endif %}
<span class="ps-2">Weiter</span>
<svg xmlns="http://www.w3.org/2000/svg"
width="16"
height="16"
fill="currentColor"
class="bi bi-arrow-right"
viewBox="0 0 16 16">
<path fill-rule="evenodd" d="M1 8a.5.5 0 0 1 .5-.5h11.793l-3.147-3.146a.5.5 0 0 1 .708-.708l4 4a.5.5 0 0 1 0 .708l-4 4a.5.5 0 0 1-.708-.708L13.293 8.5H1.5A.5.5 0 0 1 1 8" />
</svg>
</a>
{% else %}
{% endif %}
</div>
</div>
</div>
</div>
</div>
{{ block.super }}
{% endblock footer %}
{{ block.super }}
{% endblock footer %}
25 changes: 25 additions & 0 deletions building_dialouge_webapp/templates/pages/renovation_request.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,22 @@ <h1>Sanierungswunsch</h1>
</div>
<div class="help"></div>
</div>
<div class="help">
<div id="scenario-boxes">
{% for scenario_data in request.session.values %}
{% if scenario_data is dict %}
<div class="scenario-box">
<p>
<strong>Heizung:</strong> {{ scenario_data.primary_heating }}
</p>
<p>
<strong>Details:</strong> {{ scenario_data.renovation_details|join:", " }}
</p>
</div>
{% endif %}
{% endfor %}
</div>
</div>
<div class="step-question">
<div class="step-container">
<div id="primary_heating"
Expand Down Expand Up @@ -67,5 +83,14 @@ <h1>Sanierungswunsch</h1>
<div class="help"></div>
</div>
</div>
<div class="d-flex justify-content-between">
<button class="btn btn-secondary"
hx-post="{% url 'heat:renovation_request' scenario='save_scenario' %}"
hx-include="#primary_heating, #renovation_details"
hx-target="#scenario-boxes">Szenario speichern</button>
<button class="btn btn-primary"
hx-get="{% url 'heat:renovation_request' scenario='new_scenario' %}"
hx-target="body">Weiteres Heizszenario</button>
</div>
</section>
{% endblock content %}
13 changes: 13 additions & 0 deletions building_dialouge_webapp/templates/partials/scenario_box.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<div class="scenario-box" id="scenario-{{ prefix }}">
<a href="{% url 'renovation_request' prefix %}">Scenario {{ prefix }}</a>
</div>
.scenario-box {
border: 1px solid #ccc;
padding: 10px;
margin: 5px;
cursor: pointer;
background-color: #f9f9f9;
&:hover {
background-color: #e6e6e6;
}
}

0 comments on commit 753e16f

Please sign in to comment.