def isSourceCacheable(context, node): """ Helps to see if the Document using an external source defined in the XML node is cacheable. """ id = node.getAttribute('id').encode('ascii') source = getSourceForId(context, id) if source is None: return 1 parameters = getSourceParameters(context, node) is_cacheable = source.is_cacheable(**parameters) return is_cacheable
id = request.get('id') if id is None: # no id nor params saved. removeParameterElements(node) node.removeAttribute('id') return if node.getAttribute('id') != id: # is changed, so remove all attrs. node.setAttribute('id', ustr(id)) # Use Document's context. If not, the complete widgets hierarchy comes # in play and weird acquisition magic may cause 'source' to be a widget # whenever the source object has a widget's id. doc = node.get_silva_object() source = getSourceForId(doc, id) if source is None: # should not happen since id was selected from a list... # or should we silently ignore this? raise ValueError, _('Source is a NoneType - is it not available anymore?') form = source.form() try: result = form.validate_all_to_request(request) except (FormValidationError, ValidationError), errs: pass else: removeParameterElements(node) for field in form.get_fields(): param = node.createElement('parameter') param.setAttribute('key', ustr(field.id))
def sax_source(self, node): # simple output reporting to emulate behavior of widgets renderer def source_error(error): html = ['<div class="warning">' '<strong>[external source element is broken]</strong>', '<br />', escape(unicode(error)), '</div>'] self.render_html("".join(html)) self.endElementNS(NS_DOCUMENT_URI, node.nodeName) options = self.getOptions() parameters = {} parameters_type = {} for child in node.childNodes: if child.nodeName == 'parameter': name = str(child.attributes['key'].value) if options.upgrade30 and name in RENAMED_FIELDS: name = RENAMED_FIELDS[name] param_type = child.attributes.get('type') if param_type: parameters_type[name]= str(param_type.value) for grandChild in child.childNodes: text = '' if grandChild.nodeType == Node.TEXT_NODE: if grandChild.nodeValue: text = text + grandChild.nodeValue parameters[name] = text attributes = {} if node.attributes: attributes = get_dict(node.attributes) try: id = attributes['id'] except KeyError: return source = getSourceForId(self.context.get_content(), id) if options.upgrade30: if source is None: logger.error(u"Missing source %s, skipping it.", id) return logger.info(u'Document uses source %s.', id) value_settings = [] seen_fields = set() def convert_parameter(name, value): field = source.parameters.get_field(name) value_settings.append(('marker_field_' + name, '1')) value_settings.append(('field_' + name + '_novalidate', '1')) if field.meta_type == 'ReferenceField': content = self.context.get_content() content_path = '/'.join(content.getPhysicalPath()) to_identifier = getUtility(IIntIds).register def resolve_location(location): url, target, fragment = resolve_path( location, content_path, content, 'code source') if target is not None: value_settings.append( ('field_' + name, str(to_identifier(target)))) is_multiple = field.get_value('multiple') if is_multiple: map(resolve_location, value.split(',')) else: resolve_location(value) else: parameter_type = parameters_type.get(name) if (field.meta_type == 'CheckBoxField' or parameter_type == 'boolean' or isinstance(value, bool)): if value in (True, u'1', u'True'): value = u'1' else: # False boolean are not included in the settings. return if parameter_type == 'list' or isinstance(value, list): if isinstance(value, basestring): value = eval(value) for item in value: value_settings.append( ('field_' + name, unicode(item).encode('utf-8')),) else: value_settings.append( ('field_' + name, unicode(value).encode('utf-8')),) if source.parameters is not None: # Convert actual stored value for name, value in parameters.items(): try: convert_parameter(name, value) seen_fields.add(name) except AttributeError: logger.error( u"Parameter %s missing in source %s." % ( name, id)) # For any field that was not seen, add the (required) # default value for field in source.parameters.get_fields(): if field.id not in seen_fields: default_value = field.get_value('default') if default_value is not None: logger.warn( u"Using default for parameter %s in source %s." % ( field.id, id)) convert_parameter(field.id, default_value) attributes['settings'] = urllib.urlencode(value_settings) self.startElementNS(NS_DOCUMENT_URI, node.nodeName, attributes) if not options.upgrade30: # We don't need to include anything inside a code for the # upgrade, and don't need to render it. # Collect parameters for child in node.childNodes: if child.nodeName == 'parameter': attributes = {'key': child.attributes['key'].value} param_type = child.attributes.get('type') if param_type: attributes['type'] = param_type.value self.startElementNS(NS_DOCUMENT_URI, 'parameter', attributes) for grandChild in child.childNodes: if grandChild.nodeType == Node.TEXT_NODE: if grandChild.nodeValue: self.handler.characters(grandChild.nodeValue) self.endElementNS(NS_DOCUMENT_URI, 'parameter') # Render source if needed if options.external_rendering: request = self.getExported().request try: html = source.to_html(self.context, request, **parameters) except Exception, error: source_error("error message: " + str(error)) return if not html: source_error("error message: the source returned no output.") return self.render_html(html)