docs: def _get_all_resource_attributes(network_id, template_id=None):
      """
          Get all the attributes for the nodes, links and groups of a network.
          Return these attributes as a dictionary, keyed on type (NODE, LINK, GROUP)
          then by ID of the node or link.
      """
      base_qry = db.DBSession.query(
                                 ResourceAttr.id.label('id'),
                                 ResourceAttr.ref_key.label('ref_key'),
                                 ResourceAttr.cr_date.label('cr_date'),
                                 ResourceAttr.attr_is_var.label('attr_is_var'),
                                 ResourceAttr.node_id.label('node_id'),
                                 ResourceAttr.link_id.label('link_id'),
                                 ResourceAttr.group_id.label('group_id'),
                                 ResourceAttr.network_id.label('network_id'),
                                 ResourceAttr.attr_id.label('attr_id'),
                                 Attr.name.label('name'),
                                 Attr.dimension_id.label('dimension_id'),
                                ).filter(Attr.id==ResourceAttr.attr_id)
  
  
      all_node_attribute_qry = base_qry.join(Node).filter(Node.network_id==network_id)
  
      all_link_attribute_qry = base_qry.join(Link).filter(Link.network_id==network_id)
  
      all_group_attribute_qry = base_qry.join(ResourceGroup).filter(ResourceGroup.network_id==network_id)
      network_attribute_qry = base_qry.filter(ResourceAttr.network_id==network_id)
  
  
      #Filter the group attributes by template
      if template_id is not None:
          all_node_attribute_qry = all_node_attribute_qry.join(ResourceType).join(TemplateType).join(TypeAttr).filter(TemplateType.template_id==template_id).filter(ResourceAttr.attr_id==TypeAttr.attr_id)
          all_link_attribute_qry = all_link_attribute_qry.join(ResourceType).join(TemplateType).join(TypeAttr).filter(TemplateType.template_id==template_id).filter(ResourceAttr.attr_id==TypeAttr.attr_id)
          all_group_attribute_qry = all_group_attribute_qry.join(ResourceType).join(TemplateType).join(TypeAttr).filter(TemplateType.template_id==template_id).filter(ResourceAttr.attr_id==TypeAttr.attr_id)
          network_attribute_qry = network_attribute_qry.join(ResourceType, ResourceAttr.network_id==ResourceType.network_id).join(TemplateType).join(TypeAttr).filter(TemplateType.template_id==template_id).filter(ResourceAttr.attr_id==TypeAttr.attr_id)
  
      x = time.time()
      logging.info("Getting all attributes using execute")
      attribute_qry = all_node_attribute_qry.union(all_link_attribute_qry, all_group_attribute_qry, network_attribute_qry)
      all_attributes = db.DBSession.execute(attribute_qry.statement).fetchall()
      log.info("%s attrs retrieved in %s", len(all_attributes), time.time()-x)
  
      logging.info("Attributes retrieved. Processing results...")
      x = time.time()
      node_attr_dict = dict()
      link_attr_dict = dict()
      group_attr_dict = dict()
      network_attr_dict = dict()
  
      for attr in all_attributes:
          if attr.ref_key == 'NODE':
              nodeattr = node_attr_dict.get(attr.node_id, [])
              nodeattr.append(attr)
              node_attr_dict[attr.node_id] = nodeattr
          elif attr.ref_key == 'LINK':
              linkattr = link_attr_dict.get(attr.link_id, [])
              linkattr.append(attr)
              link_attr_dict[attr.link_id] = linkattr
          elif attr.ref_key == 'GROUP':
              groupattr = group_attr_dict.get(attr.group_id, [])
              groupattr.append(attr)
              group_attr_dict[attr.group_id] = groupattr
          elif attr.ref_key == 'NETWORK':
              networkattr = network_attr_dict.get(attr.network_id, [])
              networkattr.append(attr)
              network_attr_dict[attr.network_id] = networkattr
  
      all_attributes = {
          'NODE' : node_attr_dict,
          'LINK' : link_attr_dict,
          'GROUP': group_attr_dict,
          'NETWORK': network_attr_dict,
      }
  
      logging.info("Attributes processed in %s", time.time()-x)
      return all_attributes