(doc_dict)
return doc_ret
Given xform JSON such as that returned by `XFormInstanceSQL.to_json()`,
    return a copy that is ready to be sent to elasticsearch, or None, if the
    form should not be saved to elasticsearch
Given xform JSON such as that returned by `XFormInstanceSQL.to_json()`,
    return a copy that is ready to be sent to elasticsearch, or None, if the
    form should not be saved to elasticsearch
Given xform JSON such as that returned by XFormInstanceSQL . to_json () return a copy that is ready to be sent to elasticsearch or None if the form should not be saved to elasticsearch
def transform_xform_for_elasticsearch(doc_dict):
    """
    Given xform JSON such as that returned by `XFormInstanceSQL.to_json()`,
    return a copy that is ready to be sent to elasticsearch, or None, if the
    form should not be saved to elasticsearch
    """
    doc_ret = copy.deepcopy(doc_dict)

    if 'meta' in doc_ret['form']:
        if not is_valid_date(doc_ret['form']['meta'].get('timeEnd', None)):
            doc_ret['form']['meta']['timeEnd'] = None
        if not is_valid_date(doc_ret['form']['meta'].get('timeStart', None)):
            doc_ret['form']['meta']['timeStart'] = None

        # Some docs have their @xmlns and #text here
        if isinstance(doc_ret['form']['meta'].get('appVersion'), dict):
            doc_ret['form']['meta'] = format_form_meta_for_es(doc_ret['form']['meta'])

        app_version_info = get_app_version_info(
            doc_ret['domain'],
            doc_ret.get('build_id'),
            doc_ret.get('version'),
            doc_ret['form']['meta'],
        )
        doc_ret['form']['meta']['commcare_version'] = app_version_info.commcare_version
        doc_ret['form']['meta']['app_build_version'] = app_version_info.build_version

        try:
            geo_point = GeoPointProperty().wrap(doc_ret['form']['meta']['location'])
            doc_ret['form']['meta']['geo_point'] = geo_point.lat_lon
        except (KeyError, BadValueError):
            doc_ret['form']['meta']['geo_point'] = None
            pass

    try:
        user_id = doc_ret['form']['meta']['userID']
    except KeyError:
        user_id = None
    doc_ret['user_type'] = get_user_type(user_id)
    doc_ret['inserted_at'] = datetime.datetime.utcnow().isoformat()

    try:
        case_blocks = extract_case_blocks(doc_ret)
    except PhoneDateValueError:
        pass
    else:
        for case_dict in case_blocks:
            for date_modified_key in ['date_modified', '@date_modified']:
                if not is_valid_date(case_dict.get(date_modified_key, None)):
                    if case_dict.get(date_modified_key) == '':
                        case_dict[date_modified_key] = None
                    else:
                        case_dict.pop(date_modified_key, None)

            # convert all mapped dict properties to nulls if they are empty strings
            for object_key in ['index', 'attachment', 'create', 'update']:
                if object_key in case_dict and not isinstance(case_dict[object_key], dict):
                    case_dict[object_key] = None

        try:
            doc_ret["__retrieved_case_ids"] = list(set(case_update_from_block(cb).id for cb in case_blocks))
        except CaseGenerationException:
            doc_ret["__retrieved_case_ids"] = []

    if 'backend_id' not in doc_ret:
        doc_ret['backend_id'] = 'couch'

    return doc_ret
def transform_xform_for_elasticsearch ( doc_dict ) : doc_ret = copy . deepcopy ( doc_dict ) if 'meta' in doc_ret [ 'form' ] : if not is_valid_date ( doc_ret [ 'form' ] [ 'meta' ] . get ( 'timeEnd' , None ) ) : doc_ret [ 'form' ] [ 'meta' ] [ 'timeEnd' ] = None if not is_valid_date ( doc_ret [ 'form' ] [ 'meta' ] . get ( 'timeStart' , None ) ) : doc_ret [ 'form' ] [ 'meta' ] [ 'timeStart' ] = None # Some docs have their @xmlns and #text here if isinstance ( doc_ret [ 'form' ] [ 'meta' ] . get ( 'appVersion' ) , dict ) : doc_ret [ 'form' ] [ 'meta' ] = format_form_meta_for_es ( doc_ret [ 'form' ] [ 'meta' ] ) app_version_info = get_app_version_info ( doc_ret [ 'domain' ] , doc_ret . get ( 'build_id' ) , doc_ret . get ( 'version' ) , doc_ret [ 'form' ] [ 'meta' ] , ) doc_ret [ 'form' ] [ 'meta' ] [ 'commcare_version' ] = app_version_info . commcare_version doc_ret [ 'form' ] [ 'meta' ] [ 'app_build_version' ] = app_version_info . build_version try : geo_point = GeoPointProperty ( ) . wrap ( doc_ret [ 'form' ] [ 'meta' ] [ 'location' ] ) doc_ret [ 'form' ] [ 'meta' ] [ 'geo_point' ] = geo_point . lat_lon except ( KeyError , BadValueError ) : doc_ret [ 'form' ] [ 'meta' ] [ 'geo_point' ] = None pass try : user_id = doc_ret [ 'form' ] [ 'meta' ] [ 'userID' ] except KeyError : user_id = None doc_ret [ 'user_type' ] = get_user_type ( user_id ) doc_ret [ 'inserted_at' ] = datetime . datetime . utcnow ( ) . isoformat ( ) try : case_blocks = extract_case_blocks ( doc_ret ) except PhoneDateValueError : pass else : for case_dict in case_blocks : for date_modified_key in [ 'date_modified' , '@date_modified' ] : if not is_valid_date ( case_dict . get ( date_modified_key , None ) ) : if case_dict . get ( date_modified_key ) == '' : case_dict [ date_modified_key ] = None else : case_dict . pop ( date_modified_key , None ) # convert all mapped dict properties to nulls if they are empty strings for object_key in [ 'index' , 'attachment' , 'create' , 'update' ] : if object_key in case_dict and not isinstance ( case_dict [ object_key ] , dict ) : case_dict [ object_key ] = None try : doc_ret [ "__retrieved_case_ids" ] = list ( set ( case_update_from_block ( cb ) . id for cb in case_blocks ) ) except CaseGenerationException : doc_ret [ "__retrieved_case_ids" ] = [ ] if 'backend_id' not in doc_ret : doc_ret [ 'backend_id' ] = 'couch' return doc_ret