def delete_account(account_id, token_info): validate_admin_access(token_info) with Transaction() as t: acct_repo = AccountRepo(t) src_repo = SourceRepo(t) samp_repo = SampleRepo(t) sar_repo = SurveyAnswersRepo(t) acct = acct_repo.get_account(account_id) if acct is None: return jsonify(message="Account not found", code=404), 404 else: # the account is already scrubbed so let's stop early if acct.account_type == 'deleted': return None, 204 sample_count = 0 sources = src_repo.get_sources_in_account(account_id) for source in sources: samples = samp_repo.get_samples_by_source(account_id, source.id) has_samples = len(samples) > 0 sample_count += len(samples) for sample in samples: # we scrub rather than disassociate in the event that the # sample is in our freezers but not with an up-to-date scan samp_repo.scrub(account_id, source.id, sample.id) surveys = sar_repo.list_answered_surveys(account_id, source.id) if has_samples: # if we have samples, we need to scrub survey / source # free text for survey_id in surveys: sar_repo.scrub(account_id, source.id, survey_id) src_repo.scrub(account_id, source.id) else: # if we do not have associated samples, then the source # is safe to delete for survey_id in surveys: sar_repo.delete_answered_survey(account_id, survey_id) src_repo.delete_source(account_id, source.id) # an account is safe to delete if there are no associated samples if sample_count > 0: acct_repo.scrub(account_id) else: acct_repo.delete_account(account_id) t.commit() return None, 204
def read_answered_surveys(account_id, source_id, language_tag, token_info): _validate_account_access(token_info, account_id) with Transaction() as t: survey_answers_repo = SurveyAnswersRepo(t) survey_template_repo = SurveyTemplateRepo(t) answered_surveys = survey_answers_repo.list_answered_surveys( account_id, source_id) api_objs = [] for ans in answered_surveys: template_id = survey_answers_repo.find_survey_template_id(ans) if template_id is None: continue o = survey_template_repo.get_survey_template_link_info(template_id) api_objs.append(o.to_api(ans)) return jsonify(api_objs), 200
def delete_source(account_id, source_id, token_info): _validate_account_access(token_info, account_id) with Transaction() as t: source_repo = SourceRepo(t) survey_answers_repo = SurveyAnswersRepo(t) answers = survey_answers_repo.list_answered_surveys( account_id, source_id) for survey_id in answers: survey_answers_repo.delete_answered_survey(account_id, survey_id) if not source_repo.delete_source(account_id, source_id): return jsonify(code=404, message=SRC_NOT_FOUND_MSG), 404 # TODO: 422? t.commit() return '', 204
def delete_dummy_answered_surveys_from_source_with_t( t, dummy_acct_id, dummy_source_id, sample_ids, survey_answers_repo=None): if survey_answers_repo is None: survey_answers_repo = SurveyAnswersRepo(t) answers = survey_answers_repo.list_answered_surveys( dummy_acct_id, dummy_source_id) for survey_id in answers: for curr_sample_id in sample_ids: # dissociate this survey from any used sample try: survey_answers_repo.dissociate_answered_survey_from_sample( dummy_acct_id, dummy_source_id, curr_sample_id, survey_id) except werkzeug.exceptions.NotFound: pass # Now delete the answered survey survey_answers_repo.delete_answered_survey( dummy_acct_id, survey_id)
def delete_dummy_accts(): with Transaction() as t: source_repo = SourceRepo(t) survey_answers_repo = SurveyAnswersRepo(t) sources = source_repo.get_sources_in_account(ACCT_ID_1) for curr_source in sources: answers = survey_answers_repo.list_answered_surveys( ACCT_ID_1, curr_source.id) for survey_id in answers: survey_answers_repo.delete_answered_survey( ACCT_ID_1, survey_id) source_repo.delete_source(ACCT_ID_1, curr_source.id) acct_repo = AccountRepo(t) acct_repo.delete_account(ACCT_ID_1) acct_repo.delete_account(ACCT_ID_2) # Belt and suspenders: these test emails are used by some tests outside # of this module as well, so can't be sure they are paired with the # above dummy account ids acct_repo.delete_account_by_email(TEST_EMAIL) acct_repo.delete_account_by_email(TEST_EMAIL_2) t.commit()