def read_answered_survey(account_id, source_id, survey_id, language_tag, token_info): _validate_account_access(token_info, account_id) with Transaction() as t: survey_answers_repo = SurveyAnswersRepo(t) survey_answers = survey_answers_repo.get_answered_survey( account_id, source_id, survey_id, language_tag) if not survey_answers: return jsonify(code=404, message="No survey answers found"), 404 template_id = survey_answers_repo.find_survey_template_id(survey_id) if template_id is None: return jsonify(code=422, message="No answers in survey"), 422 template_repo = SurveyTemplateRepo(t) link_info = template_repo.get_survey_template_link_info(template_id) link_info.survey_id = survey_id link_info.survey_text = survey_answers return jsonify(link_info), 200
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()
def test_dissociate_sample_from_source_success(self): dummy_acct_id, dummy_source_id = create_dummy_source( "Bo", Source.SOURCE_TYPE_HUMAN, DUMMY_HUMAN_SOURCE, create_dummy_1=True) create_dummy_kit(dummy_acct_id, dummy_source_id) dummy_answered_survey_id = create_dummy_answered_survey( dummy_acct_id, dummy_source_id, dummy_sample_id=MOCK_SAMPLE_ID) base_url = '/api/accounts/{0}/sources/{1}/samples'.format( dummy_acct_id, dummy_source_id) sample_url = "{0}/{1}".format(base_url, MOCK_SAMPLE_ID) delete_resp = self.client.delete( '%s?%s' % (sample_url, self.default_lang_querystring), headers=self.dummy_auth ) # check response code self.assertEqual(204, delete_resp.status_code) # load the samples associated to this source get_response = self.client.get( '%s?%s' % (base_url, self.default_lang_querystring), headers=self.dummy_auth) # check response code self.assertEqual(200, get_response.status_code) # ensure there are zero samples associated with this source get_resp_obj = json.loads(get_response.data) self.assertEqual(get_resp_obj, []) # load the sample info _, expected_sample = create_dummy_sample_objects(False) with Transaction() as t: # make sure the sample's collection info is wiped sample_repo = SampleRepo(t) obs_sample = sample_repo._get_sample_by_id(MOCK_SAMPLE_ID) self.assertEqual(expected_sample.__dict__, obs_sample.__dict__) # make sure answered survey no longer associated with any samples answered_survey_repo = SurveyAnswersRepo(t) answered_survey_ids = answered_survey_repo.\ _get_survey_sample_associations(dummy_answered_survey_id) self.assertEqual([], answered_survey_ids)
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 get_survey_metadata(self, sample_barcode, survey_template_id=None): ids = self._get_ids_relevant_to_barcode(sample_barcode) if ids is None: raise NotFound("No such barcode") account_id = ids.get('account_id') source_id = ids.get('source_id') sample_id = ids.get('sample_id') account = None source = None sample = None if sample_id is not None: sample_repo = SampleRepo(self._transaction) sample = sample_repo._get_sample_by_id(sample_id) if source_id is not None and account_id is not None: source_repo = SourceRepo(self._transaction) account_repo = AccountRepo(self._transaction) account = account_repo.get_account(account_id) source = source_repo.get_source(account_id, source_id) if source is None: raise RepoException("Barcode is not associated with a source") # TODO: This is my best understanding of how the data must be # transformed to get the host_subject_id, needs verification that it # generates the expected values for preexisting samples. prehash = account_id + source.name.lower() host_subject_id = sha512(prehash.encode()).hexdigest() survey_answers_repo = SurveyAnswersRepo(self._transaction) answer_ids = survey_answers_repo.list_answered_surveys_by_sample( account_id, source_id, sample_id) answer_to_template_map = {} for answer_id in answer_ids: template_id = survey_answers_repo.find_survey_template_id( answer_id) answer_to_template_map[answer_id] = template_id # if a survey template is specified, filter the returned surveys if survey_template_id is not None: # TODO: This schema is so awkward for this type of query... answers = [] for answer_id in answer_ids: if answer_to_template_map[answer_id] == survey_template_id: answers.append(answer_id) if len(answers) == 0: raise NotFound("This barcode is not associated with any " "surveys matching this template id") if len(answers) > 1: # I really hope this can't happen. (x . x) raise RepoException("This barcode is associated with more " "than one survey matching this template" " id") answer_ids = answers metadata_map = survey_answers_repo.build_metadata_map() all_survey_answers = [] for answer_id in answer_ids: answer_model = survey_answers_repo.get_answered_survey( account_id, source_id, answer_id, "en-US") survey_answers = {} for k in answer_model: new_k = metadata_map[int(k)] survey_answers[k] = [new_k, answer_model[k]] all_survey_answers.append({ "template": answer_to_template_map[answer_id], "response": survey_answers }) pulldown = { "sample_barcode": sample_barcode, "host_subject_id": host_subject_id, "account": account, "source": source, "sample": sample, "survey_answers": all_survey_answers } return pulldown