from corehq.apps.domain.models import Domain
from dimagi.utils.couch.database import get_db


def get_doc_ids_in_domain_by_class(domain, doc_class):
    db = doc_class.get_db()
    doc_type = doc_class.__name__
    return get_doc_ids_in_domain_by_type(domain, doc_type, db)


def get_doc_ids_in_domain_by_type(domain, doc_type, database=None):
    """
    Given a domain and doc type, get all docs matching that domain and type
    """
    if not database:
        database = get_db()
    return [row['id'] for row in database.view('domain/docs',
        startkey=[domain, doc_type],
        endkey=[domain, doc_type, {}],
        reduce=False,
        include_docs=False,
    )]


def get_docs_in_domain_by_class(domain, doc_class):
    """
    Given a domain and doc class, get all docs matching that domain and type

    in order to prevent this from being used on a doc_class with many docs per domain
    doc_class must be white-listed.
    """
    whitelist = [
        'CallCenterIndicatorConfig',
        'CommtrackConfig',
    ]
    doc_type = doc_class.__name__
    assert doc_type in whitelist
    return doc_class.view(
        'domain/docs',
        startkey=[domain, doc_type],
        endkey=[domain, doc_type, {}],
        reduce=False,
        include_docs=True,
    ).all()


def get_domain_ids_by_names(names):
    return [result['id'] for result in Domain.view(
        "domain/domains",
        keys=names,
        reduce=False,
        include_docs=False
    )]

from corehq.apps.domain.models import Domain
from dimagi.utils.couch.database import get_db


def get_doc_ids_in_domain_by_class(domain, doc_class):
    db = doc_class.get_db()
    doc_type = doc_class.__name__
    return get_doc_ids_in_domain_by_type(domain, doc_type, db)


def get_doc_ids_in_domain_by_type(domain, doc_type, database=None):
    """
    Given a domain and doc type, get all docs matching that domain and type
    """
    if not database:
        database = get_db()
    return [row['id'] for row in database.view('domain/docs',
        startkey=[domain, doc_type],
        endkey=[domain, doc_type, {}],
        reduce=False,
        include_docs=False,
    )]


def get_docs_in_domain_by_class(domain, doc_class):
    """
    Given a domain and doc class, get all docs matching that domain and type

    in order to prevent this from being used on a doc_class with many docs per domain
    doc_class must be white-listed.
    """
    whitelist = [
        'CallCenterIndicatorConfig',
        'CommtrackConfig',
        'Invitation',
    ]
    doc_type = doc_class.__name__
    assert doc_type in whitelist
    return doc_class.view(
        'domain/docs',
        startkey=[domain, doc_type],
        endkey=[domain, doc_type, {}],
        reduce=False,
        include_docs=True,
    ).all()


def get_domain_ids_by_names(names):
    return [result['id'] for result in Domain.view(
        "domain/domains",
        keys=names,
        reduce=False,
        include_docs=False
    )]

add doc_type to whitelist