Skip to content

Commit

Permalink
fix: issues where lazy values were used like bools
Browse files Browse the repository at this point in the history
  • Loading branch information
saemideluxe committed Dec 24, 2024
1 parent 60bfa47 commit 076b0e1
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 24 deletions.
11 changes: 9 additions & 2 deletions basxbread/layout/components/forms/helpers.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import htmlgenerator as hg
from django.conf import settings
from django.utils import formats
from django.utils.translation import gettext_lazy as _

import htmlgenerator as hg

from ..button import Button

REQUIRED_LABEL = getattr(settings, "REQUIRED_LABEL", " *")
Expand Down Expand Up @@ -54,7 +55,13 @@ def __init__(self, errors):
super().__init__(
errors,
hg.DIV(
hg.UL(hg.Iterator(errors or (), "error", hg.LI(hg.C("error")))),
hg.UL(
hg.Iterator(
errors if errors is not None else (),
"error",
hg.LI(hg.C("error")),
)
),
_class="bx--form-requirement",
),
)
Expand Down
70 changes: 48 additions & 22 deletions basxbread/layout/components/forms/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@

import django_countries.widgets
import django_filters
import htmlgenerator as hg
from _strptime import TimeRE
from basxbread.utils import get_all_subclasses
from django.conf import settings
from django.forms import widgets
from django.urls import reverse
from django.utils import formats
from django.utils.translation import gettext_lazy as _
from phonenumber_field.formfields import PhoneNumberField

from basxbread.utils import get_all_subclasses
import htmlgenerator as hg

