def test_submission_language_code_in_payload(self): with patch( "app.views.handlers.submission.get_session_store", return_value=self.session_store, ): submission_handler = SubmissionHandler( QuestionnaireSchema({}), self.questionnaire_store_mock(), {}) assert submission_handler.get_payload( )["submission_language_code"] == "cy"
def block(schema, questionnaire_store, block_id, list_name=None, list_item_id=None): try: block_handler = get_block_handler( schema=schema, block_id=block_id, list_name=list_name, list_item_id=list_item_id, questionnaire_store=questionnaire_store, language=flask_babel.get_locale().language, request_args=request.args, form_data=request.form, ) except InvalidLocationException: raise NotFound if block_handler.block["type"] == "RelationshipCollector": return redirect(block_handler.get_first_location_url()) if "action[clear_radios]" in request.form: block_handler.clear_radio_answers() return redirect(request.url) if request.method == "GET" or (hasattr(block_handler, "form") and not block_handler.form.validate()): return _render_page( template=block_handler.rendered_block["type"], context=block_handler.get_context(), previous_location_url=block_handler.get_previous_location_url(), schema=schema, page_title=block_handler.page_title, ) if block_handler.block["type"] in END_BLOCKS: submission_handler = SubmissionHandler( schema, questionnaire_store, block_handler.router.full_routing_path()) submission_handler.submit_questionnaire() language_code = get_session_store().session_data.language_code if "census" in cookie_session["theme"]: log_out_url = get_census_base_url(cookie_session["theme"], language_code) cookie_session["account_service_log_out_url"] = log_out_url return redirect(url_for("post_submission.get_thank_you")) block_handler.handle_post() next_location_url = block_handler.get_next_location_url() return redirect(next_location_url)
def test_questionnaire_store_delete_called(self): questionnaire_store = self.questionnaire_store_mock() questionnaire_store.delete = Mock() with self.app_request_context(): with patch( "app.views.handlers.submission.get_session_store", return_value=self.session_store, ): submission_handler = SubmissionHandler(QuestionnaireSchema({}), questionnaire_store, full_routing_path=[]) submission_handler.submit_questionnaire() assert questionnaire_store.delete.called
def test_submission_language_code_in_payload(self): session_store = self.session_store.create("eq_session_id", "user_id", self.session_data, self.expires_at) storage = Mock() storage.get_user_data = Mock(return_value=("{}", 1)) with patch( "app.views.handlers.submission.get_session_store", return_value=session_store, ): with patch("app.views.handlers.submission.convert_answers", return_value={}): submission_handler = SubmissionHandler( QuestionnaireSchema({}), QuestionnaireStore(storage), {}) assert (submission_handler.get_payload() ["submission_language_code"] == "cy")
def test_submission_language_code_uses_default_language_if_session_data_language_none( self, ): self.session_data.language_code = None self.session_data.launch_language_code = None session_store = SessionStore("user_ik", "pepper", "eq_session_id").create( "eq_session_id", "user_id", self.session_data, self.expires_at) with patch( "app.views.handlers.submission.get_session_store", return_value=session_store, ): submission_handler = SubmissionHandler( QuestionnaireSchema({}), self.questionnaire_store_mock(), {}) assert submission_handler.get_payload( )["submission_language_code"] == "en"
def dump_submission(schema, questionnaire_store): router = Router( schema, questionnaire_store.answer_store, questionnaire_store.list_store, questionnaire_store.progress_store, questionnaire_store.metadata, ) routing_path = router.full_routing_path() questionnaire_store = get_questionnaire_store(current_user.user_id, current_user.user_ik) submission_handler = SubmissionHandler(schema, questionnaire_store, routing_path) response = {"submission": submission_handler.get_payload()} return json_dumps(response), 200
def block(schema, questionnaire_store, block_id, list_name=None, list_item_id=None): try: block_handler = get_block_handler( schema=schema, block_id=block_id, list_name=list_name, list_item_id=list_item_id, questionnaire_store=questionnaire_store, language=flask_babel.get_locale().language, request_args=request.args, form_data=request.form, ) except InvalidLocationException: raise NotFound if "action[clear_radios]" in request.form: block_handler.clear_radio_answers() return redirect(block_handler.current_location.url()) if request.method != "POST" or (hasattr(block_handler, "form") and not block_handler.form.validate()): return _render_page( template=block_handler.rendered_block["type"], context=block_handler.get_context(), previous_location_url=block_handler.get_previous_location_url(), schema=schema, page_title=block_handler.page_title, ) if block_handler.block["type"] in END_BLOCKS: submission_handler = SubmissionHandler( schema, questionnaire_store, block_handler.router.full_routing_path()) submission_handler.submit_questionnaire() return redirect(url_for("post_submission.get_thank_you")) block_handler.handle_post() next_location_url = block_handler.get_next_location_url() return redirect(next_location_url)
def get_questionnaire(schema, questionnaire_store): router = Router( schema, questionnaire_store.answer_store, questionnaire_store.list_store, questionnaire_store.progress_store, questionnaire_store.metadata, questionnaire_store.response_metadata, ) if not router.can_access_hub(): redirect_location_url = ( router.get_first_incomplete_location_in_questionnaire_url() ) return redirect(redirect_location_url) if request.method == "POST": if router.is_questionnaire_complete: submission_handler = SubmissionHandler( schema, questionnaire_store, router.full_routing_path() ) submission_handler.submit_questionnaire() return redirect(url_for("post_submission.get_thank_you")) return redirect(router.get_first_incomplete_location_in_questionnaire_url()) hub_context = HubContext( language=flask_babel.get_locale().language, schema=schema, answer_store=questionnaire_store.answer_store, list_store=questionnaire_store.list_store, progress_store=questionnaire_store.progress_store, metadata=questionnaire_store.metadata, response_metadata=questionnaire_store.response_metadata, ) context = hub_context( survey_complete=router.is_questionnaire_complete, enabled_section_ids=router.enabled_section_ids, ) return render_template( "hub", content=context, page_title=context["title"], )
def get_questionnaire(schema, questionnaire_store): router = Router( schema, questionnaire_store.answer_store, questionnaire_store.list_store, questionnaire_store.progress_store, questionnaire_store.metadata, ) if not router.can_access_hub(): redirect_location_url = router.get_first_incomplete_location_in_survey_url( ) return redirect(redirect_location_url) if request.method == "POST": if router.is_survey_complete(): submission_handler = SubmissionHandler(schema, questionnaire_store, router.full_routing_path()) submission_handler.submit_questionnaire() return redirect(url_for("post_submission.get_thank_you")) return redirect(router.get_first_incomplete_location_in_survey_url()) language_code = get_session_store().session_data.language_code hub = HubContext( language=language_code, schema=schema, answer_store=questionnaire_store.answer_store, list_store=questionnaire_store.list_store, progress_store=questionnaire_store.progress_store, metadata=questionnaire_store.metadata, ) hub_context = hub.get_context(router.is_survey_complete(), router.enabled_section_ids) return render_template("hub", content=hub_context, page_title=hub_context["title"])
def test_view_submitted_response_true_questionnaire_store_delete_not_called( self): questionnaire_store = self.questionnaire_store_mock() questionnaire_store.delete = Mock() with self.app_request_context(): with patch( "app.views.handlers.submission.get_session_store", return_value=self.session_store, ): submission_handler = SubmissionHandler( QuestionnaireSchema( {"submission": { "view_submitted_response": "True" }}), questionnaire_store, full_routing_path=[], ) submission_handler.submit_questionnaire() assert not questionnaire_store.delete.called
def handle_post(self) -> None: submission_handler = SubmissionHandler(self._schema, self._questionnaire_store, self.router.full_routing_path()) submission_handler.submit_questionnaire()