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 post(request, create_survey_input): survey_name = create_survey_input.get('survey_name') validation_utils.validate_param(survey_name, 'survey_name', validation_utils.is_not_blank_str) survey_id_to_clone = create_survey_input.get('survey_id_to_clone') if survey_id_to_clone: return clone_survey(survey_name, survey_id_to_clone) else: return create_survey(survey_name)
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 delete(request, survey_id, question_id): validation_utils.validate_param(survey_id, 'survey_id', validation_utils.is_not_blank_str) validation_utils.validate_param(question_id, 'question_id', validation_utils.is_not_blank_str) survey_metadata = survey_utils.get_survey_metadata_by_id( survey_id, ['questions'], True) for i, question in enumerate(survey_metadata['questions']): if question.get('id') == question_id: try: if question.get('active'): survey_utils.get_survey_table().update_item( Key={'survey_id': survey_id}, UpdateExpression= 'REMOVE questions[{}] ADD num_active_questions :minus_one' .format(i), ConditionExpression= 'questions[{}].id = :question_id and questions[{}].active = :true' .format(i, i), ExpressionAttributeValues={ ':question_id': question_id, ':minus_one': -1, ':true': True }) else: survey_utils.get_survey_table().update_item( Key={'survey_id': survey_id}, UpdateExpression='REMOVE questions[{}]'.format(i), ConditionExpression= 'questions[{}].id = :question_id and questions[{}].active = :false' .format(i, i), ExpressionAttributeValues={ ':question_id': question_id, ':false': False }) except ClientError as e: if e.response['Error'][ 'Code'] == 'ConditionalCheckFailedException': raise errors.ClientError( 'Survey has been modified before update') else: raise RuntimeError('Failed to update DynamoDB') break else: raise errors.ClientError( 'No question with id [{}] found for survey [{}]'.format( question_id, survey_id)) survey_utils.get_question_table().delete_item(Key={ 'survey_id': survey_id, 'question_id': question_id }) return 'success'
def validate_activation_period(activation_start_time, activation_end_time): activation_start_time = activation_start_time if activation_start_time is not None else 0 validation_utils.validate_param(activation_start_time, 'activation_start_time', validation_utils.is_non_negative) validation_utils.validate_param(activation_end_time, 'activation_end_time', validation_utils.is_non_negative_or_none) if activation_end_time is not None and activation_end_time < activation_start_time: raise errors.ClientError( 'range is invalid, activation_start_time: [{}], activation_end_time: [{}]' .format(activation_start_time, activation_end_time)) return activation_start_time, activation_end_time
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"
def put(request, survey_id, question_id, question): validation_utils.validate_param(survey_id, 'survey_id', validation_utils.is_not_blank_str) validation_utils.validate_param(question_id, 'question_id', validation_utils.is_not_blank_str) validation_common.validate_question(question) survey_common.ensure_question_belongs_to_survey(survey_id, question_id) 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 'success'
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 validate_answers(answers): validation_utils.validate_param(answers, 'answers', validation_utils.is_not_empty) if __has_duplicate_answer(answers): raise errors.ClientError('Answers has duplicate elements') for answer in answers: validation_utils.validate_param(answer.get('question_id'), 'question_id', validation_utils.is_not_blank_str) validation_utils.validate_param(answer.get('answer'), 'answer', validation_utils.is_not_empty)
def validate_survey_id(survey_id): validation_utils.validate_param(survey_id, 'survey_id', validation_utils.is_not_blank_str)
def validate_cognito_identity_id(cognito_identity_id): validation_utils.validate_param(cognito_identity_id, 'cognito_identity_id', validation_utils.is_not_blank_str)
def validate_min_max(min, max): validation_utils.validate_param(min, 'min', validation_utils.is_not_none) validation_utils.validate_param(max, 'max', validation_utils.is_not_none) if min > max: raise errors.ClientError( 'range is invalid, min: [{}], max: [{}]'.format(min, max))
def __validate_type_specific_attributes(question): question_type = question.get('type') if question_type == 'text': validation_utils.validate_param(question.get('max_chars'), 'max_chars', validation_utils.is_positive) elif question_type == 'scale': validate_min_max(question.get('min'), question.get('max')) min_label = question.get('min_label') if validation_utils.is_not_none(min_label): validation_utils.validate_param(min_label, 'min_label', validation_utils.is_not_blank_str) max_label = question.get('max_label') if validation_utils.is_not_none(max_label): validation_utils.validate_param(max_label, 'max_label', validation_utils.is_not_blank_str) elif question_type == 'predefined': validation_utils.validate_param(question.get('multiple_select'), 'multiple_select', validation_utils.is_not_none) predefines = question.get('predefines') validation_utils.validate_param(predefines, 'predefines', validation_utils.is_not_empty) for predefine in predefines: validation_utils.validate_param(predefine, 'predefine', validation_utils.is_not_blank_str)
def validate_question_id(question_id): validation_utils.validate_param(question_id, 'question_id', validation_utils.is_not_blank_str)
def __validate_question_title(title): validation_utils.validate_param(title, 'title', validation_utils.is_not_blank_str)