def delete(request, survey_id, submission_id): survey_utils.get_answer_table().delete_item( Key={ 'survey_id': survey_id, 'submission_id': submission_id } ) return { 'status': 'success' }
def get_answer_submissions(survey_id, limit, pagination_token, sort): params = {} params['KeyConditionExpression'] = Key('survey_id').eq(survey_id) params['IndexName'] = 'SubmissionTimeIndex' if limit is not None: params['Limit'] = limit if pagination_token is not None: params['ExclusiveStartKey'] = decode_pagination_token(pagination_token) if sort.lower() == 'desc': params['ScanIndexForward'] = False query_result = survey_utils.get_answer_table().query(**params) submissions = [] for item in query_result['Items']: del item['survey_id'] __convert_answers_map_to_list(item) submissions.append(item) out = {'submissions': submissions} last_evaluated_key = query_result.get('LastEvaluatedKey') if last_evaluated_key is not None: out['pagination_token'] = encode_pagination_token(last_evaluated_key) return out
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}
def post(request, survey_id, answer_list): cognito_identity_id = request.event["cognitoIdentityId"] validation_common.validate_cognito_identity_id(cognito_identity_id) answers = answer_list.get('answers') validation_common.validate_answers(answers) survey_common.ensure_survey_active(survey_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 submission_id = str(uuid.uuid4()) item = {} item['survey_id'] = survey_id item['submission_id'] = submission_id item['user_id'] = cognito_identity_id item['creation_time'] = int(time.time()) answers_map = {} item['answers'] = 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().put_item(Item=item) # +1 num_responses survey_utils.get_survey_table().update_item( Key={'survey_id': survey_id}, UpdateExpression='ADD num_responses :one', ExpressionAttributeValues={':one': 1}) return {'submission_id': submission_id}
def get_answer_submission(survey_id, submission_id): get_result = survey_utils.get_answer_table().get_item(Key={ 'survey_id': survey_id, 'submission_id': submission_id }) submission = get_result.get('Item') if submission is None: return None del submission['survey_id'] del submission['user_id'] __convert_answers_map_to_list(submission) return submission
def main(delete_submissions_info, context): survey_id = delete_submissions_info['survey_id'] submission_id_list = delete_submissions_info['submission_id_list'] idx = 0 no_more_submission = False while True: if idx >= len(submission_id_list): if no_more_submission: break # load at most next 1000 submission ids to avoid exceeding the max payload limit to lambda 128k result = survey_common.get_answer_submission_ids( survey_id, 1000, delete_submissions_info['pagination_token'], 'desc') if len(result['submission_ids']) == 0: break submission_id_list.extend(result['submission_ids']) delete_submissions_info['pagination_token'] = result.get( 'pagination_token') if delete_submissions_info['pagination_token'] is None or len( delete_submissions_info['pagination_token']) == 0: no_more_submission = True submission_id = submission_id_list[idx] idx += 1 survey_utils.get_answer_table().delete_item( Key={ 'survey_id': survey_id, 'submission_id': submission_id }) if should_trigger_next_lambda(context): delete_submissions_info['submission_id_list'] = submission_id_list[ idx:] trigger_next_lambda(delete_submissions_info, context) break
def get(request, survey_id): cognito_identity_id = request.event["cognitoIdentityId"] params = {} params['KeyConditionExpression'] = Key('user_id').eq(cognito_identity_id) & Key('survey_id').eq(survey_id) params['IndexName'] = 'UserAnswersIndex' params['ProjectionExpression'] = 'submission_id' query_result = survey_utils.get_answer_table().query(**params) submission_ids = [] for submission in query_result['Items']: submission_ids.append(submission['submission_id']) return { 'submission_id_list': submission_ids }