def json_messages(self, f): '''Convert errors in form into a JSON serializable dictionary with keys given by errors html id.''' dfields = f._fields_dict ListDict = ajax.jhtmls(f.environ) self._add(ListDict,dfields,f.errors,self.form_error_class) self._add(ListDict,dfields,f.messages,self.form_message_class) for fset in f.form_sets.values(): for f in fset.forms: dfields = f._fields_dict self._add(ListDict,dfields,f.errors,self.form_error_class) self._add(ListDict,dfields,f.messages,self.form_message_class) return ListDict
def json_messages(self, f): '''Convert errors in form into a JSON serializable dictionary with keys given by errors html id.''' dfields = f._fields_dict ListDict = ajax.jhtmls(f.environ) self._add(ListDict, dfields, f.errors, self.form_error_class) self._add(ListDict, dfields, f.messages, self.form_message_class) for fset in f.form_sets.values(): for f in fset.forms: dfields = f._fields_dict self._add(ListDict, dfields, f.errors, self.form_error_class) self._add(ListDict, dfields, f.messages, self.form_message_class) return ListDict
def ajax_post_response(self, request, commit=True, plugin_form=True): '''View called when changing the content plugin values. The instance.plugin object is maintained but its fields may change.''' fhtml = self.get_form(request, withdata=True) form = fhtml.form if plugin_form: instance = request.instance prefix = form.prefix if not form.is_valid(): if commit: return fhtml.maker.json_messages(form) else: return ajax.jhtmls(request.environ) data = form.cleaned_data pform = self.get_plugin_form(request, data['plugin_name'], prefix, withdata=commit) #plugin editing form is available if pform is not None: # if plugin form is bound and has error, returns the errors if pform.form.is_bound and not pform.is_valid(): return fhtml.maker.json_messages(pform.form) # Check what action to perform. action = forms.get_ajax_action(request.REQUEST) instance = form.submit(commit=commit) plugin = instance.plugin if action: action_func = getattr(plugin, 'ajax__' + action, None) if action_func: value = forms.get_ajax_action_value( action, request.REQUEST) return action_func(request, value) # if committing, we get the arguments from the form if commit: instance.arguments = plugin.save(pform.form) instance.save() plugin_form = pform.render(request) else: instance = form.submit(commit=commit) plugin_form = '' plugin_form = self.plugin_form_container(instance, plugin_form) jquery = ajax.jhtmls(request.environ, identifier='#' + plugin_form.attr('id'), html=plugin_form.render(request), type='replacewith') else: # we are just rerendering the plugin with a different wrapper instance = form.submit(commit=commit) jquery = ajax.jhtmls(request.environ) preview = self.get_preview(request, instance) jquery.add('#%s' % instance.pluginid('preview'), html.render(request, preview)) if plugin_form: if commit: form.add_message("Plugin changed to %s"\ % instance.plugin.description) jquery.update(fhtml.maker.json_messages(form)) return jquery
def ajax_post_response(self, request, commit=True, plugin_form=True): '''View called when changing the content plugin values. The instance.plugin object is maintained but its fields may change.''' fhtml = self.get_form(request, withdata=True) form = fhtml.form if plugin_form: instance = request.instance prefix = form.prefix if not form.is_valid(): if commit: return fhtml.maker.json_messages(form) else: return ajax.jhtmls(request.environ) data = form.cleaned_data pform = self.get_plugin_form(request, data['plugin_name'], prefix, withdata=commit) #plugin editing form is available if pform is not None: # if plugin form is bound and has error, returns the errors if pform.form.is_bound and not pform.is_valid(): return fhtml.maker.json_messages(pform.form) # Check what action to perform. action = forms.get_ajax_action(request.REQUEST) instance = form.submit(commit=commit) plugin = instance.plugin if action: action_func = getattr(plugin,'ajax__'+action,None) if action_func: value = forms.get_ajax_action_value(action, request.REQUEST) return action_func(request, value) # if committing, we get the arguments from the form if commit: instance.arguments = plugin.save(pform.form) instance.save() plugin_form = pform.render(request) else: instance = form.submit(commit=commit) plugin_form = '' plugin_form = self.plugin_form_container(instance, plugin_form) jquery = ajax.jhtmls(request.environ, identifier = '#' + plugin_form.attr('id'), html = plugin_form.render(request), type = 'replacewith') else: # we are just rerendering the plugin with a different wrapper instance = form.submit(commit=commit) jquery = ajax.jhtmls(request.environ) preview = self.get_preview(request, instance) jquery.add('#%s' % instance.pluginid('preview'), html.render(request, preview)) if plugin_form: if commit: form.add_message("Plugin changed to %s"\ % instance.plugin.description) jquery.update(fhtml.maker.json_messages(form)) return jquery