docs: def _get_all_templates(network_id, template_id):
      """
          Get all the templates for the nodes, links and groups of a network.
          Return these templates as a dictionary, keyed on type (NODE, LINK, GROUP)
          then by ID of the node or link.
      """
      base_qry = db.DBSession.query(
                                 ResourceType.ref_key.label('ref_key'),
                                 ResourceType.node_id.label('node_id'),
                                 ResourceType.link_id.label('link_id'),
                                 ResourceType.group_id.label('group_id'),
                                 ResourceType.network_id.label('network_id'),
                                 Template.name.label('template_name'),
                                 Template.id.label('template_id'),
                                 TemplateType.id.label('type_id'),
                                 TemplateType.layout.label('layout'),
                                 TemplateType.name.label('type_name'),
                                ).filter(TemplateType.id==ResourceType.type_id,
                                         Template.id==TemplateType.template_id)
  
  
      all_node_type_qry = base_qry.filter(Node.id==ResourceType.node_id,
                                          Node.network_id==network_id)
  
      all_link_type_qry = base_qry.filter(Link.id==ResourceType.link_id,
                                          Link.network_id==network_id)
  
      all_group_type_qry = base_qry.filter(ResourceGroup.id==ResourceType.group_id,
                                           ResourceGroup.network_id==network_id)
  
      network_type_qry = base_qry.filter(ResourceType.network_id==network_id)
  
      #Filter the group attributes by template
      if template_id is not None:
          all_node_type_qry = all_node_type_qry.filter(Template.id==template_id)
          all_link_type_qry = all_link_type_qry.filter(Template.id==template_id)
          all_group_type_qry = all_group_type_qry.filter(Template.id==template_id)
  
      x = time.time()
      log.info("Getting all types")
      type_qry = all_node_type_qry.union(all_link_type_qry, all_group_type_qry, network_type_qry)
      all_types = db.DBSession.execute(type_qry.statement).fetchall()
      log.info("%s types retrieved in %s", len(all_types), time.time()-x)
  
  
      log.info("Attributes retrieved. Processing results...")
      x = time.time()
      node_type_dict = dict()
      link_type_dict = dict()
      group_type_dict = dict()
      network_type_dict = dict()
  
      for t in all_types:
          templatetype = JSONObject({
                                      'template_id':t.template_id,
                                     'id':t.type_id,
                                     'template_name':t.template_name,
                                     'layout': t.layout,
                                     'name': t.type_name,})
  
          if t.ref_key == 'NODE':
              nodetype = node_type_dict.get(t.node_id, [])
              nodetype.append(templatetype)
              node_type_dict[t.node_id] = nodetype
          elif t.ref_key == 'LINK':
              linktype = link_type_dict.get(t.link_id, [])
              linktype.append(templatetype)
              link_type_dict[t.link_id] = linktype
          elif t.ref_key == 'GROUP':
              grouptype = group_type_dict.get(t.group_id, [])
              grouptype.append(templatetype)
              group_type_dict[t.group_id] = grouptype
          elif t.ref_key == 'NETWORK':
              nettype = network_type_dict.get(t.network_id, [])
              nettype.append(templatetype)
              network_type_dict[t.network_id] = nettype
  
  
      all_types = {
          'NODE' : node_type_dict,
          'LINK' : link_type_dict,
          'GROUP': group_type_dict,
          'NETWORK': network_type_dict,
      }
  
      logging.info("Attributes processed in %s", time.time()-x)
      return all_types