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/')
Exemple #2
0
    def post(self):
        skid = self.current_user
        tl = text_locale['handlers']
        ag_login_id = ag_data.get_user_for_kit(skid)
        ag_login_info = ag_data.get_login_info(ag_login_id)[0]
        animal_survey_id = self.get_argument('survey_id', None)
        sitebase = media_locale['SITEBASE']

        if not animal_survey_id:
            animal_survey_id = binascii.hexlify(os.urandom(8))
            new_survey = True
        else:
            new_survey = False

        form = self.animal_survey()
        form.process(data=self.request.arguments)
        data = {'questions': form.data}
        participant_name = form['Pet_Information_127_0'].data[0]
        # If the participant already exists, stop them outright
        if new_survey and \
                ag_data.check_if_consent_exists(ag_login_id, participant_name):
            errmsg = url_escape(tl['PARTICIPANT_EXISTS'] % participant_name)
            url = sitebase + "/authed/portal/?errmsg=%s" % errmsg
            self.redirect(url)
            return

        consent = {
            'login_id': ag_login_id,
            'participant_name': participant_name,
            'participant_email': ag_login_info['email'],
            'assent_obtainer': 'ANIMAL_SURVEY',
            'parent_1_name': 'ANIMAL_SURVEY',
            'parent_2_name': 'ANIMAL_SURVEY',
            'survey_id': animal_survey_id,
            'is_juvenile': True,
            'deceased_parent': False,
            'obtainer_name': 'ANIMAL_SURVEY',
            'age_range': 'ANIMAL_SURVEY'
        }
        redis.hset(animal_survey_id, 'consent', dumps(consent))
        redis.hset(animal_survey_id, 0, dumps(data))
        redis.expire(animal_survey_id, 86400)

        store_survey(primary_animal_survey, animal_survey_id)
        if not new_survey:
            message = urlencode([('errmsg', tl['SUCCESSFULLY_EDITED'] %
                                 participant_name)])
        else:
            message = urlencode([('errmsg', tl['SUCCESSFULLY_ADDED'] %
                                 participant_name)])

        url = sitebase + '/authed/portal/?%s' % message
        self.redirect(url)
    def post(self):
        tl = text_locale['handlers']
        participant_name = self.get_argument("participant_name").strip()
        participant_email = self.get_argument("participant_email").strip()
        age_range = self.get_argument("age_range")
        parent_1_name = self.get_argument("parent_1_name", None)
        parent_2_name = self.get_argument("parent_2_name", None)
        obtainer_name = self.get_argument("obtainer_name", None)
        deceased_parent = self.get_argument("deceased_parent", 'No')
        sitebase = media_locale['SITEBASE']

        if not participant_name or not participant_email:
            self.render("new_participant.html",
                        skid=self.current_user,
                        message=tl['MISSING_NAME_EMAIL'])
            return

        ag_login_id = ag_data.get_user_for_kit(self.current_user)

        # If the participant already exists, stop them outright
        if ag_data.check_if_consent_exists(ag_login_id, participant_name):
            errmsg = url_escape(tl['PARTICIPANT_EXISTS'] % participant_name)
            url = sitebase + "/authed/portal/?errmsg=%s" % errmsg
            self.redirect(url)
            return

        human_survey_id = binascii.hexlify(os.urandom(8))

        consent = {
            'participant_name': participant_name,
            'participant_email': participant_email,
            'parent_1_name': parent_1_name,
            'parent_2_name': parent_2_name,
            'is_juvenile': True if age_range != '18-plus' else False,
            'deceased_parent': deceased_parent,
            'obtainer_name': obtainer_name,
            'age_range': age_range,
            'login_id': ag_login_id,
            'survey_id': human_survey_id
        }

        redis.hset(human_survey_id, 'consent', dumps(consent))
        redis.expire(human_survey_id, 86400)

        self.set_secure_cookie('human_survey_id', human_survey_id)
        self.redirect(sitebase + "/authed/survey_main/")
    def post(self):
        tl = text_locale['handlers']
        participant_name = self.get_argument("participant_name").strip()
        participant_email = self.get_argument("participant_email").strip()
        age_range = self.get_argument("age_range")
        parent_1_name = self.get_argument("parent_1_name", None)
        parent_2_name = self.get_argument("parent_2_name", None)
        obtainer_name = self.get_argument("obtainer_name", None)
        deceased_parent = self.get_argument("deceased_parent", 'No')
        sitebase = media_locale['SITEBASE']

        if not participant_name or not participant_email:
            self.render("new_participant.html", skid=self.current_user,
                        message=tl['MISSING_NAME_EMAIL'])
            return

        ag_login_id = ag_data.get_user_for_kit(self.current_user)

        # If the participant already exists, stop them outright
        if ag_data.check_if_consent_exists(ag_login_id, participant_name):
            errmsg = url_escape(tl['PARTICIPANT_EXISTS'] % participant_name)
            url = sitebase + "/authed/portal/?errmsg=%s" % errmsg
            self.redirect(url)
            return

        human_survey_id = binascii.hexlify(os.urandom(8))

        consent = {'participant_name': participant_name,
                   'participant_email': participant_email,
                   'parent_1_name': parent_1_name,
                   'parent_2_name': parent_2_name,
                   'is_juvenile': True if age_range != '18-plus' else False,
                   'deceased_parent': deceased_parent,
                   'obtainer_name': obtainer_name,
                   'age_range': age_range,
                   'login_id': ag_login_id,
                   'survey_id': human_survey_id}

        redis.hset(human_survey_id, 'consent', dumps(consent))
        redis.expire(human_survey_id, 86400)

        self.set_secure_cookie('human_survey_id', human_survey_id)
        self.redirect(sitebase + "/authed/survey_main/")
    def post(self):
        skid = self.current_user
        tl = text_locale['handlers']
        ag_login_id = ag_data.get_user_for_kit(skid)
        survey_id = self.get_argument('survey_id', None)
        survey_type = self.get_argument('type')
        participant_name = url_unescape(self.get_argument('participant_name'))
        sitebase = media_locale['SITEBASE']

        if not survey_id:
            survey_id = binascii.hexlify(os.urandom(8))

        sec_survey = self.sec_surveys[survey_type]
        survey_class = make_survey_class(sec_survey.groups[0],
                                         survey_type='SecondarySurvey')

        form = survey_class()
        form.process(data=self.request.arguments)
        data = {'questions': form.data}

        consent = {
            'login_id': ag_login_id,
            'participant_name': participant_name,
            'survey_id': survey_id,
            'secondary': True
        }
        redis.hset(survey_id, 'consent', dumps(consent))
        redis.hset(survey_id, 0, dumps(data))
        redis.expire(survey_id, 86400)

        store_survey(sec_survey, survey_id)
        if survey_id:
            message = urlencode([
                ('errmsg', tl['SUCCESSFULLY_EDITED'] % participant_name)
            ])
        else:
            message = urlencode([
                ('errmsg', tl['SUCCESSFULLY_ADDED'] % participant_name)
            ])

        url = '%s/authed/portal/?%s' % (sitebase, message)
        self.redirect(url)
    def post(self):
        skid = self.current_user
        tl = text_locale['handlers']
        ag_login_id = ag_data.get_user_for_kit(skid)
        survey_id = self.get_argument('survey_id', None)
        survey_type = self.get_argument('type')
        participant_name = self.get_argument('participant_name')
        sitebase = media_locale['SITEBASE']

        if not survey_id:
            survey_id = binascii.hexlify(os.urandom(8))

        sec_survey = self.sec_surveys[survey_type]
        survey_class = make_survey_class(sec_survey.groups[0],
                                         survey_type='SecondarySurvey')

        form = survey_class()
        form.process(data=self.request.arguments)
        data = {'questions': form.data}

        consent = {
            'login_id': ag_login_id,
            'participant_name': participant_name,
            'survey_id': survey_id,
            'secondary': True
        }
        redis.hset(survey_id, 'consent', dumps(consent))
        redis.hset(survey_id, 0, dumps(data))
        redis.expire(survey_id, 86400)

        store_survey(sec_survey, survey_id)
        if survey_id:
            message = urlencode([('errmsg', tl['SUCCESSFULLY_EDITED'] %
                                 participant_name)])
        else:
            message = urlencode([('errmsg', tl['SUCCESSFULLY_ADDED'] %
                                 participant_name)])

        url = '%s/authed/portal/?%s' % (sitebase, message)
        self.redirect(url)
    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/')
