blocks: add support for post conditions (#71778)
gitea/wcs/pipeline/head This commit looks good
Details
gitea/wcs/pipeline/head This commit looks good
Details
This commit is contained in:
parent
9e46be8374
commit
dfab955432
|
@ -24,7 +24,7 @@ from contextlib import contextmanager
|
|||
from quixote import get_publisher, get_request, get_response
|
||||
from quixote.html import htmltag, htmltext
|
||||
|
||||
from . import data_sources, fields
|
||||
from . import conditions, data_sources, fields
|
||||
from .categories import BlockCategory
|
||||
from .formdata import FormData
|
||||
from .qommon import _, misc
|
||||
|
@ -435,6 +435,21 @@ class BlockSubWidget(CompositeWidget):
|
|||
all_lists = False
|
||||
if widget_value.get(widget.field.id) not in empty_values:
|
||||
empty = False
|
||||
|
||||
if not empty and self.block.post_conditions:
|
||||
error_messages = []
|
||||
with self.block.visibility_context(value, self.index):
|
||||
for i, post_condition in enumerate(self.block.post_conditions):
|
||||
condition = post_condition.get('condition')
|
||||
error_message = post_condition.get('error_message')
|
||||
try:
|
||||
if not conditions.Condition(condition, record_errors=False).evaluate():
|
||||
error_messages.append(error_message)
|
||||
except RuntimeError:
|
||||
error_messages.append(error_message)
|
||||
if error_messages:
|
||||
self.set_error(' '.join(error_messages))
|
||||
|
||||
if empty and not all_lists and not get_publisher().keep_all_block_rows_mode:
|
||||
value = None
|
||||
for widget in self.get_widgets(): # reset "required" errors
|
||||
|
@ -528,6 +543,10 @@ class BlockWidget(WidgetList):
|
|||
self._parse(request)
|
||||
if self.required and self.value is None:
|
||||
self.set_error(_(self.REQUIRED_ERROR))
|
||||
for widget in self.widgets:
|
||||
# mark required rows with a special attribute, to avoid doubling the
|
||||
# error messages in the template.
|
||||
widget.is_required_error = bool(widget.error == self.REQUIRED_ERROR)
|
||||
return self.value
|
||||
|
||||
def add_media(self):
|
||||
|
|
|
@ -6,6 +6,7 @@
|
|||
{% endblock %}
|
||||
|
||||
{% block widget-content %}
|
||||
{% if not widget.readonly and widget.error and not widget.is_required_error %}<div class="error"><p>{{ widget.error }}</p></div>{% endif %}
|
||||
{% for subwidget in widget.get_widgets %}
|
||||
{% if widget.readonly and not subwidget.field.include_in_validation_page %}<div style="display: none">{% endif %}
|
||||
{{ subwidget.render|safe }}
|
||||
|
|
Loading…
Reference in New Issue