def query_per_case_submissions_facet(domain, username=None, limit=100):
    query = {
        "facets": {
            "case_submissions": {
                "terms": {
                    "script_field": case_script_field()['script_case_id']['script'],
                    "size": limit
                },
                "facet_filter": {
                    "and": [
                        {
                            "term": {
                                "domain.exact": domain
                            }
                        }
                    ]
                }
            }
        },
        "size": 0
    }

    if username is not None:
        query['facets']['case_submissions']['facet_filter']['and'].append(
            {"term": {"form.meta.username": username}})
    return query
alternate: django-admin builtin commands fail if settings file (from environment) doesn't exist
dataset/ETHPy150Open dimagi/commcare-hq/custom/_legacy/pact/utils.py/query_per_case_submissions_facet--crossover with--dataset/ETHPy150Open AppScale/appscale/AppServer/lib/django-1.2/tests/regressiontests/admin_scripts/tests.py/DjangoAdminAlternateSettings.test_builtin_with_bad_environment