Skip to content

Commit 5edddba

Browse files
swap order of classes around
1 parent 4e70966 commit 5edddba

File tree

1 file changed

+50
-60
lines changed

1 file changed

+50
-60
lines changed

src/django_bird/params.py

Lines changed: 50 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -14,33 +14,44 @@
1414

1515

1616
@dataclass
17-
class Value:
18-
raw: str | bool | None
19-
quoted: bool = False
17+
class Params:
18+
attrs: list[Param] = field(default_factory=list)
19+
props: list[Param] = field(default_factory=list)
2020

21-
def resolve(self, context: Context | dict[str, Any]) -> str | bool | None | Any:
22-
match (self.raw, self.quoted):
23-
# Handle special string values and None
24-
case (None, _) | ("False", _):
25-
return None
26-
case ("True", _):
27-
return True
21+
@classmethod
22+
def with_attrs(cls, attrs: list[Param] | None) -> Params:
23+
"""Create a Params instance with a copy of the provided attrs."""
24+
return cls(attrs=attrs.copy() if attrs is not None else [], props=[])
2825

29-
# Handle boolean values
30-
case (bool(boolean), _):
31-
return boolean
26+
def render_props(self, nodelist: NodeList | None, context: Context):
27+
if nodelist is None:
28+
return
3229

33-
# Handle quoted strings as literals
34-
case (str(quoted_string), True):
35-
return quoted_string
30+
attrs_to_remove = set()
3631

37-
# Handle everything else as template variables, falling back to raw
38-
case _:
39-
raw_string = str(self.raw)
40-
try:
41-
return template.Variable(raw_string).resolve(context)
42-
except template.VariableDoesNotExist:
43-
return raw_string
32+
for node in nodelist:
33+
if not isinstance(node, PropNode):
34+
continue
35+
36+
value = Value(node.default, quoted=False)
37+
38+
for idx, attr in enumerate(self.attrs):
39+
if node.name == attr.name:
40+
resolved = attr.value.resolve(context)
41+
if resolved is not None:
42+
value = attr.value
43+
attrs_to_remove.add(idx)
44+
45+
self.props.append(Param(name=node.name, value=value))
46+
47+
for idx in sorted(attrs_to_remove, reverse=True):
48+
self.attrs.pop(idx)
49+
50+
return {prop.name: prop.render_prop(context) for prop in self.props}
51+
52+
def render_attrs(self, context: Context) -> SafeString:
53+
rendered = " ".join(attr.render_attr(context) for attr in self.attrs)
54+
return mark_safe(rendered)
4455

4556

4657
@dataclass
@@ -75,41 +86,20 @@ def from_bit(cls, bit: str) -> Param:
7586

7687

7788
@dataclass
78-
class Params:
79-
attrs: list[Param] = field(default_factory=list)
80-
props: list[Param] = field(default_factory=list)
81-
82-
@classmethod
83-
def with_attrs(cls, attrs: list[Param] | None) -> Params:
84-
"""Create a Params instance with a copy of the provided attrs."""
85-
return cls(attrs=attrs.copy() if attrs is not None else [], props=[])
86-
87-
def render_props(self, nodelist: NodeList | None, context: Context):
88-
if nodelist is None:
89-
return
90-
91-
attrs_to_remove = set()
92-
93-
for node in nodelist:
94-
if not isinstance(node, PropNode):
95-
continue
96-
97-
value = Value(node.default, quoted=False)
98-
99-
for idx, attr in enumerate(self.attrs):
100-
if node.name == attr.name:
101-
resolved = attr.value.resolve(context)
102-
if resolved is not None:
103-
value = attr.value
104-
attrs_to_remove.add(idx)
105-
106-
self.props.append(Param(name=node.name, value=value))
107-
108-
for idx in sorted(attrs_to_remove, reverse=True):
109-
self.attrs.pop(idx)
110-
111-
return {prop.name: prop.render_prop(context) for prop in self.props}
89+
class Value:
90+
raw: str | bool | None
91+
quoted: bool = False
11292

113-
def render_attrs(self, context: Context) -> SafeString:
114-
rendered = " ".join(attr.render_attr(context) for attr in self.attrs)
115-
return mark_safe(rendered)
93+
def resolve(self, context: Context | dict[str, Any]) -> Any:
94+
if self.raw is None or (isinstance(self.raw, str) and self.raw == "False"):
95+
return None
96+
if (isinstance(self.raw, bool) and self.raw) or (
97+
isinstance(self.raw, str) and self.raw == "True"
98+
):
99+
return True
100+
if isinstance(self.raw, str) and not self.quoted:
101+
try:
102+
return template.Variable(str(self.raw)).resolve(context)
103+
except template.VariableDoesNotExist:
104+
return self.raw
105+
return self.raw

0 commit comments

Comments
 (0)