old_contents: from corehq.apps.casegroups.models import CommCareCaseGroup
  
  
  def get_case_groups_in_domain(domain, limit=None, skip=None):
      extra_kwargs = {}
      if limit is not None:
          extra_kwargs['limit'] = limit
      if skip is not None:
          extra_kwargs['skip'] = skip
      return CommCareCaseGroup.view(
          'casegroups/groups_by_domain',
          startkey=[domain],
          endkey=[domain, {}],
          include_docs=True,
          reduce=False,
          **extra_kwargs
      ).all()
  
  
  def get_case_group_meta_in_domain(domain):
      """
      returns a list (id, name) tuples sorted by name
  
      ideal for creating a user-facing dropdown menu, etc.
      """
      return [(r['id'], r['key'][1]) for r in CommCareCaseGroup.view(
          'casegroups/groups_by_domain',
          startkey=[domain],
          endkey=[domain, {}],
          include_docs=False,
          reduce=False,
      ).all()]
  
  
  def get_number_of_case_groups_in_domain(domain):
      data = CommCareCaseGroup.get_db().view(
          'casegroups/groups_by_domain',
          startkey=[domain],
          endkey=[domain, {}],
          reduce=True
      ).first()
      return data['value'] if data else 0
  
new_contents: from corehq.apps.casegroups.models import CommCareCaseGroup
  from corehq.apps.domain.dbaccessors import (
      get_doc_ids_in_domain_by_class,
      get_docs_in_domain_by_class,
  )
  
  
  def get_case_groups_in_domain(domain, limit=None, skip=None):
      def _get_case_groups_generator(domain_name):
          for case_group in get_docs_in_domain_by_class(domain_name, CommCareCaseGroup):
              yield case_group
  
      case_groups_generator = _get_case_groups_generator(domain)
  
      if skip is not None:
          try:
              for _ in range(skip):
                  next(case_groups_generator)
          except StopIteration:
              pass
  
      if limit is not None:
          return list(next(case_groups_generator) for _ in range(limit))
      return list(case_groups_generator)
  
  
  def get_case_group_meta_in_domain(domain):
      """
      returns a list (id, name) tuples sorted by name
  
      ideal for creating a user-facing dropdown menu, etc.
      """
      return map(
          lambda case_group: (case_group._id, case_group.name),
          get_docs_in_domain_by_class(domain, CommCareCaseGroup)
      )
  
  
  def get_number_of_case_groups_in_domain(domain):
      return len(get_doc_ids_in_domain_by_class(domain, CommCareCaseGroup))
  
subject: replace uses of casegroups/groups_by_domain