(domain, couch_user, app_id=None, since=None, version='1.0',
                         state=None, items=False, force_cache=False,
                         cache_timeout=None, overwrite_cache=False,
                         as_user=None, device_id=None, user_id=None,
                         openrosa_version=None,
                         skip_fixtures=False, auth_type=None)
return restore_config.get_response(), restore_config.timing_context
:param domain: Domain being restored from
    :param couch_user: User performing restore
    :param app_id: App ID of the app making the request
    :param since: ID of current sync log used to generate incremental sync
    :param version: Version of the sync response required
    :param state: Hash value of the current database of cases on the device for consistency checking
    :param items: Include item count if True
    :param force_cache: Force response to be cached
    :param cache_timeout: Override the default cache timeout of 1 hour.
    :param overwrite_cache: Ignore cached response if True
    :param as_user: Username of user to generate restore for (if different from current user)
    :param device_id: ID of device performing restore
    :param user_id: ID of user performing restore (used in case of deleted user with same username)
    :param openrosa_version:
    :param skip_fixtures: Do not include fixtures in sync payload
    :param auth_type: The type of auth that was used to authenticate the request.
        Used to determine if the request is coming from an actual user or as part of some automation.
    :return: Tuple of (http response, timing context or None)
:param domain: Domain being restored from
    :param couch_user: User performing restore
    :param app_id: App ID of the app making the request
    :param since: ID of current sync log used to generate incremental sync
    :param version: Version of the sync response required
    :param state: Hash value of the current database of cases on the device for consistency checking
    :param items: Include item count if True
    :param force_cache: Force response to be cached
    :param cache_timeout: Override the default cache timeout of 1 hour.
    :param overwrite_cache: Ignore cached response if True
    :param as_user: Username of user to generate restore for (if different from current user)
    :param device_id: ID of device performing restore
    :param user_id: ID of user performing restore (used in case of deleted user with same username)
    :param openrosa_version:
    :param skip_fixtures: Do not include fixtures in sync payload
    :param auth_type: The type of auth that was used to authenticate the request.
        Used to determine if the request is coming from an actual user or as part of some automation.
    :return: Tuple of (http response, timing context or None)
: param domain : Domain being restored from : param couch_user : User performing restore : param app_id : App ID of the app making the request : param since : ID of current sync log used to generate incremental sync : param version : Version of the sync response required : param state : Hash value of the current database of cases on the device for consistency checking : param items : Include item count if True : param force_cache : Force response to be cached : param cache_timeout : Override the default cache timeout of 1 hour . : param overwrite_cache : Ignore cached response if True : param as_user : Username of user to generate restore for ( if different from current user ) : param device_id : ID of device performing restore : param user_id : ID of user performing restore ( used in case of deleted user with same username ) : param openrosa_version : : param skip_fixtures : Do not include fixtures in sync payload : param auth_type : The type of auth that was used to authenticate the request . Used to determine if the request is coming from an actual user or as part of some automation . : return : Tuple of ( http response timing context or None )
def get_restore_response(domain, couch_user, app_id=None, since=None, version='1.0',
                         state=None, items=False, force_cache=False,
                         cache_timeout=None, overwrite_cache=False,
                         as_user=None, device_id=None, user_id=None,
                         openrosa_version=None,
                         skip_fixtures=False, auth_type=None):
    """
    :param domain: Domain being restored from
    :param couch_user: User performing restore
    :param app_id: App ID of the app making the request
    :param since: ID of current sync log used to generate incremental sync
    :param version: Version of the sync response required
    :param state: Hash value of the current database of cases on the device for consistency checking
    :param items: Include item count if True
    :param force_cache: Force response to be cached
    :param cache_timeout: Override the default cache timeout of 1 hour.
    :param overwrite_cache: Ignore cached response if True
    :param as_user: Username of user to generate restore for (if different from current user)
    :param device_id: ID of device performing restore
    :param user_id: ID of user performing restore (used in case of deleted user with same username)
    :param openrosa_version:
    :param skip_fixtures: Do not include fixtures in sync payload
    :param auth_type: The type of auth that was used to authenticate the request.
        Used to determine if the request is coming from an actual user or as part of some automation.
    :return: Tuple of (http response, timing context or None)
    """

    if user_id and user_id != couch_user.user_id:
        # sync with a user that has been deleted but a new
        # user was created with the same username and password
        from couchforms.openrosa_response import get_simple_response_xml
        from couchforms.openrosa_response import ResponseNature
        response = get_simple_response_xml(
            'Attempt to sync with invalid user.',
            ResponseNature.OTA_RESTORE_ERROR
        )
        return HttpResponse(response, content_type="text/xml; charset=utf-8", status=412), None

    is_demo_restore = couch_user.is_commcare_user() and couch_user.is_demo_user
    if is_demo_restore:
        # if user is in demo-mode, return demo restore
        return demo_user_restore_response(couch_user), None

    uses_login_as = bool(as_user)
    as_user_obj = CouchUser.get_by_username(as_user) if uses_login_as else None
    if uses_login_as and not as_user_obj:
        msg = _('Invalid restore as user {}').format(as_user)
        return HttpResponse(msg, status=401), None
    is_permitted, message = is_permitted_to_restore(
        domain,
        couch_user,
        as_user_obj,
    )
    if not is_permitted:
        return HttpResponse(message, status=401), None

    restore_user = get_restore_user(domain, couch_user, as_user_obj)
    if not restore_user:
        return HttpResponse('Could not find user', status=404), None

    project = Domain.get_by_name(domain)
    async_restore_enabled = (
        toggles.ASYNC_RESTORE.enabled(domain)
        and openrosa_version
        and LooseVersion(openrosa_version) >= LooseVersion(OPENROSA_VERSION_MAP['ASYNC_RESTORE'])
    )

    # Ensure fixtures are included if sync is full rather than incremental
    if not since:
        skip_fixtures = False

    app = get_app_cached(domain, app_id) if app_id else None
    restore_config = RestoreConfig(
        project=project,
        restore_user=restore_user,
        params=RestoreParams(
            sync_log_id=since,
            version=version,
            state_hash=state,
            include_item_count=items,
            app=app,
            device_id=device_id,
            openrosa_version=openrosa_version,
        ),
        cache_settings=RestoreCacheSettings(
            force_cache=force_cache or async_restore_enabled,
            cache_timeout=cache_timeout,
            overwrite_cache=overwrite_cache
        ),
        is_async=async_restore_enabled,
        skip_fixtures=skip_fixtures,
        auth_type=auth_type
    )
    return restore_config.get_response(), restore_config.timing_context
