Support field_name argument in honeypot jinja extension

This commit is contained in:
Antoine Bartuccio 2024-07-22 11:34:22 +02:00
parent 58ff5b934a
commit c7f8cdd098

View File

@ -47,12 +47,23 @@ class HoneypotExtension(Extension):
def parse(self, parser: Parser) -> nodes.Output: def parse(self, parser: Parser) -> nodes.Output:
lineno = parser.stream.expect("name:render_honeypot_field").lineno lineno = parser.stream.expect("name:render_honeypot_field").lineno
key = nodes.Name("render_honeypot_field", "load", lineno=lineno)
if parser.stream.current.type != "block_end":
field_name = parser.parse_expression()
else:
field_name = nodes.Const(None)
call = self.call_method( call = self.call_method(
"_render", "_render",
[nodes.Name("render_honeypot_field", "load", lineno=lineno)], [key, field_name],
lineno=lineno, lineno=lineno,
) )
return nodes.Output([nodes.MarkSafe(call)]) return nodes.Output([nodes.MarkSafe(call)])
def _render(self, render_honeypot_field: Callable[[str | None], str]): def _render(
return render_to_string("honeypot/honeypot_field.html", render_honeypot_field()) self,
render_honeypot_field: Callable[[str | None], str],
field_name: str | None = None,
):
return render_to_string(
"honeypot/honeypot_field.html", render_honeypot_field(field_name=field_name)
)