(domain, location_id, app_id)
for a location search for enabled app releases for all parent locations.
    Child location's setting takes precedence over parent
for a location search for enabled app releases for all parent locations.
    Child location's setting takes precedence over parent
for a location search for enabled app releases for all parent locations . Child location s setting takes precedence over parent
def get_latest_app_release_by_location(domain, location_id, app_id):
    """
    for a location search for enabled app releases for all parent locations.
    Child location's setting takes precedence over parent
    """
    from corehq.apps.app_manager.models import AppReleaseByLocation
    location = SQLLocation.active_objects.get(location_id=location_id)
    location_and_ancestor_ids = location.get_ancestors(include_self=True).values_list(
        'location_id', flat=True).reverse()
    # get all active enabled releases and order by version desc to get one with the highest version in the end
    # for a location. Do not use the first object itself in order to respect the location hierarchy and use
    # the closest location to determine the valid active release
    latest_enabled_releases = {
        release.location_id: release.build_id
        for release in
        AppReleaseByLocation.objects.filter(
            location_id__in=location_and_ancestor_ids, app_id=app_id, domain=domain, active=True).order_by(
            'version')
    }
    for loc_id in location_and_ancestor_ids:
        build_id = latest_enabled_releases.get(loc_id)
        if build_id:
            return get_app(domain, build_id)
def get_latest_app_release_by_location ( domain , location_id , app_id ) : from corehq . apps . app_manager . models import AppReleaseByLocation location = SQLLocation . active_objects . get ( location_id = location_id ) location_and_ancestor_ids = location . get_ancestors ( include_self = True ) . values_list ( 'location_id' , flat = True ) . reverse ( ) # get all active enabled releases and order by version desc to get one with the highest version in the end # for a location. Do not use the first object itself in order to respect the location hierarchy and use # the closest location to determine the valid active release latest_enabled_releases = { release . location_id : release . build_id for release in AppReleaseByLocation . objects . filter ( location_id__in = location_and_ancestor_ids , app_id = app_id , domain = domain , active = True ) . order_by ( 'version' ) } for loc_id in location_and_ancestor_ids : build_id = latest_enabled_releases . get ( loc_id ) if build_id : return get_app ( domain , build_id )