Beispiel #1
0
def post(request, survey_id, question):
    validation_common.validate_survey_id(survey_id)
    validation_common.validate_question(question)

    survey_common.ensure_survey_exists(survey_id)

    question_id = str(uuid.uuid4())

    survey_utils.get_survey_table().update_item(
        Key={'survey_id':survey_id},
        UpdateExpression='SET questions = list_append(questions, :question) ADD num_active_questions :one',
        ExpressionAttributeValues={
            ':question': [{'id': question_id, 'active': True}],
            ':one': 1
        }
    )

    item = {}
    item['survey_id'] = survey_id
    item['question_id'] = question_id
    item['metadata'] = survey_common.extract_question_metadata(question)
    item['title'] = question['title']
    item['type'] = question['type']
    survey_utils.get_question_table().put_item(
        Item=item
    )

    return {
        'question_id': question_id
    }
def put(request, survey_id, update_activation_period_input):
    validation_common.validate_survey_id(survey_id)
    activation_start_time, activation_end_time = validation_common.validate_activation_period(
        update_activation_period_input.get('activation_start_time'),
        update_activation_period_input.get('activation_end_time'))

    survey_common.ensure_survey_exists(survey_id)

    expression_attribute_values = {
        ':activation_start_time': activation_start_time
    }
    update_expression = 'SET activation_start_time = :activation_start_time'
    if activation_end_time is None:
        update_expression += ' REMOVE activation_end_time'
    else:
        update_expression += ',activation_end_time = :activation_end_time'
        expression_attribute_values[
            ':activation_end_time'] = activation_end_time

    survey_utils.get_survey_table().update_item(
        Key={'survey_id': survey_id},
        UpdateExpression=update_expression,
        ExpressionAttributeValues=expression_attribute_values)

    return "success"
def get(request, survey_id):
    validation_common.validate_survey_id(survey_id)

    query_result = survey_utils.get_answer_aggregation_table().query(
        KeyConditionExpression=Key('survey_id').eq(survey_id)
    )

    survey_answer_aggregations = []
    for each_survey_answer_aggregation in query_result['Items']:
        for key, val in each_survey_answer_aggregation.items():
            if key == 'survey_id':
                continue
            question_id = key
            each_question_answer_aggregation = val

            answer_aggregations = []
            question_answer_aggregations = {
                'question_id': question_id,
                'answer_aggregations': answer_aggregations
            }

            for answer, count in each_question_answer_aggregation.items():
                answer_aggregations.append({
                    'answer': answer,
                    'count': count
                })

            survey_answer_aggregations.append(question_answer_aggregations)

    return {
        'question_answer_aggregations': survey_answer_aggregations
    }
Beispiel #4
0
def put(request, survey_id, question_order):
    validation_common.validate_survey_id(survey_id)

    question_id_list = question_order.get('question_id_list')
    validation_utils.validate_param(question_id_list, 'question_id_list',
                                    validation_utils.is_not_none)

    survey_metadata = survey_utils.get_survey_metadata_by_id(
        survey_id, ['questions'], True)
    question_ids = [x['id'] for x in survey_metadata['questions']]
    if set(question_ids) != set(question_id_list):
        raise errors.ClientError(
            "Question IDs from input doesn't match existing question IDs")

    question_map = {}
    for question in survey_metadata['questions']:
        question_map[question['id']] = question

    new_question_list = [
        question_map[question_id] for question_id in question_id_list
    ]

    survey_utils.get_survey_table().update_item(
        Key={'survey_id': survey_id},
        UpdateExpression='SET questions = :new_question_list',
        ExpressionAttributeValues={':new_question_list': new_question_list})

    return "success"
def put(request, survey_id, question_id, question_status):
    validation_common.validate_survey_id(survey_id)
    validation_common.validate_question_id(question_id)
    enabled = question_status.get('enabled')
    validation_utils.validate_param(enabled, 'enabled', validation_utils.is_not_none)

    if enabled:
        return enable_survey_question(survey_id, question_id)
    else:
        return disable_survey_question(survey_id, question_id)
