Commit 9fea8c1d authored by Serge S. Koval's avatar Serge S. Koval

Prettified MongoEngine form rule-based rendering and added Text/HTML rules

parent 0d7d466d
......@@ -99,7 +99,8 @@ class PostView(ModelView):
'inner': {
'form_subdocuments': {
None: {
'form_rules': ('name', 'tags', rules.Header('Comment'), 'value')
# Add <hr> at the end of the form
'form_rules': ('name', 'tag', 'value', rules.HTML('<hr>'))
}
}
}
......
......@@ -93,6 +93,39 @@ class NestedRule(BaseRule):
return Markup(self.separator.join(result))
class Text(BaseRule):
"""
Render text (or HTML snippet) from string.
"""
def __init__(self, text, escape=True):
"""
Constructor.
:param text:
Text snippet to render
:param escape:
Should text be escaped or not. Default is `True`.
"""
super(Text, self).__init__()
self.text = text
self.escape = escape
def __call__(self, form, form_opts=None, field_args={}):
if self.escape:
return self.text
return Markup(self.text)
class HTML(Text):
"""
Shortcut for `Text` rule with `escape` set to `False.
"""
def __init__(self, html):
super(HTML, self).__init__(html, escape=False)
class Macro(BaseRule):
"""
Render macro by its name from current Jinja2 context.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment