def task(self): from celery.task.base import Task return Task.AsyncResult(self.task_id)
def get_task(task_id): from celery.task.base import Task return Task.AsyncResult(task_id)
def resubmit_for_users(request, domain): """ url-base: "https://india.commcarehq.org/a/care-bihar" """ from celery.task.base import Task if request.method == 'POST': data = json.loads(request.raw_post_data) async = data.get('async', False) debug = data.get('debug', False) check = data.get('check', False) url_base = data['url_base'] app_id = data['app_id'] remote_mapping = data['remote_mapping'] # having this exposed was totally insecure... # def get_remote_id_mapping(): # url = url_base + '/settings/api/id_mapping/' # response = restkit.request(url) # if response.status[0] == '2': # body = response.body_string() # return json.loads(body) # else: # raise Exception("Failure at %s" % url) # # remote_mapping = get_remote_id_mapping() def get_id(type, origin, name): """ i.e. get_id('users', 'remote', 'barbara') """ if origin == 'remote': return remote_mapping[type][name] if (type, origin) == ('users', 'local'): username = normalize_username(name, domain) user = CommCareUser.get_by_username(username) if user: return user.user_id else: raise Exception('Unknown local user "%s"' % username) if (type, origin) == ('groups', 'local'): return Group.by_name(domain, name).get_id id_mapping = {} for type in 'users', 'groups': id_mapping[type] = dict([ (get_id(type, 'local', local_name), get_id(type, 'remote', remote_name)) for (local_name, remote_name) in data[type].items() ]) args = url, user_id_mapping, group_id_mapping, domain = url_base + '/receiver/' + app_id + '/', id_mapping[ 'users'], id_mapping['groups'], domain if debug: forms = [] for form in tasks.forms_for_cases_for_users( id_mapping['users'].keys(), domain): new_xml = prepare_for_resubmission(form.get_xml(), copy(user_id_mapping), copy(group_id_mapping), salt=url) # if form.get_id in new_xml: forms.append({ 'id': form.get_id, 'xml': form.get_xml(), 'new_xml': new_xml, 'submit_time': form.received_on, 'new_id': None }) return json_response({ 'forms': forms, 'user_id_mapping': user_id_mapping, 'group_id_mapping': group_id_mapping, }) elif check: forms = [] for form in tasks.forms_for_cases_for_users( id_mapping['users'].keys(), domain): new_id = submission_xml.IdMap(url)(form.get_id) went_through = get_db().doc_exist(new_id) if not went_through: new_xml = prepare_for_resubmission(form.get_xml(), copy(user_id_mapping), copy(group_id_mapping), salt=url) forms.append({ 'id': form.get_id, 'xml': form.get_xml(), 'new_id': new_id, 'new_xml': new_xml }) return json_response({ 'forms': forms, 'user_id_mapping': user_id_mapping, 'group_id_mapping': group_id_mapping, }) else: if async: subtask = tasks.resubmit_for_users.delay(*args) return json_response({'task_id': subtask.task_id}) else: tasks.resubmit_for_users(*args) return json_response(id_mapping) else: if request.GET.get('debug'): return render(request, 'migration/resubmit_for_users_debug.html', {'domain': domain}) else: task_id = request.GET.get('task_id') if task_id: result = Task.AsyncResult(task_id) return json_response({ 'task_id': task_id, 'state': result.state, 'info': result.info }) else: return render(request, 'migration/resubmit_for_users.html', {'domain': domain})