Skip to content

Commit

Permalink
changed access to forms in template and creation in view
Browse files Browse the repository at this point in the history
  • Loading branch information
josihoppe committed Jul 15, 2024
1 parent f1693c9 commit bc5d0d6
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 22 deletions.
14 changes: 6 additions & 8 deletions slapp/explorer/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,9 @@ def get_field_attrs(self, name: str, parameters: dict) -> dict: # noqa: ARG002
return attrs

def generate_fields(self, parameters: dict) -> dict: # noqa: D102
for mun, param_set in parameters.items():
for name, item in param_set.items():
field_name = f"{mun}_{name}"
field = FloatField(
widget=TextInput(attrs=self.get_field_attrs(field_name, parameters=item)),
required=item.get("required", True),
)
yield {"name": field_name, "field": field}
for name, item in parameters.items():
field = FloatField(
widget=TextInput(attrs=self.get_field_attrs(name, parameters=item)),
required=item.get("required", True),
)
yield {"name": name, "field": field}
15 changes: 9 additions & 6 deletions slapp/explorer/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,21 +124,24 @@ def details_csv(request: HttpRequest) -> HttpResponse:
def optimization_parameters(request: HttpRequest) -> HttpResponse:
"""Render parameters page for given municipality IDs."""
ids = request.GET.getlist("id")
mun_forms = {}

if ids:
if len(ids) > MAX_MUNICIPALITY_COUNT:
ids = ids[:MAX_MUNICIPALITY_COUNT]
messages.add_message(request, messages.WARNING, "Es können maximal 3 Gemeinden ausgewählt werden.")
municipalities = Municipality.objects.filter(id__in=ids)
else:
municipalities = None

with open("slapp/static/config/parameters_slider.json") as f: # noqa: PTH123
sliders_config = json.load(f)
with open("slapp/static/config/parameters_slider.json") as f: # noqa: PTH123
sliders_config = json.load(f)
for mun in municipalities:
parameters = sliders_config[str(mun.id)]
form_instance = ParametersSliderForm(parameters=parameters)
mun_forms[mun.name] = form_instance

return render(
request,
"pages/parameters.html",
{"municipalities": municipalities, "sliders_form": ParametersSliderForm(parameters=sliders_config)},
{"mun_forms": mun_forms},
)


Expand Down
16 changes: 8 additions & 8 deletions slapp/templates/pages/parameters.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ <h2>Parameter für die Energiesystemoptimieung festlegen</h2>
</div>
</div>
</div>
{% if municipalities %}
{% if mun_forms %}
<div class="row mt-4 g-0" id="municipality-results">
<div class="col-3">
<div class="rounded-0text-start">
Expand All @@ -48,11 +48,11 @@ <h2>Parameter für die Energiesystemoptimieung festlegen</h2>
<div class="h-155px table-cell">Speicherkapazität</div>
</div>
</div>
{% for municipality in municipalities %}
{% for municipality, form in mun_forms.items %}
<div class="col-3">
<div class="rounded-0 h-100px text-start">
<div class="h-55px table-cell fw-bold bg-secondary text-white">
{{ municipality.name }}
{{ municipality }}
<a class="icon-link text-white"
href="{% url 'explorer:parameters' %}? {% for municipality_ids in municipalities %}{% if municipality_ids.id != municipality.id %}id={{ municipality_ids.id }}&{% endif %}{% endfor %}">
<svg xmlns="http://www.w3.org/2000/svg"
Expand Down Expand Up @@ -84,14 +84,14 @@ <h2>Parameter für die Energiesystemoptimieung festlegen</h2>
<fieldset class="mb-3">
<div class="form-check">
<!-- This is the input slider -->
<span>{{ sliders_form.1_pv }}</span>
<span>{{ form.pv }}</span>
<label class="form-check-label d-inline-block">
MW max. Gesamtleistung <i>(aktuell: 0,12 MW)</i>
</label>
</div>
<div class="form-check">
<!-- This is the input slider -->
<span>{{ sliders_form.1_pv }}</span>
<span>{{ form.pv }}</span>
<label class="form-check-label d-inline-block"
for="pv-{{ municipality.id }}-area">
km² max. Flächenverbrauch <i>(1,73 km² ausgewiesen, davon derzeit 0,27 km² belegt)</i>
Expand All @@ -104,14 +104,14 @@ <h2>Parameter für die Energiesystemoptimieung festlegen</h2>
<fieldset class="mb-3">
<div class="form-check">
<!-- This is the input slider -->
<span>{{ sliders_form.1_wind }}</span>
<span>{{ form.wind }}</span>
<label class="form-check-label d-inline-block" for="maxOutputRadio">
MW max. Gesamtleistung <i>(aktuell: 0,12 MW)</i>
</label>
</div>
<div class="form-check">
<!-- This is the input slider -->
{{ sliders_form.1_wind }}
{{ form.wind }}
<label class="form-check-label d-inline-block"
for="pv-{{ municipality.id }}-area">
km² max. Flächenverbrauch <i>(1,73 km² ausgewiesen, davon derzeit 0,27 km² belegt)</i>
Expand All @@ -124,7 +124,7 @@ <h2>Parameter für die Energiesystemoptimieung festlegen</h2>
<fieldset class="mb-3">
<div class="form-check">
<!-- This is the input slider -->
{{ sliders_form.1_hydro }}
{{ form.hydro }}
<label class="form-check-label d-inline-block" for="maxOutputRadio">
MW max. Gesamtleistung <i>(aktuell: 0,12 MW)</i>
</label>
Expand Down

0 comments on commit bc5d0d6

Please sign in to comment.