def process_document_tree(root, context): """Iterate and optionally update children of provided root node. Rendering is based on type of node. Further calls to this function happen when a node with children exists - and so on. Only nodes with the bungeni namespace tags "br:type" are modified with content from the provided context. """ cond = get_attr(root, "condition") if cond and not check_exists(context, cond): return None iter_children = root.getchildren() or [root] if not (root in iter_children): root_typ = get_attr(root, "type") if root_typ: process_single_node(root, context, root_typ, get_attr(root, "source")) for child in iter_children: typ = get_attr(child, "type") src = get_attr(child, "source") cond = get_attr(child, "condition") if cond and not check_exists(context, cond): drop_element(child) continue children = child.getchildren() if len(children) == 0: if typ: process_single_node(child, context, typ, src) else: if typ: if typ == "listing": clean_element(child) children = child.getchildren() listing = get_element_value(context, src, default=[]) if IAlchemistContainer.providedBy(listing): listing = [item for item in common.list_container_items(listing)] len_listing = len(listing) expanded_children = [deepcopy(children) for x in range(len_listing)] empty_element(child) if len(listing) == 0: no_items_tag = "p" if child.tag == "tr": no_items_tag = "td" no_items_node = etree.SubElement(child, no_items_tag) no_items_node.text = translate_i18n(_(u"No items found")) else: for (index, item) in enumerate(listing): for inner_element in expanded_children[index]: iroot = process_document_tree(inner_element, item) if iroot is not None: child.append(iroot) elif typ == "block" and src: block_context = get_element_value(context, src, default=None) process_document_tree(child, block_context) else: process_document_tree(child, context) else: process_document_tree(child, context) clean_element(root) return root
def process_document_tree(root, context): """Iterate and optionally update children of provided root node. Rendering is based on type of node. Further calls to this function happen when a node with children exists - and so on. Only nodes with the bungeni namespace tags "br:type" are modified with content from the provided context. """ iter_children = root.getchildren() or [root] if not (root in iter_children): root_typ = get_attr(root, "type") if root_typ: process_single_node(root, context, root_typ, get_attr(root, "source")) for child in iter_children: typ = get_attr(child, "type") src = get_attr(child, "source") children = child.getchildren() if len(children) == 0: if typ: process_single_node(child, context, typ, src) else: if typ: if typ == "listing": clean_element(child) children = child.getchildren() listing = get_element_value(context, src, default=[]) if IAlchemistContainer.providedBy(listing): listing = [ item for item in common.list_container_items(listing) ] len_listing = len(listing) expanded_children = [ deepcopy(children) for x in range(len_listing) ] empty_element(child) if len(listing) == 0: no_items_tag = "p" if child.tag == "tr": no_items_tag = "td" no_items_node = etree.SubElement( child, no_items_tag) no_items_node.text = translate_i18n( _(u"No items found")) else: for (index, item) in enumerate(listing): for inner_element in expanded_children[ index]: iroot = process_document_tree( inner_element, item) child.append(iroot) else: process_document_tree(child, context) else: process_document_tree(child, context) return root
def generate_tree(root, context): for element in root.getiterator(): typ = get_attr(element, "type") src = get_attr(element, "source") if typ: if typ=="text": clean_element(element) element.text = get_element_value(context, src) elif typ=="link": clean_element(element) url_source = get_attr(element, "url") if url_source: link_url = get_element_value(context, url_source) else: link_url = url.absoluteURL(context, common.get_request() ) element.attrib["href"] = link_url if src: element.text = get_element_value(context, src) elif typ=="html": clean_element(element) _html = u"<div>%s</div>" % get_element_value(context, src ) new_html = element.insert(0, etree.fromstring(_html)) elif typ=="listing": listing = get_element_value(context, src, default=[]) if IAlchemistContainer.providedBy(listing): _listing = common.list_container_items(listing) listing = [ item for item in _listing ] log.debug("[LISTING] %s @@ %s", src, listing) listing_count = len(listing) new_children = [ deepcopy(element.getchildren()) for x in range(listing_count) ] empty_element(element) clean_element(element) if listing_count == 0: parent = element.getparent() no_items_element = etree.SubElement(element, "p") no_items_element.text = translate_i18n( _(u"No items found") ) else: for (index, item) in enumerate(listing): for child in new_children[index]: generate_tree(child, item) for children in new_children: for descendant in children: element.append(descendant) break return etree.tostring(root)
def generate_tree(root, context): for element in root.getiterator(): typ = get_attr(element, "type") src = get_attr(element, "source") if typ: if typ == "text": clean_element(element) element.text = get_element_value(context, src) elif typ == "link": clean_element(element) url_source = get_attr(element, "url") if url_source: link_url = get_element_value(context, url_source) else: link_url = url.absoluteURL(context, common.get_request()) element.attrib["href"] = link_url if src: element.text = get_element_value(context, src) elif typ == "html": clean_element(element) _html = u"<div>%s</div>" % get_element_value( context, src) new_html = element.insert(0, etree.fromstring(_html)) elif typ == "listing": listing = get_element_value(context, src, default=[]) if IAlchemistContainer.providedBy(listing): _listing = common.list_container_items(listing) listing = [item for item in _listing] log.debug("[LISTING] %s @@ %s", src, listing) listing_count = len(listing) new_children = [ deepcopy(element.getchildren()) for x in range(listing_count) ] empty_element(element) clean_element(element) if listing_count == 0: parent = element.getparent() no_items_element = etree.SubElement(element, "p") no_items_element.text = translate_i18n( _(u"No items found")) else: for (index, item) in enumerate(listing): for child in new_children[index]: generate_tree(child, item) for children in new_children: for descendant in children: element.append(descendant) break return etree.tostring(root)
def load_formatted_container_items(container, out_format={}, extra_params={}): """Load container items and return as a list of formatted dictionary items. params: extra_params: a dictionary of extra parameters to include in dict out_format: property titles and getters getters based acting on item """ formatted_items = [] if interfaces.IAlchemistContainer.providedBy(container): item_list = common.list_container_items(container) else: item_list = [removeSecurityProxy(item) for item in container] for item in item_list: item_dict = {} item_dict.update(extra_params) map(lambda fmt: item_dict.update([(fmt[0], fmt[1](item))]), out_format.iteritems()) formatted_items.append(item_dict) return formatted_items
def load_formatted_container_items(container, out_format={}, extra_params={}): """Load container items and return as a list of formatted dictionary items. params: extra_params: a dictionary of extra parameters to include in dict out_format: property titles and getters getters based acting on item """ formatted_items = [] if interfaces.IAlchemistContainer.providedBy(container): item_list = common.list_container_items(container) else: item_list = [ removeSecurityProxy(item) for item in container ] for item in item_list: item_dict = {} item_dict.update(extra_params) map( lambda fmt:item_dict.update([ ( fmt[0], fmt[1](item) ) ]), out_format.iteritems() ) formatted_items.append(item_dict) return formatted_items
def process_document_tree(root, context): """Iterate and optionally update children of provided root node. Rendering is based on type of node. Further calls to this function happen when a node with children exists - and so on. Only nodes with the bungeni namespace tags "br:type" are modified with content from the provided context. """ cond = get_attr(root, "condition") if cond and not check_exists(context, cond): return None iter_children = root.getchildren() or [root] if not (root in iter_children): root_typ = get_attr(root, "type") if root_typ: process_single_node(root, context, root_typ, get_attr(root, "source")) for child in iter_children: typ = get_attr(child, "type") src = get_attr(child, "source") cond = get_attr(child, "condition") if cond and not check_exists(context, cond): drop_element(child) continue children = child.getchildren() if len(children) == 0: if typ: process_single_node(child, context, typ, src) else: if not typ: process_document_tree(child, context) elif typ == "listing": child = clean_element(child) children = child.getchildren() listing = get_element_value(context, src, default=[]) if IAlchemistContainer.providedBy(listing): listing = [ item for item in common.list_container_items( listing) ] len_listing = len(listing) if not len_listing: add_empty_listing_node(child) else: expanded_children = [ deepcopy(children) for x in range(len_listing) ] empty_element(child) for i, item in enumerate(listing): for inner_element in expanded_children[i]: iroot = process_document_tree( inner_element, item) if iroot is not None: child.append(iroot) elif typ == "block" and src: block_context = get_element_value(context, src, default=None) process_document_tree(child, block_context) else: process_document_tree(child, context) return clean_element(root)