def migration_restore(request, domain, case_id): """Restore endpoint used in bulk case migrations Accepts the provided case_id and returns a restore for the user containing: * Registration block * The passed in case and its full network of cases """ domain_obj = Domain.get_by_name(domain) restore_user = request.couch_user restore_params = RestoreParams(device_id="case_migration", version=V2) restore_state = RestoreState(domain_obj, restore_user.to_ota_restore_user(domain), restore_params) restore_state.start_sync() timing_context = TimingContext('migration-restore-{}-{}'.format( domain, restore_user.username)) case_ids = get_related_case_ids(domain, case_id) with RestoreContent(restore_user.username) as content: content.append(get_registration_element(restore_user)) sync_payload = CleanOwnerSyncPayload(timing_context, case_ids, restore_state) sync_payload.extend_response(content) payload = content.get_fileobj() restore_state.finish_sync() return RestoreResponse(payload).get_http_response()
def test_items(self): user = u'user1' body = b'<elem>data0</elem>' expected = self._expected(user, body, items=2) with RestoreContent(user, True) as response: response.append(body) with response.get_fileobj() as fileobj: self.assertEqual(expected, fileobj.read())
def test_no_items(self): user = '******' body = '<elem>data0</elem>' expected = self._expected(user, body, items=None) with RestoreContent(user, False) as response: response.append(body.encode('utf-8')) with response.get_fileobj() as fileobj: self.assertEqual(expected, fileobj.read().decode('utf-8'))
def migration_restore(request, domain, case_id): """Restore endpoint used in bulk case migrations Accepts the provided case_id and returns a restore for the user containing: * Registration block * The passed in case and its full network of cases """ restore_user = request.couch_user with RestoreContent(restore_user.username) as content: content.append(get_registration_element(restore_user)) for case in get_case_and_descendants(domain, case_id): # Formplayer will be creating these cases for the first time, so # include create blocks content.append(get_case_element(case, ('create', 'update'), V2)) payload = content.get_fileobj() return RestoreResponse(payload).get_http_response()
from corehq.apps.reports.view_helpers import get_case_hierarchy return [ c for c in get_case_hierarchy(case, {})['case_list'] if not c.closed ] @formplayer_auth def migration_restore(request, domain, case_id): """Restore endpoint used in bulk case migrations Accepts the provided case_id and returns a restore for the user containing: * Registration block * The passed in case and its full network of cases """ try: case = CaseAccessors(domain).get_case(case_id) if case.domain != domain or case.is_deleted: raise Http404 except CaseNotFound: raise Http404 with RestoreContent('Case[{}]'.format(case_id)) as content: content.append(get_registration_element_for_case(case)) for case in get_case_hierarchy_for_restore(case): # Formplayer will be creating these cases for the first time, so # include create blocks content.append(get_case_element(case, ('create', 'update'), V2)) payload = content.get_fileobj() return RestoreResponse(payload).get_http_response()