Ejemplo n.º 1
0
def sendSurveys():
    email_sender_address = Settings.get('email_sender_address')
    survey_link_base_url = Settings.get('survey_link_base_url')
    # first get surveys with survey_send_time < now AND
    # last_sent == null
    now = datetime.now()
    logging.info(now)
    # Currently only send to those who are due to be reminded AND have NOT
    # completed it AND have never received an email about this nbl run 
    toSend = Survey.query(Survey.survey_send_time < now,
                          Survey.send_count == 0,
                          Survey.survey_complete_timestamp == None).fetch()
    logging.info('About to send %s emails' % len(toSend))
    sent_keys = []
    for survey in toSend:
        # create a link to the survey
        link = survey_link_base_url + survey.key.urlsafe()
        # mail it
        mail.send_mail(sender=email_sender_address,
                       to="%s <%s>" % (survey.first_name, survey.email),
                       subject="30-second NBL survey about your recent suit exposure",
                       body=SURVEY_EMAIL.format(survey.first_name, link))
        
        # mark it as sent with a timestamp
        survey.last_sent = datetime.now()
        # increment number times sent
        survey.send_count += 1
        # save it
        sent_keys.append(survey.put().urlsafe())
    logging.info('Sent the following keys: ')
    logging.info(sent_keys)
    return json.dumps(sent_keys)
Ejemplo n.º 2
0
def sendResults():
    email_sender_address = Settings.get('email_sender_address')
    survey_admin_email = Settings.get('survey_admin_email')

    toSend = Survey.query(Survey.results_reported == False,
                          Survey.survey_complete_timestamp != None).fetch()

    if len(toSend) == 0:
        return 'no surveys to send'

    survey_strs = []
    for survey in toSend:
        # convert dates to readable dates. Divide by 1000 b/c javascript
        # stamps in milliseconds and we want this back to python format
        nbl_str_time = to_central_time(
                survey.nbl_finish_timestamp).strftime('%m/%d/%Y %H:%M')
        complete_str_time = to_central_time(
                survey.survey_complete_timestamp).strftime('%m/%d/%Y %H:%M')
        survey_data = survey.jsonify(include_sensitive=True,
                                     exclude_unaffected=True)
        # Using jsonify gave us timestamps so we want to reset those to the 
        # human readable strings we just created
        survey_data['nblFinishTimestamp'] = nbl_str_time
        survey_data['surveyCompleteTimestamp'] = complete_str_time
        j = json.dumps(survey_data, indent=4)
        logging.info('adding new survey string to list \n\n' + j)
        survey_strs.append(j)

    logging.info('about to concat')
    body = '\n\n\n\n'.join(survey_strs)
    logging.info('the length of the body of email is %s' % str(len(body)))
    logging.info(body)

    mail.send_mail(sender=email_sender_address,
                   to=survey_admin_email,
                   subject="NBL survey data", body=body)
    
    reported_keys = []
    for survey in toSend:
        survey.results_reported = True
        reported_keys.append(survey.put().urlsafe())
    return json.dumps(reported_keys)
Ejemplo n.º 3
0
    def testCreateSurvey(self):
        time = 1500000000000
        delay = 24  # hours
        first_name = 'f'
        eis_id = '123'
        expectedSendTime = datetime.fromtimestamp(
            (time + delay * 60 * 60 * 1000) / 1000)
        data = {
            'eisId': eis_id,
            'firstName': first_name,
            'nblFinishTime': time,
            'surveySendDelay': delay
        }
        j = json.dumps(data)
        response = self.app.post('/api/survey/create', data=j)

        self.assertEqual(response.status_code, 200)
        # Should have saved that survey, so get it now and check it
        s = Survey.query().fetch(1)[0]
        self.assertEqual(s.eis_id, eis_id)
        self.assertEqual(s.first_name, first_name)
        self.assertEqual(s.nbl_finish_timestamp,
                         datetime.fromtimestamp(time / 1000))
        self.assertEqual(s.survey_send_time, expectedSendTime)