misc: add check condition example js
This commit is contained in:
parent
b703a78a5b
commit
228790d21e
|
@ -892,6 +892,7 @@ class WidgetField(Field):
|
|||
form.add(self.widget_class, 'f%s' % self.id, title=self.label, hint=hint, **kwargs)
|
||||
widget = form.get_widget('f%s' % self.id)
|
||||
widget.field = self
|
||||
widget.supports_check_condition = True
|
||||
if self.extra_css_class:
|
||||
if hasattr(widget, 'extra_css_class') and widget.extra_css_class:
|
||||
widget.extra_css_class = '%s %s' % (widget.extra_css_class, self.extra_css_class)
|
||||
|
@ -3898,7 +3899,9 @@ class BlockField(WidgetField):
|
|||
self.block
|
||||
except KeyError:
|
||||
raise MissingBlockFieldError(self.type[6:])
|
||||
return super().add_to_form(form, value=value)
|
||||
widget = super().add_to_form(form, value=value)
|
||||
widget.supports_check_condition = False
|
||||
return widget
|
||||
|
||||
def fill_admin_form(self, form):
|
||||
super().fill_admin_form(form)
|
||||
|
|
|
@ -270,6 +270,7 @@ class FormPage(FormdefDirectoryBase, FormTemplateMixin):
|
|||
'code',
|
||||
'removedraft',
|
||||
'live',
|
||||
'check_condition',
|
||||
('go-to-backoffice', 'go_to_backoffice'),
|
||||
]
|
||||
|
||||
|
@ -929,6 +930,9 @@ class FormPage(FormdefDirectoryBase, FormTemplateMixin):
|
|||
def create_form(self, *args, **kwargs):
|
||||
form = self.formdef.create_form(*args, **kwargs)
|
||||
form.attrs['data-live-url'] = self.formdef.get_url(language=get_publisher().current_language) + 'live'
|
||||
form.attrs['data-check-condition-url'] = (
|
||||
self.formdef.get_url(language=get_publisher().current_language) + 'check_condition'
|
||||
)
|
||||
return form
|
||||
|
||||
def create_view_form(self, *args, **kwargs):
|
||||
|
@ -1656,6 +1660,41 @@ class FormPage(FormdefDirectoryBase, FormTemplateMixin):
|
|||
return result_error('form deserialization failed: %s' % e)
|
||||
return FormStatusPage.live_process_fields(form, formdata, displayed_fields)
|
||||
|
||||
def check_condition(self):
|
||||
get_request().ignore_session = True
|
||||
get_response().set_content_type('application/json')
|
||||
|
||||
def result_error(reason):
|
||||
return json.dumps({'result': 'error', 'reason': reason})
|
||||
|
||||
session = get_session()
|
||||
if not (session and session.id):
|
||||
return result_error('missing session')
|
||||
|
||||
field_ref = get_request().form.get('field')
|
||||
parts = field_ref.split('__')
|
||||
for field in self.formdef.fields:
|
||||
if 'f%s' % field.id == parts[0]:
|
||||
break
|
||||
else:
|
||||
return result_error('missing field')
|
||||
if len(parts) == 3: # block field
|
||||
for subfield in field.block.fields:
|
||||
if 'f%s' % subfield.id == parts[2]:
|
||||
break
|
||||
else:
|
||||
return result_error('missing sub field')
|
||||
field = subfield
|
||||
field.id = field_ref[1:].replace('__', '$')
|
||||
|
||||
form = Form()
|
||||
widget = field.add_to_form(form)
|
||||
widget.parse()
|
||||
if widget.has_error():
|
||||
return json.dumps({'err': 1, 'msg': str(widget.get_error())})
|
||||
else:
|
||||
return json.dumps({'err': 0})
|
||||
|
||||
def clean_submission_context(self):
|
||||
get_publisher().substitutions.unfeed(lambda x: x.__class__.__name__ == 'ConditionVars')
|
||||
get_publisher().substitutions.unfeed(lambda x: isinstance(x, FormData))
|
||||
|
@ -2226,7 +2265,7 @@ class RootDirectory(AccessControlled, Directory):
|
|||
|
||||
|
||||
class PublicFormStatusPage(FormStatusPage):
|
||||
_q_exports_orig = ['', 'download', 'status', 'live', 'tempfile']
|
||||
_q_exports_orig = ['', 'download', 'status', 'live', 'tempfile', 'check_condition']
|
||||
form_page_class = FormPage
|
||||
history_templates = ['wcs/front/formdata_history.html', 'wcs/formdata_history.html']
|
||||
status_templates = ['wcs/front/formdata_status.html', 'wcs/formdata_status.html']
|
||||
|
|
|
@ -756,3 +756,21 @@ $(function() {
|
|||
});
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
function check_condition(button, widget_id) {
|
||||
var $form = $('form[data-check-condition-url]');
|
||||
var form_content = $form.serialize();
|
||||
$.ajax({
|
||||
type: 'POST',
|
||||
url: $form.attr('data-check-condition-url') + '?field=' + widget_id,
|
||||
data: form_content,
|
||||
success: function(json) {
|
||||
if (json.err === 0) {
|
||||
button.textContent = 'ok';
|
||||
} else {
|
||||
button.textContent = 'err: ' + json.msg;
|
||||
}
|
||||
},
|
||||
});
|
||||
}
|
||||
|
|
|
@ -45,11 +45,14 @@
|
|||
{% if widget.render_br %}
|
||||
<br class="content {{widget.content.content_extra_css_class}}">
|
||||
{% endif %}
|
||||
{% block widget-error-templates %}
|
||||
{% if not widget.readonly %}
|
||||
{% for error_message in widget.get_error_messages %}
|
||||
<template id="error_{{ widget.get_name_for_id }}_{{ error_message.camel_code }}">{{ error_message.label }}</template>
|
||||
{% endfor %}
|
||||
{% endif %}
|
||||
{% endblock %}
|
||||
{% if widget.supports_check_condition %}
|
||||
{% block widget-error-templates %}
|
||||
{% if not widget.readonly %}
|
||||
{% for error_message in widget.get_error_messages %}
|
||||
<template id="error_{{ widget.get_name_for_id }}_{{ error_message.camel_code }}">{{ error_message.label }}</template>
|
||||
{% endfor %}
|
||||
{% endif %}
|
||||
{% endblock %}
|
||||
<button type="button" onclick="check_condition(this, '{{ widget.get_name_for_id }}')">check condition</button>
|
||||
{% endif %}
|
||||
</div>
|
||||
|
|
Loading…
Reference in New Issue