"""Additional widgets used by forms or individually"""
from collections import defaultdict
from django.forms import Select, Widget, NumberInput
from django.forms.widgets import CheckboxSelectMultiple
from django.utils import html
from wam.settings import APP_LABELS
from stemp.app_settings import ADDITIONAL_PARAMETERS
from utils.widgets import CustomWidget
[Doku]class SelectWithDisabled(Select):
"""
Subclass of Django's select widget that allows disabling options.
To disable an option, pass a dict instead of a string for its label,
of the form: {'label': 'option label', 'disabled': True}
From: https://djangosnippets.org/snippets/2453/
"""
[Doku] def create_option(
self, name, value, label, selected, index, subindex=None, attrs=None
):
disabled = False
if isinstance(label, dict):
label, disabled = label["label"], label["disabled"]
option_dict = super(SelectWithDisabled, self).create_option(
name, value, label, selected, index, subindex=subindex, attrs=attrs
)
if disabled:
option_dict["attrs"]["disabled"] = "disabled"
return option_dict
[Doku]class HouseholdSummary(CustomWidget):
"""
Widget to show summary of a household
If "use_header" is True, an accordion is used to hide summary at first.
"""
def __init__(self, household, use_header=True, count=None):
self.template_name = (
"widgets/summary_household_accordion.html"
if use_header
else "widgets/summary_household_simple.html"
)
self.household = household
self.count = count
[Doku] def get_context(self):
return {"household": self.household, "count": self.count}
[Doku]class TechnologySummary(CustomWidget):
"""Widget to show chosen technology as icon plus name"""
template_name = "widgets/summary_technology.html"
def __init__(self, scenario_config):
self.config = scenario_config
[Doku] def get_context(self):
return {
"name": self.config["LABELS"]["name"],
"icon": self.config["LABELS"].get("icon", None),
"icon_class": self.config["LABELS"].get("icon_class", None),
}
[Doku]class ParameterSummary(CustomWidget):
"""
Widget to show non-default parameters, changed by user
For each changed parameter, related icon and label is shown
"""
template_name = "widgets/summary_parameter.html"
def __init__(self, changed_parameters):
self.parameters = changed_parameters
[Doku] def get_context(self):
labels = defaultdict(dict)
icons = {}
for comp, parameters in self.parameters.items():
icons[comp] = APP_LABELS["stemp"]["technologies"]["icons"].get(comp, None)
for parameter in parameters:
labels[comp][parameter] = (
ADDITIONAL_PARAMETERS.get(comp, {})
.get(parameter, ADDITIONAL_PARAMETERS.get(parameter, {}))
.get("label", parameter)
)
context = {"parameters": self.parameters, "labels": labels, "icons": icons}
return context