def post(self): # see if we're coming from an edit human_survey_id = self.get_argument('survey_id', None) page_number = int(self.get_argument('page_number')) if human_survey_id is None: # we came from consent human_survey_id = self.get_secure_cookie('human_survey_id') if human_survey_id is None: err_msg = url_escape("There was an unexpected error.") self.redirect(media_locale['SITEBASE'] + "/authed/portal/?errmsg=%s" % err_msg) return else: # we came from participant_overview consent = ag_data.getConsent(human_survey_id) self.set_secure_cookie('human_survey_id', human_survey_id) data = primary_human_survey.fetch_survey(human_survey_id) redis.hset(human_survey_id, 'consent', dumps(consent)) redis.hset(human_survey_id, 'existing', dumps(data)) redis.expire(human_survey_id, 86400) next_page_number = page_number + 1 if page_number >= 0: form_data = surveys[page_number]() form_data.process(data=self.request.arguments) data = {'questions': form_data.data} redis.hset(human_survey_id, page_number, dumps(data)) progress = int(100.0*(page_number+2)/(len(primary_human_survey.groups) + 1)) # if this is not the last page, render the next page if next_page_number < len(surveys): the_form = surveys[next_page_number]() existing_responses = redis.hget(human_survey_id, 'existing') if existing_responses: existing_responses = loads(existing_responses) the_form = surveys[next_page_number](data=existing_responses) title = primary_human_survey.groups[next_page_number].name self.render('human_survey.html', the_form=the_form, skid=self.current_user, TITLE=title, page_number=next_page_number, progress=progress) else: # only get the cookie if you complete the survey self.clear_cookie('human_survey_id') self.set_secure_cookie('completed_survey_id', human_survey_id) store_survey(primary_human_survey, human_survey_id) self.redirect(media_locale['SITEBASE'] + '/authed/human_survey_completed/')
def get(self): human_survey_id = self.get_secure_cookie('completed_survey_id') if human_survey_id is None: self.clear_cookie('completed_survey_id') self.redirect(media_locale['SITEBASE'] + '/authed/portal/') else: consent_info = ag_data.getConsent(human_survey_id) surveys = [f(human_survey_id, consent_info) for f in external_surveys] self.render('human_survey_completed.html', skid=self.current_user, surveys=surveys)
def get(self): human_survey_id = self.get_secure_cookie('completed_survey_id') if human_survey_id is None: self.clear_cookie('completed_survey_id') self.redirect(media_locale['SITEBASE'] + '/authed/portal/') else: consent_info = ag_data.getConsent(human_survey_id) surveys = [ f(human_survey_id, consent_info) for f in external_surveys ] self.render('human_survey_completed.html', skid=self.current_user, surveys=surveys)
def test_store_survey_edit(self): survey, survey_id, notes_test, consent = self.insert_data() obs = survey.fetch_survey(survey_id) exp = { 'Pet_Information_127_0': 'Fluffy', 'Pet_Information_137_0': [0], 'Pet_Information_143_0': 'Capybara', 'Pet_Information_132_0': 2, 'Pet_Information_138_0': 1, 'Pet_Information_144_0': notes_test, 'Pet_Information_129_0': 4, 'Pet_Information_142_0': 'Giant ratty pet!', 'Pet_Information_133_0': 3, 'Pet_Information_139_0': 5, 'Pet_Information_130_0': '20', 'Pet_Information_128_0': 9, 'Pet_Information_134_0': 2, 'Pet_Information_140_0': 0, 'Pet_Information_131_0': 1, 'Pet_Information_145_0': '29 - Male', 'Pet_Information_136_0': 3, 'Pet_Information_135_0': [3], 'Pet_Information_141_0': 4 } self.assertEqual(obs, exp) obs = ag_data.getConsent(survey_id) # change datatypes as postgres does: consent['deceased_parent'] = 'false' \ if consent['deceased_parent'] is False else 'true' consent['ag_login_id'] = consent['login_id'] del consent['login_id'] del consent['obtainer_name'] del consent['parent_1_name'] del consent['parent_2_name'] del consent['participant_name'] del obs['date_signed'] del obs['parent_1_name'] del obs['parent_2_name'] del obs['participant_name'] self.assertEqual(obs, consent) self.delete_survey(survey_id)
def test_store_survey_edit(self): survey, survey_id, notes_test, consent = self.insert_data() obs = survey.fetch_survey(survey_id) exp = {'Pet_Information_127_0': 'Fluffy', 'Pet_Information_137_0': [0], 'Pet_Information_143_0': 'Capybara', 'Pet_Information_132_0': 2, 'Pet_Information_138_0': 1, 'Pet_Information_144_0': notes_test, 'Pet_Information_129_0': 4, 'Pet_Information_142_0': 'Giant ratty pet!', 'Pet_Information_133_0': 3, 'Pet_Information_139_0': 5, 'Pet_Information_130_0': '20', 'Pet_Information_128_0': 9, 'Pet_Information_134_0': 2, 'Pet_Information_140_0': 0, 'Pet_Information_131_0': 1, 'Pet_Information_145_0': '29 - Male', 'Pet_Information_136_0': 3, 'Pet_Information_135_0': [3], 'Pet_Information_141_0': 4} self.assertEqual(obs, exp) obs = ag_data.getConsent(survey_id) # change datatypes as postgres does: consent['deceased_parent'] = 'false' \ if consent['deceased_parent'] is False else 'true' consent['ag_login_id'] = consent['login_id'] del consent['login_id'] del consent['obtainer_name'] del consent['parent_1_name'] del consent['parent_2_name'] del consent['participant_name'] del obs['date_signed'] del obs['parent_1_name'] del obs['parent_2_name'] del obs['participant_name'] self.assertEqual(obs, consent) self.delete_survey(survey_id)
def post(self): # see if we're coming from an edit human_survey_id = self.get_argument('survey_id', None) page_number = int(self.get_argument('page_number')) sitebase = media_locale['SITEBASE'] if human_survey_id is None: # we came from consent human_survey_id = self.get_secure_cookie('human_survey_id') if human_survey_id is None: err_msg = url_escape("There was an unexpected error.") self.redirect(sitebase + "/authed/portal/?errmsg=%s" % err_msg) return else: # we came from participant_overview consent = ag_data.getConsent(human_survey_id) # make sure is string so can be serialized consent['date_signed'] = str(consent['date_signed']) self.set_secure_cookie('human_survey_id', human_survey_id) data = primary_human_survey.fetch_survey(human_survey_id) redis.hset(human_survey_id, 'consent', dumps(consent)) redis.hset(human_survey_id, 'existing', dumps(data)) redis.expire(human_survey_id, 86400) next_page_number = page_number + 1 if page_number >= 0: form_data = surveys[page_number]() form_data.process(data=self.request.arguments) data = {'questions': form_data.data} redis.hset(human_survey_id, page_number, dumps(data)) progress = int(100.0 * (page_number + 2) / (len(phs_groups) + 1)) # if this is not the last page, render the next page if next_page_number < len(surveys): the_form = surveys[next_page_number]() existing_responses = redis.hget(human_survey_id, 'existing') if existing_responses: existing_responses = loads(existing_responses) the_form = surveys[next_page_number](data=existing_responses) title = phs_groups[next_page_number].name self.render('human_survey.html', the_form=the_form, skid=self.current_user, TITLE=title, page_number=next_page_number, progress=progress) else: # only get the cookie if you complete the survey self.clear_cookie('human_survey_id') self.set_secure_cookie('completed_survey_id', human_survey_id) store_survey(primary_human_survey, human_survey_id) existing = redis.hget(human_survey_id, 'existing') if existing is None: # Send consent info email since new participant consent_info = ag_data.getConsent(human_survey_id) try: message = build_consent_form(consent_info) send_email(message, 'American Gut-Signed Consent Form(s)', recipient=consent_info['participant_email'], sender='*****@*****.**', html=True) except: logging.exception('Error sending signed consent form for ' 'survey ID: %s to email: %s' % (human_survey_id, consent_info['participant_email'])) self.redirect(sitebase + '/authed/human_survey_completed/')
def test_store_survey(self): # Create new survey ID and make sure it is unused survey = Survey(2) c = ascii_letters + '1234567890' survey_id = ''.join([choice(c) for i in range(16)]) name = ''.join([choice(ascii_letters) for i in range(20)]) with self.assertRaises(ValueError): survey.fetch_survey(survey_id) consent = { 'login_id': 'd8592c74-84b1-2135-e040-8a80115d6401', 'survey_id': survey_id, 'participant_name': name, 'age_range': 'ANIMAL_SURVEY', 'parent_1_name': 'ANIMAL_SURVEY', 'parent_2_name': 'ANIMAL_SURVEY', 'deceased_parent': False, 'participant_email': 'REMOVED', 'obtainer_name': 'ANIMAL_SURVEY', 'assent_obtainer': 'ANIMAL_SURVEY', 'is_juvenile': True } with_fk = [(survey_id, 128, 'Other'), (survey_id, 129, 'Wild'), (survey_id, 131, 'Male'), (survey_id, 132, 'Suburban'), (survey_id, 133, 'Normal'), (survey_id, 134, 'Omnivore'), (survey_id, 135, 'Wild food'), (survey_id, 136, 'Both'), (survey_id, 137, 'Unspecified'), (survey_id, 138, 'Lives alone with humans'), (survey_id, 139, '8+'), (survey_id, 140, 'Unspecified'), (survey_id, 141, 'Never')] without_fk = [(survey_id, 130, '["20"]'), (survey_id, 142, unicode('["Mötley Crüe!"]', 'utf-8')), (survey_id, 143, '["Capybara"]'), (survey_id, 144, '[""]'), (survey_id, 145, '["29 - Male"]'), (survey_id, 127, '["%s"]' % name)] survey.store_survey(consent, with_fk, without_fk) obs = survey.fetch_survey(survey_id) exp = { 'Pet_Information_127_0': name, 'Pet_Information_137_0': [0], 'Pet_Information_143_0': 'Capybara', 'Pet_Information_132_0': 2, 'Pet_Information_138_0': 1, 'Pet_Information_144_0': '', 'Pet_Information_129_0': 4, 'Pet_Information_142_0': u'M\xf6tley Cr\xfce!'.encode('utf-8'), 'Pet_Information_133_0': 3, 'Pet_Information_139_0': 5, 'Pet_Information_130_0': '20', 'Pet_Information_128_0': 9, 'Pet_Information_134_0': 2, 'Pet_Information_140_0': 0, 'Pet_Information_131_0': 1, 'Pet_Information_145_0': '29 - Male', 'Pet_Information_136_0': 3, 'Pet_Information_135_0': [3], 'Pet_Information_141_0': 4 } self.assertEqual(obs, exp) obs = ag_data.getConsent(survey_id) del obs['date_signed'] consent['ag_login_id'] = consent['login_id'] del consent['login_id'] del consent['obtainer_name'] consent['deceased_parent'] = 'false' self.assertEqual(obs, consent) # revert database self.delete_survey(survey_id)
def test_store_survey_edit(self): # Create random string to test update happens c = ascii_letters + '1234567890' notes_test = ''.join([choice(c) for i in range(40)]) # Set up survey survey_id = '817ff95701f4dd10' survey = Survey(2) consent = { 'login_id': 'eba20873-b7db-33cc-e040-8a80115d392c', 'survey_id': survey_id, 'participant_name': 'some name that should be ignored', 'age_range': 'ANIMAL_SURVEY', 'parent_1_name': 'ANIMAL_SURVEY', 'parent_2_name': 'ANIMAL_SURVEY', 'deceased_parent': False, 'participant_email': 'REMOVED', 'obtainer_name': 'ANIMAL_SURVEY', 'assent_obtainer': 'ANIMAL_SURVEY', 'is_juvenile': True} with_fk = [(survey_id, 128, 'Other'), (survey_id, 129, 'Wild'), (survey_id, 131, 'Male'), (survey_id, 132, 'Suburban'), (survey_id, 133, 'Normal'), (survey_id, 134, 'Omnivore'), (survey_id, 135, 'Wild food'), (survey_id, 136, 'Both'), (survey_id, 137, 'Unspecified'), (survey_id, 138, 'Lives alone with humans'), (survey_id, 139, '8+'), (survey_id, 140, 'Unspecified'), (survey_id, 141, 'Never')] without_fk = [(survey_id, 130, '["20"]'), (survey_id, 142, '["Giant ratty pet!"]'), (survey_id, 143, '["Capybara"]'), (survey_id, 144, '["%s"]' % notes_test), (survey_id, 145, '["29 - Male"]'), (survey_id, 127, '["Fluffy"]')] survey.store_survey(consent, with_fk, without_fk) obs = survey.fetch_survey(survey_id) exp = {'Pet_Information_127_0': 'Fluffy', 'Pet_Information_137_0': [0], 'Pet_Information_143_0': 'Capybara', 'Pet_Information_132_0': 2, 'Pet_Information_138_0': 1, 'Pet_Information_144_0': notes_test, 'Pet_Information_129_0': 4, 'Pet_Information_142_0': 'Giant ratty pet!', 'Pet_Information_133_0': 3, 'Pet_Information_139_0': 5, 'Pet_Information_130_0': '20', 'Pet_Information_128_0': 9, 'Pet_Information_134_0': 2, 'Pet_Information_140_0': 0, 'Pet_Information_131_0': 1, 'Pet_Information_145_0': '29 - Male', 'Pet_Information_136_0': 3, 'Pet_Information_135_0': [3], 'Pet_Information_141_0': 4} self.assertEqual(obs, exp) obs = ag_data.getConsent(survey_id) consent['ag_login_id'] = consent['login_id'] del consent['login_id'] del consent['obtainer_name'] consent['deceased_parent'] = 'false' consent['date_signed'] = date(2015, 9, 27) consent['parent_1_name'] = 'REMOVED' consent['parent_2_name'] = 'REMOVED' consent['participant_name'] = 'REMOVED-0' consent['parent_1_name'] = 'REMOVED' self.assertEqual(obs, consent)
def test_store_survey(self): # Create new survey ID and make sure it is unused survey = Survey(2) c = ascii_letters + '1234567890' survey_id = ''.join([choice(c) for i in range(16)]) name = ''.join([choice(ascii_letters) for i in range(20)]) with self.assertRaises(ValueError): survey.fetch_survey(survey_id) consent = { 'login_id': 'd8592c74-84b1-2135-e040-8a80115d6401', 'survey_id': survey_id, 'participant_name': name, 'age_range': 'ANIMAL_SURVEY', 'parent_1_name': 'ANIMAL_SURVEY', 'parent_2_name': 'ANIMAL_SURVEY', 'deceased_parent': False, 'participant_email': 'REMOVED', 'obtainer_name': 'ANIMAL_SURVEY', 'assent_obtainer': 'ANIMAL_SURVEY', 'is_juvenile': True} with_fk = [(survey_id, 128, 'Other'), (survey_id, 129, 'Wild'), (survey_id, 131, 'Male'), (survey_id, 132, 'Suburban'), (survey_id, 133, 'Normal'), (survey_id, 134, 'Omnivore'), (survey_id, 135, 'Wild food'), (survey_id, 136, 'Both'), (survey_id, 137, 'Unspecified'), (survey_id, 138, 'Lives alone with humans'), (survey_id, 139, '8+'), (survey_id, 140, 'Unspecified'), (survey_id, 141, 'Never')] without_fk = [(survey_id, 130, '["20"]'), (survey_id, 142, unicode('["Mötley Crüe!"]', 'utf-8')), (survey_id, 143, '["Capybara"]'), (survey_id, 144, '[""]'), (survey_id, 145, '["29 - Male"]'), (survey_id, 127, '["%s"]' % name)] survey.store_survey(consent, with_fk, without_fk) obs = survey.fetch_survey(survey_id) exp = {'Pet_Information_127_0': name, 'Pet_Information_137_0': [0], 'Pet_Information_143_0': 'Capybara', 'Pet_Information_132_0': 2, 'Pet_Information_138_0': 1, 'Pet_Information_144_0': '', 'Pet_Information_129_0': 4, 'Pet_Information_142_0': u'M\xf6tley Cr\xfce!'.encode('utf-8'), 'Pet_Information_133_0': 3, 'Pet_Information_139_0': 5, 'Pet_Information_130_0': '20', 'Pet_Information_128_0': 9, 'Pet_Information_134_0': 2, 'Pet_Information_140_0': 0, 'Pet_Information_131_0': 1, 'Pet_Information_145_0': '29 - Male', 'Pet_Information_136_0': 3, 'Pet_Information_135_0': [3], 'Pet_Information_141_0': 4} self.assertEqual(obs, exp) obs = ag_data.getConsent(survey_id) del obs['date_signed'] consent['ag_login_id'] = consent['login_id'] del consent['login_id'] del consent['obtainer_name'] consent['deceased_parent'] = 'false' self.assertEqual(obs, consent)
def post(self): # see if we're coming from an edit human_survey_id = self.get_argument('survey_id', None) page_number = int(self.get_argument('page_number')) sitebase = media_locale['SITEBASE'] if human_survey_id is None: # we came from consent human_survey_id = self.get_secure_cookie('human_survey_id') if human_survey_id is None: err_msg = url_escape("There was an unexpected error.") self.redirect(sitebase + "/authed/portal/?errmsg=%s" % err_msg) return else: # we came from participant_overview consent = ag_data.getConsent(human_survey_id) # make sure is string so can be serialized consent['date_signed'] = str(consent['date_signed']) self.set_secure_cookie('human_survey_id', human_survey_id) data = primary_human_survey.fetch_survey(human_survey_id) redis.hset(human_survey_id, 'consent', dumps(consent)) redis.hset(human_survey_id, 'existing', dumps(data)) redis.expire(human_survey_id, 86400) next_page_number = page_number + 1 if page_number >= 0: form_data = surveys[page_number]() form_data.process(data=self.request.arguments) data = {'questions': form_data.data} redis.hset(human_survey_id, page_number, dumps(data)) progress = int(100.0 * (page_number + 2) / (len(phs_groups) + 1)) # if this is not the last page, render the next page if next_page_number < len(surveys): the_form = surveys[next_page_number]() existing_responses = redis.hget(human_survey_id, 'existing') if existing_responses: existing_responses = loads(existing_responses) the_form = surveys[next_page_number](data=existing_responses) title = phs_groups[next_page_number].name self.render('human_survey.html', the_form=the_form, skid=self.current_user, TITLE=title, page_number=next_page_number, progress=progress) else: # only get the cookie if you complete the survey self.clear_cookie('human_survey_id') self.set_secure_cookie('completed_survey_id', human_survey_id) store_survey(primary_human_survey, human_survey_id) existing = redis.hget(human_survey_id, 'existing') if existing is None: # Send consent info email since new participant consent_info = ag_data.getConsent(human_survey_id) try: message = build_consent_form(consent_info) send_email(message, 'American Gut-Signed Consent Form(s)', recipient=consent_info['participant_email'], sender='*****@*****.**', html=True) except: logging.exception( 'Error sending signed consent form for ' 'survey ID: %s to email: %s' % (human_survey_id, consent_info['participant_email'])) self.redirect(sitebase + '/authed/human_survey_completed/')