Exemple #8
0
    def test_edit_survey(self):
        # creating a new participant for existing ag_login_id
        main_survey_id = '38792874'
        sec_survey_id_fermented = '3879287455'
        sec_survey_id_surfer = '3879287456'
        ag_login_id = 'd8592c74-9694-2135-e040-8a80115d6401'
        participant_name = 'test_dude_stefan'
        email = '*****@*****.**'

        with_fk_inserts =\
            [(main_survey_id, _id, 'Unspecified') for _id in
                [1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13, 14, 15, 16, 17, 18, 19,
                 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34,
                 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
                 50, 51, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65,
                 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80,
                 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
                 96, 107, 109, 110, 111, 112, 114, 146, 148, 149, 153, 154,
                 155, 156, 157, 158, 159, 160, 162, 163, 164]]
        without_fk_inserts =\
            [(main_survey_id, _id, '[""]') for _id in
                [98, 99, 150, 103, 104, 105, 106, 108, 113, 115, 116, 117, 118,
                 119, 120, 122, 124, 126, 101]]
        self.mock_login(ag_data.ut_get_supplied_kit_id(ag_login_id))

        s = Survey(main_survey_id)
        s.store_survey(
            {u'login_id': ag_login_id,
             u'age_range': u'18-plus',
             u'parent_1_name': None,
             u'participant_email': email,
             u'obtainer_name': None,
             u'parent_2_name': None,
             u'deceased_parent': u'No',
             u'participant_name': participant_name,
             u'survey_id': main_survey_id,
             u'is_juvenile': False},
            with_fk_inserts, without_fk_inserts)

        # confirm that no secondary surveys are present
        response = self.post('/participants/%s' % participant_name,
                             {'participant_type': 'human'})
        self.assertEqual(response.code, 200)
        self.assertNotIn('fermented', response.body)
        self.assertNotIn('surf', response.body)

        # add a new fermented food survey
        data = {'questions': {'Fermented_Foods_170_0': [''],
                              'Fermented_Foods_173_0': [''],
                              'Fermented_Foods_169_0': None,
                              'Fermented_Foods_171_0': ['13'],
                              'Fermented_Foods_168_0': [''],
                              'Fermented_Foods_167_0': ['1'],
                              'Fermented_Foods_166_0': ['0'],
                              'Fermented_Foods_165_0': ['0'],
                              'Fermented_Foods_172_0': ['']}}
        consent = {
            'login_id': ag_login_id,
            'participant_name': participant_name,
            'survey_id': sec_survey_id_fermented,
            'secondary': True
        }
        redis.hset(sec_survey_id_fermented, 'consent', dumps(consent))
        redis.hset(sec_survey_id_fermented, 0, dumps(data))
        redis.expire(sec_survey_id_fermented, 86400)
        store_survey(fermented_survey, sec_survey_id_fermented)

        # confirm that now a fermented food survey is present
        response = self.post('/participants/%s' % participant_name,
                             {'participant_type': 'human'})
        self.assertEqual(response.code, 200)
        self.assertIn('fermented', response.body)
        self.assertNotIn('surf', response.body)
        self.assertIn('secondary_survey/?type=%s&participant_name=%s&survey=%s'
                      % ('fermented',
                         participant_name,
                         sec_survey_id_fermented), response.body)
        # check human_survey_completed links:
        # TODO: I don't know how to set a secured cookie for
        # completed_survey_id and could need some help to actually test values
        # for the rendered page /authed/human_survey_completed/
        # response = self.get('/authed/human_survey_completed/')

        # add a new surfers survey
        data = {'questions': {'Surfers_182_0': ['0'],
                              'Surfers_176_0': ['0'],
                              'Surfers_175_0': ['0'],
                              'Surfers_185_0': ['0'],
                              'Surfers_179_0': ['0'],
                              'Surfers_183_0': ['0'],
                              'Surfers_180_0': ['0'],
                              'Surfers_174_0': ['0'],
                              'Surfers_178_0': ['0'],
                              'Surfers_181_0': ['0'],
                              'Surfers_177_0': ['0'],
                              'Surfers_184_0': ['0']}}
        consent = {
            'login_id': ag_login_id,
            'participant_name': participant_name,
            'survey_id': sec_survey_id_surfer,
            'secondary': True
        }
        redis.hset(sec_survey_id_surfer, 'consent', dumps(consent))
        redis.hset(sec_survey_id_surfer, 0, dumps(data))
        redis.expire(sec_survey_id_surfer, 86400)
        store_survey(surf_survey, sec_survey_id_surfer)

        # confirm that now a surfers and fermented food survey is present
        response = self.post('/participants/%s' % participant_name,
                             {'participant_type': 'human'})
        self.assertEqual(response.code, 200)
        self.assertIn('fermented', response.body)
        self.assertIn('surf', response.body)
        self.assertIn('secondary_survey/?type=%s&participant_name=%s&survey=%s'
                      % ('fermented',
                         participant_name,
                         sec_survey_id_fermented),
                      response.body)
        self.assertIn('secondary_survey/?type=%s&participant_name=%s&survey=%s'
                      % ('surf',
                         participant_name,
                         sec_survey_id_surfer),
                      response.body)
    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 post(self):
        tl = text_locale['handlers']
        deceased_parent = self.get_argument("deceased_parent", None)
        participant_name = self.get_argument("participant_name")
        participant_email = self.get_argument("participant_email")
        is_juvenile = self.get_argument("is_juvenile", 'off')
        parent_1_name = self.get_argument("parent_1_name", None)
        parent_2_name = self.get_argument("parent_2_name", None)

        ag_login_id = ag_data.get_user_for_kit(self.current_user)
        kit_email = ag_data.get_user_info(self.current_user)['email']

        # Check if the participant is on the exceptions list
        is_exception = (
            participant_name
            in ag_data.getParticipantExceptions(ag_login_id))

        # If the participant already exists, stop them outright
        if ag_data.check_if_consent_exists(ag_login_id, participant_name):
            errmsg = url_escape(tl['PARTICIPANT_EXISTS'] % participant_name)
            self.redirect(media_locale['SITEBASE'] + "/authed/portal/?errmsg=%s" % errmsg)
            return

        if is_juvenile == 'off' and is_exception:
            errmsg = url_escape(tl["JUVENILE_CONSENT_EXPECTED"] %
                                participant_name)
            self.redirect(media_locale['SITEBASE'] + "/authed/portal/?errmsg=%s" % errmsg)
            return

        if is_juvenile == 'on':
            # If they aren't already an exception, we need to verify them
            if not is_exception:
                alert_message = tl['MINOR_PARENTAL_BODY']

                subject = ("AGJUVENILE: %s (ag_login_id: %s) is a child"
                           % (participant_name, ag_login_id))

                message = MESSAGE_TEMPLATE % (participant_name,
                                              parent_1_name, parent_2_name,
                                              deceased_parent,
                                              self.current_user, kit_email)

                try:
                    send_email(message, subject, sender=kit_email)
                    alert_message = tl['MESSAGE_SENT']
                except:
                    alert_message = media_locale['EMAIL_ERROR']

                self.redirect(media_locale['SITEBASE'] + "/authed/portal/?errmsg=%s" % alert_message)
                return

        human_survey_id = binascii.hexlify(os.urandom(8))

        consent= {'participant_name': participant_name,
                  'participant_email': participant_email,
                  'parent_1_name': parent_1_name,
                  'parent_2_name': parent_2_name,
                  'is_juvenile': True if is_juvenile == 'on' else False,
                  'deceased_parent': deceased_parent,
                  'login_id': ag_login_id,
                  'survey_id': human_survey_id}

        redis.hset(human_survey_id, 'consent', dumps(consent))
        redis.expire(human_survey_id, 86400)

        self.set_secure_cookie('human_survey_id', human_survey_id)
        self.redirect(media_locale['SITEBASE'] + "/authed/survey_main/")