def post(self): args = self.post_req_parser.parse_args() validation_result = self.validate_scores(args['scores']) if validation_result is not None: return validation_result response_id = args['response_id'] review_form_id = args['review_form_id'] reviewer_user_id = g.current_user['id'] scores = args['scores'] language = args['language'] is_submitted = args['is_submitted'] response_reviewer = review_repository.get_response_reviewer( response_id, reviewer_user_id) if response_reviewer is None: return FORBIDDEN review_response = ReviewResponse(review_form_id, reviewer_user_id, response_id, language) review_response.review_scores = self.get_review_scores(scores) if is_submitted: review_response.submit() review_repository.add_model(review_response) return review_response, 201
def put(self, event_id): dt_format = lambda x: datetime.strptime(x, '%Y-%m-%dT%H:%M:%S') req_parser = reqparse.RequestParser() req_parser.add_argument('id', type=int, required=True) req_parser.add_argument('application_form_id', type=int, required=True) req_parser.add_argument('stage', type=int, required=True) req_parser.add_argument('is_open', type=bool, required=True) req_parser.add_argument('deadline', type=dt_format, required=True) req_parser.add_argument('sections', type=dict, required=True, action='append') args = req_parser.parse_args() id = args['id'] application_form_id = args['application_form_id'] stage = args['stage'] is_open = args['is_open'] deadline = args['deadline'] sections_data = args['sections'] review_form = review_repository.get_review_form_by_id(id) if not review_form: return REVIEW_FORM_NOT_FOUND if application_form_id != review_form.application_form_id: return UPDATE_CONFLICT if event_id != review_form.application_form.event_id: return UPDATE_CONFLICT review_form.stage = stage review_form.is_open = is_open review_form.deadline = deadline # Note: we don't update the active property which is controlled by # the ReviewStageAPI # Delete questions in the review form that no longer exist all_question_ids = [ q["id"] for s in sections_data for q in s["questions"] if "id" in q ] for section in review_form.review_sections: for question in section.review_questions: if question.id not in all_question_ids: review_repository.delete_review_question(question) all_section_ids = [s["id"] for s in sections_data if "id" in s] for section in review_form.review_sections: if section.id not in all_section_ids: review_repository.delete_review_section(section) for section_data in sections_data: if 'id' in section_data: # If ID is populated, then update the existing section section = next((s for s in review_form.review_sections if s.id == section_data['id']), None) # type: ReviewSection if not section: return SECTION_NOT_FOUND current_translations = section.translations # type: Sequence[ReviewSectionTranslation] for current_translation in current_translations: current_translation.description = section_data[ 'description'][current_translation.language] current_translation.headline = section_data['name'][ current_translation.language] section.order = section_data["order"] else: # if not populated, then add new section section = ReviewSection(review_form.id, section_data["order"]) review_repository.add_model(section) languages = section_data['name'].keys() for language in languages: section_translation = ReviewSectionTranslation( section.id, language, section_data['name'][language], section_data['description'][language]) review_repository.add_model(section_translation) for question_data in section_data["questions"]: if "id" in question_data: current_question = next((q for q in section.review_questions if q.id == question_data['id']), None) # type: ReviewQuestion if not current_question: return QUESTION_NOT_FOUND current_question.question_id = question_data[ "question_id"] or None current_question.type = question_data["type"] current_question.order = question_data["order"] current_question.is_required = question_data["is_required"] current_question.weight = question_data["weight"] current_translations = current_question.translations # type: Sequence[ReviewQuestionTranslation] for current_translation in current_translations: current_translation.description = question_data[ 'description'][current_translation.language] current_translation.headline = question_data[ 'headline'][current_translation.language] current_translation.options = question_data['options'][ current_translation.language] current_translation.placeholder = question_data[ 'placeholder'][current_translation.language] current_translation.validation_regex = question_data[ 'validation_regex'][current_translation.language] current_translation.validation_text = question_data[ 'validation_text'][current_translation.language] else: question = ReviewQuestion( review_section_id=section.id, question_id=question_data["question_id"] or None, type=question_data["type"], is_required=question_data["is_required"], order=question_data["order"], weight=question_data["weight"]) review_repository.add_model(question) for language in question_data['headline'].keys(): translation = ReviewQuestionTranslation( review_question_id=question.id, language=language, description=question_data["description"][language], headline=question_data["headline"][language], placeholder=question_data["placeholder"][language], options=question_data["options"][language], validation_regex=question_data["validation_regex"] [language], validation_text=question_data["validation_text"] [language]) review_repository.add_model(translation) db.session.commit() review_form = review_repository.get_review_form_by_id(id) review_form.event_id = event_id return review_form, 200
def post(self, event_id): dt_format = lambda x: datetime.strptime(x, '%Y-%m-%dT%H:%M:%S') req_parser = reqparse.RequestParser() req_parser.add_argument('application_form_id', type=int, required=True) req_parser.add_argument('stage', type=int, required=True) req_parser.add_argument('is_open', type=bool, required=True) req_parser.add_argument('deadline', type=dt_format, required=True) req_parser.add_argument('active', type=bool, required=True) req_parser.add_argument('sections', type=dict, required=True, action='append') args = req_parser.parse_args() application_form_id = args['application_form_id'] stage = args['stage'] is_open = args['is_open'] deadline = args['deadline'] active = args['active'] sections_data = args['sections'] review_form = review_repository.get_review_form(event_id, stage) if review_form is not None: return REVIEW_FORM_EXISTS other_review_forms = review_repository.get_review_forms(event_id) review_form = ReviewForm( application_form_id, deadline, stage, # Only make this stage active if there is no other active stage. active=not any(o.active for o in other_review_forms)) review_repository.add_model(review_form) for section_data in sections_data: section = ReviewSection(review_form.id, section_data['order']) review_repository.add_model(section) languages = section_data['name'].keys() for language in languages: section_translation = ReviewSectionTranslation( review_section_id=section.id, language=language, headline=section_data['name'][language], description=section_data['description'][language]) review_repository.add_model(section_translation) for question_data in section_data['questions']: question = ReviewQuestion( review_section_id=section.id, question_id=question_data['question_id'] or None, type=question_data['type'], is_required=question_data['is_required'], order=question_data['order'], weight=question_data['weight']) review_repository.add_model(question) languages = question_data['headline'].keys() for language in languages: question_translation = ReviewQuestionTranslation( review_question_id=question.id, language=language, description=question_data["description"][language], headline=question_data["headline"][language], placeholder=question_data["placeholder"][language], options=question_data["options"][language], validation_regex=question_data["validation_regex"] [language], validation_text=question_data["validation_text"] [language]) review_repository.add_model(question_translation) new_review_form = review_repository.get_review_form_by_id( review_form.id) new_review_form.event_id = event_id return new_review_form, 201