def put(request, survey_id, rename_survey_input):
    validation_common.validate_survey_id(survey_id)

    survey_name = rename_survey_input.get('survey_name')
    validation_utils.validate_param(survey_name, 'survey_name',
                                    validation_utils.is_not_blank_str)

    survey_common.ensure_survey_exists(survey_id)

    survey_utils.get_survey_table().update_item(
        Key={'survey_id': survey_id},
        UpdateExpression='SET survey_name = :survey_name',
        ExpressionAttributeValues={':survey_name': survey_name})

    return "success"
Beispiel #7
0
def put(request, survey_id, submission_id, answer_list):
    cognito_identity_id = request.event["cognitoIdentityId"]
    validation_common.validate_cognito_identity_id(cognito_identity_id)
    validation_common.validate_survey_id(survey_id)
    validation_common.validate_submission_id(submission_id)
    answers = answer_list.get('answers')
    validation_common.validate_answers(answers)

    survey_common.ensure_survey_active(survey_id)

    submission = survey_utils.get_submission_by_id(survey_id, submission_id,
                                                   ['user_id'], True)
    if submission.get('user_id') != cognito_identity_id:
        raise errors.ClientError(
            "Cognito identity ID [{}] doesn't own this submission".format(
                cognito_identity_id))

    question_ids = [answer['question_id'] for answer in answers]
    if len(question_ids) != len(set(question_ids)):
        raise errors.ClientError("Input has duplicate question IDs")

    questions = survey_common.get_questions(survey_id, question_ids)
    if len(questions) != len(question_ids):
        raise errors.ClientError("Some question IDs not found")
    question_map = {}
    for question in questions:
        question_map[question['question_id']] = question

    answers_map = {}
    for answer in answers:
        question = question_map[answer['question_id']]
        validate_answer_by_question_type(question, answer['answer'])
        # for empty text type answer, replace it with a single whitespace
        # as dynamo db doesn't allow empty string..
        if question['type'] == 'text' and len(answer['answer'][0]) == 0:
            answer['answer'][0] = " "
        answers_map[answer['question_id']] = answer['answer']

    survey_utils.get_answer_table().update_item(
        Key={
            'survey_id': survey_id,
            'submission_id': submission_id
        },
        UpdateExpression='SET answers=:answers',
        ExpressionAttributeValues={':answers': answers_map})

    return {'submission_id': submission_id}
Beispiel #8
0
def put(request, survey_id, survey_published_status):
    validation_common.validate_survey_id(survey_id)
    published = survey_published_status.get('published')
    validation_utils.validate_param(published, 'published',
                                    validation_utils.is_not_none)

    if published:
        survey_metadata = survey_utils.get_survey_metadata_by_id(
            survey_id,
            ['questions', 'activation_start_time', 'activation_end_time'])

        validate_eligible_for_publishing(survey_metadata)

        survey_utils.get_survey_table().update_item(
            Key={'survey_id': survey_id},
            UpdateExpression='SET published = :true',
            ExpressionAttributeValues={':true': 1})
    else:
        survey_utils.get_survey_table().update_item(
            Key={'survey_id': survey_id}, UpdateExpression='REMOVE published')

    return 'success'
def post(request, survey_id):
    validation_common.validate_survey_id(survey_id)

    timestamp = str(int(time.time()*1000))
    request_id = get_request_id(survey_id, timestamp)
    status_file_key = get_status_file_key(request_id)

    # create status file
    create_status_file(status_file_key)

    # initiate multipart upload
    export_file_key = get_export_file_key(request_id)
    s3 = boto3.client('s3', config=Config(signature_version='s3v4'))
    bucket_name = survey_utils.get_answer_submissions_export_s3_bucket_name()
    upload = s3.create_multipart_upload(Bucket=bucket_name, Key=export_file_key)

    upload_info = {
        'survey_id': survey_id,
        'bucket_name': bucket_name,
        'num_submissions_exported': 0,
        'part_num': 1,
        'upload_id': upload['UploadId'],
        'export_file_key': export_file_key,
        'status_file_key': status_file_key,
        'parts_info':{
            'Parts':[]
        },
        'pagination_token': None
    }

    boto3.client('lambda').invoke_async(
        FunctionName=CloudCanvas.get_setting('ExportAnswerSubmissionsCSVLambda'),
        InvokeArgs=json.dumps(upload_info)
    )

    return {
        "request_id": request_id
    }