old_contents: """
  Utility functions for parsing the HTML generated by the markup renderer.
  """
  
  from django.core.urlresolvers import resolve
  
  from ..models import Note, Topic, Document
  
  
  MODELS_BY_LABEL = {
      'note': Note,
      'topic': Topic,
      'document': Document
  }
  
  
  def models_by_id(Model, ids):
      return Model.objects.filter(id__in=ids)
  
  
  def rel_els_from_tree(item_type, tree):
      return tree.xpath('//*[@rel="{rel_base}{item_type}"]'.format(
          rel_base='http://editorsnotes.org/v#',
          item_type=item_type
      ))
  
  
  def kwargs_from_rel_els(rel_els):
      "Resolve the URLs of embedded items, and return their url config kwargs"
      urls = {el.attrib['href'] for el in rel_els}
      return [resolve(url).kwargs for url in urls]
  
  
  def get_embedded_item_urls(tree):
      return {
          item_type: [
              el.attrib['href'] for el in rel_els_from_tree(item_type, tree)
          ]
          for item_type in ('note', 'topic', 'document')
      }
  
  
  def qs_from_urls(model_class, urls):
      ids = [resolve(url).kwargs['pk'] for url in urls]
      return models_by_id(model_class, ids)
  
  
  def get_embedded_models(tree):
      embedded_item_urls = get_embedded_item_urls(tree)
  
      return {
          item_type: qs_from_urls(MODELS_BY_LABEL[item_type], urls)
          for item_type, urls in embedded_item_urls.items()
      }
  
new_contents: """
  Utility functions for parsing the HTML generated by the markup renderer.
  """
  
  from django.core.urlresolvers import resolve
  
  from ..models import Note, Topic, Document
  
  
  MODELS_BY_LABEL = {
      'note': Note,
      'topic': Topic,
      'document': Document
  }
  
  
  def models_by_id(Model, ids):
      if Model == Topic:
          return Model.objects.filter(topic_node_id__in=ids)
      return Model.objects.filter(id__in=ids)
  
  
  def rel_els_from_tree(item_type, tree):
      return tree.xpath('//*[@rel="{rel_base}{item_type}"]'.format(
          rel_base='http://editorsnotes.org/v#',
          item_type=item_type
      ))
  
  
  def kwargs_from_rel_els(rel_els):
      "Resolve the URLs of embedded items, and return their url config kwargs"
      urls = {el.attrib['href'] for el in rel_els}
      return [resolve(url).kwargs for url in urls]
  
  
  def get_embedded_item_urls(tree):
      return {
          item_type: [
              el.attrib['href'] for el in rel_els_from_tree(item_type, tree)
          ]
          for item_type in ('note', 'topic', 'document')
      }
  
  
  def id_from_kwargs(kwargs):
      if 'topic_node_id' in kwargs:
          return kwargs['topic_node_id']
      return kwargs['pk']
  
  
  def qs_from_urls(model_class, urls):
      ids = [id_from_kwargs(resolve(url).kwargs) for url in urls]
      return models_by_id(model_class, ids)
  
  
  def get_embedded_models(tree):
      embedded_item_urls = get_embedded_item_urls(tree)
  
      return {
          item_type: qs_from_urls(MODELS_BY_LABEL[item_type], urls)
          for item_type, urls in embedded_item_urls.items()
      }
  
subject: Fix some bugs mixing up topic_node_id with pk