def test_scrub(self): with Transaction() as t: sar = SurveyAnswersRepo(t) sar.scrub(HUMAN_SOURCE.account_id, HUMAN_SOURCE.id, SURVEY_ID) obs = sar.get_answered_survey(HUMAN_SOURCE.account_id, HUMAN_SOURCE.id, SURVEY_ID, 'en_US') self.assertEqual(obs['3'], SURVEY_ANSWERS['3']) self.assertNotEqual(obs['116'], SURVEY_ANSWERS['116'])
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 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