from ..button import Button
from ..icon import Icon
Expand Down Expand Up @@ -67,7 +67,7 @@ def get_input_element(self, inputelement_attrs, errors, **kwargs):
type=self.input_type,
lazy_attributes=_combine_lazy_dict(
_append_classes(
inputelement_attrs or {},
inputelement_attrs if inputelement_attrs is not None else {},
self.carbon_input_class,
hg.If(
getattr(errors, "condition", False),
Expand Down Expand Up @@ -115,7 +115,9 @@ def __init__(
icon=None,
**attributes,
):
inputelement_attrs = inputelement_attrs or {}
inputelement_attrs = (
inputelement_attrs if inputelement_attrs is not None else {}
)
inputelement_attrs = hg.merge_html_attrs(
inputelement_attrs, {"style": "padding-right: 2.5rem"}
)
Expand Down Expand Up @@ -192,7 +194,9 @@ def __init__(
boundfield=None,
**attributes,
):
inputelement_attrs = inputelement_attrs or {}
inputelement_attrs = (
inputelement_attrs if inputelement_attrs is not None else {}
)
super().__init__(
label,
hg.DIV(
Expand Down Expand Up @@ -240,7 +244,9 @@ def __init__(
boundfield=None,
**attributes,
):
inputelement_attrs = inputelement_attrs or {}
inputelement_attrs = (
inputelement_attrs if inputelement_attrs is not None else {}
)
showhidebtn = Button(
_("Show password"),
icon=hg.BaseElement(
Expand Down Expand Up @@ -283,7 +289,9 @@ def __init__(
boundfield=None,
**attributes,
):
inputelement_attrs = inputelement_attrs or {}
inputelement_attrs = (
inputelement_attrs if inputelement_attrs is not None else {}
)
super().__init__(
label,
hg.DIV(
Expand All @@ -296,10 +304,10 @@ def __init__(
),
),
hg.TEXTAREA(
boundfield.value() if boundfield else None,
boundfield.value() if boundfield is not None else None,
lazy_attributes=_combine_lazy_dict(
_append_classes(
inputelement_attrs or {},
inputelement_attrs,
self.carbon_input_class,
hg.If(
getattr(errors, "condition", None),
Expand Down Expand Up @@ -333,7 +341,9 @@ def __init__(
choices=None, # for non-django-form select elements use this
**attributes,
):
inputelement_attrs = inputelement_attrs or {}
inputelement_attrs = (
inputelement_attrs if inputelement_attrs is not None else {}
)
select_wrapper = hg.DIV(
hg.If(
_more_choices_than(choices if choices else boundfield, 12),
Expand Down Expand Up @@ -515,7 +525,9 @@ def __init__(
choices=None, # for non-django-form select elements use this
**attributes, # for non-django-form select elements use this
):
inputelement_attrs = inputelement_attrs or {}
inputelement_attrs = (
inputelement_attrs if inputelement_attrs is not None else {}
)
optgroups = (
_optgroups_from_choices(
choices,
Expand Down Expand Up @@ -673,9 +685,11 @@ def __init__(
boundfield=None,
**attributes,
):
inputelement_attrs = inputelement_attrs or {}
inputelement_attrs = (
inputelement_attrs if inputelement_attrs is not None else {}
)
attrs = {}
if boundfield:
if boundfield is not None:
attrs["checked"] = hg.F(
lambda c: hg.resolve_lazy(boundfield, c).field.widget.check_test(
hg.resolve_lazy(boundfield, c).value()
Expand Down Expand Up @@ -726,7 +740,9 @@ def __init__(
boundfield=None,
**attributes,
):
inputelement_attrs = inputelement_attrs or {}
inputelement_attrs = (
inputelement_attrs if inputelement_attrs is not None else {}
)
super().__init__(
hg.FIELDSET(
label,
Expand Down Expand Up @@ -770,9 +786,11 @@ def __init__(
boundfield=None,
**attributes,
):
inputelement_attrs = inputelement_attrs or {}
inputelement_attrs = (
inputelement_attrs if inputelement_attrs is not None else {}
)
attrs = {}
if boundfield:
if boundfield is not None:
attrs["checked"] = hg.F(
lambda c: hg.resolve_lazy(boundfield, c).field.widget.check_test(
hg.resolve_lazy(boundfield, c).value()
Expand Down Expand Up @@ -808,7 +826,9 @@ def __init__(
boundfield=None,
**attributes,
):
inputelement_attrs = inputelement_attrs or {}
inputelement_attrs = (
inputelement_attrs if inputelement_attrs is not None else {}
)
super().__init__(
hg.FIELDSET(
label,
Expand Down Expand Up @@ -859,7 +879,9 @@ def __init__(
formatkey=None,
**attributes,
):
inputelement_attrs = inputelement_attrs or {}
inputelement_attrs = (
inputelement_attrs if inputelement_attrs is not None else {}
)

def format_date_value(context):
bfield = hg.resolve_lazy(boundfield, context)
Expand Down Expand Up @@ -965,7 +987,9 @@ def __init__(
boundfield=None,
**attributes,
):
inputelement_attrs = inputelement_attrs or {}
inputelement_attrs = (
inputelement_attrs if inputelement_attrs is not None else {}
)
uploadbutton = hg.LABEL(
hg.SPAN(
hg.If(inputelement_attrs.get("value"), "...", _("Select file")),
Expand Down Expand Up @@ -997,7 +1021,7 @@ def __init__(
# we can only clear the field if it originates form a django field
# otherwise it has no use
clearbox = None
if boundfield:
if boundfield is not None:
checkbox_name = hg.F(
lambda c: hg.resolve_lazy(
boundfield, c
Expand Down Expand Up @@ -1121,7 +1145,9 @@ def __init__(
boundfield=None,
**attributes,
):
inputelement_attrs = inputelement_attrs or {}
inputelement_attrs = (
inputelement_attrs if inputelement_attrs is not None else {}
)
searchresult_id = hg.format("{}-searchresult", inputelement_attrs.get("id"))
super().__init__(
label,
Expand Down Expand Up @@ -1215,7 +1241,7 @@ def __init__(
):
def _subwidgets(context):
ret = []
if boundfield:
if boundfield is not None:
realboundfield = hg.resolve_lazy(boundfield, context)
for i, (widget, data) in enumerate(
zip(
Expand Down

0 comments on commit 076b0e1

Please sign in to comment.