def get_restore_response ( domain , couch_user , app_id = None , since = None , version = '1.0' , state = None , items = False , force_cache = False , cache_timeout = None , overwrite_cache = False , as_user = None , device_id = None , user_id = None , openrosa_version = None , skip_fixtures = False , auth_type = None ) : if user_id and user_id != couch_user . user_id : # sync with a user that has been deleted but a new # user was created with the same username and password from couchforms . openrosa_response import get_simple_response_xml from couchforms . openrosa_response import ResponseNature response = get_simple_response_xml ( 'Attempt to sync with invalid user.' , ResponseNature . OTA_RESTORE_ERROR ) return HttpResponse ( response , content_type = "text/xml; charset=utf-8" , status = 412 ) , None is_demo_restore = couch_user . is_commcare_user ( ) and couch_user . is_demo_user if is_demo_restore : # if user is in demo-mode, return demo restore return demo_user_restore_response ( couch_user ) , None uses_login_as = bool ( as_user ) as_user_obj = CouchUser . get_by_username ( as_user ) if uses_login_as else None if uses_login_as and not as_user_obj : msg = _ ( 'Invalid restore as user {}' ) . format ( as_user ) return HttpResponse ( msg , status = 401 ) , None is_permitted , message = is_permitted_to_restore ( domain , couch_user , as_user_obj , ) if not is_permitted : return HttpResponse ( message , status = 401 ) , None restore_user = get_restore_user ( domain , couch_user , as_user_obj ) if not restore_user : return HttpResponse ( 'Could not find user' , status = 404 ) , None project = Domain . get_by_name ( domain ) async_restore_enabled = ( toggles . ASYNC_RESTORE . enabled ( domain ) and openrosa_version and LooseVersion ( openrosa_version ) >= LooseVersion ( OPENROSA_VERSION_MAP [ 'ASYNC_RESTORE' ] ) ) # Ensure fixtures are included if sync is full rather than incremental if not since : skip_fixtures = False app = get_app_cached ( domain , app_id ) if app_id else None restore_config = RestoreConfig ( project = project , restore_user = restore_user , params = RestoreParams ( sync_log_id = since , version = version , state_hash = state , include_item_count = items , app = app , device_id = device_id , openrosa_version = openrosa_version , ) , cache_settings = RestoreCacheSettings ( force_cache = force_cache or async_restore_enabled , cache_timeout = cache_timeout , overwrite_cache = overwrite_cache ) , is_async = async_restore_enabled , skip_fixtures = skip_fixtures , auth_type = auth_type ) return restore_config . get_response ( ) , restore_config . timing_context