def login(): """ Initial url processing - expects a token parameter and then will authenticate this token. Once authenticated it will be placed in the users session :return: a 302 redirect to the next location for the user """ logger.new() # logging in again clears any session state if session: session.clear() logger.debug("attempting token authentication") authenticator.jwt_login(request) logger.debug("token authenticated - linking to session") metadata = get_metadata(current_user) eq_id = metadata["eq_id"] form_type = metadata["form_type"] logger.bind(eq_id=eq_id, form_type=form_type) if not eq_id or not form_type: logger.error("missing eq id or form type in jwt") raise NotFound json = get_schema(metadata) navigator = PathFinder(json, get_answer_store(current_user), metadata) current_location = navigator.get_latest_location( get_completed_blocks(current_user)) return redirect(current_location.url(metadata))
def get_block(eq_id, form_type, collection_id, group_id, group_instance, block_id): # pylint: disable=unused-argument,too-many-locals current_location = Location(group_id, group_instance, block_id) metadata = get_metadata(current_user) answer_store = get_answer_store(current_user) path_finder = PathFinder(g.schema_json, answer_store, metadata) valid_group = group_id in SchemaHelper.get_group_ids(g.schema_json) full_routing_path = path_finder.get_routing_path() is_valid_location = valid_group and current_location in path_finder.get_routing_path( group_id, group_instance) latest_location = path_finder.get_latest_location( get_completed_blocks(current_user), routing_path=full_routing_path) if not is_valid_location: return _redirect_to_location(collection_id, eq_id, form_type, latest_location) block = _render_schema(current_location) block_type = block['type'] is_skipping_to_end = block_type in [ 'Summary', 'Confirmation' ] and current_location != latest_location if is_skipping_to_end: return _redirect_to_location(collection_id, eq_id, form_type, latest_location) context = _get_context(block, current_location, answer_store) return _build_template(current_location, context, template=block_type, routing_path=full_routing_path)
def _redirect_to_latest_location(collection_id, eq_id, form_type, schema): metadata = get_metadata(current_user) answer_store = get_answer_store(current_user) path_finder = PathFinder(schema, answer_store, metadata) routing_path = path_finder.get_routing_path() latest_location = path_finder.get_latest_location( get_completed_blocks(current_user), routing_path=routing_path) return _redirect_to_location(collection_id, eq_id, form_type, latest_location)
def test_given_some_completed_blocks_when_get_latest_location_then_go_to_next_uncompleted_block(self): # Given no completed blocks survey = load_schema_file("test_repeating_household.json") path_finder = PathFinder(survey) completed_block = [Location('multiple-questions-group', 0, 'introduction')] # When go latest location latest_location = path_finder.get_latest_location(completed_block) # Then go to the first block self.assertEqual(Location('multiple-questions-group', 0, 'household-composition'), latest_location)
def test_given_completed_all_the_blocks_when_get_latest_location_then_go_to_last_block(self): # Given no completed blocks survey = load_schema_file("test_textarea.json") path_finder = PathFinder(survey) completed_block = [Location('textarea-group', 0, 'textarea-block'), Location('textarea-group', 0, 'textarea-summary')] # When go latest location latest_location = path_finder.get_latest_location(completed_block) # Then go to the first block self.assertEqual(Location('textarea-group', 0, 'textarea-summary'), latest_location)
def get_confirmation(eq_id, form_type, collection_id): # pylint: disable=unused-argument answer_store = get_answer_store(current_user) path_finder = PathFinder(g.schema_json, answer_store, get_metadata(current_user)) latest_location = path_finder.get_latest_location(get_completed_blocks(current_user)) if latest_location.block_id == 'confirmation': block = _render_schema(latest_location) return _build_template(current_location=latest_location, context={"block": block}) metadata = get_metadata(current_user) return redirect(latest_location.url(metadata))
def get_summary(eq_id, form_type, collection_id): # pylint: disable=unused-argument answer_store = get_answer_store(current_user) path_finder = PathFinder(g.schema_json, answer_store, get_metadata(current_user)) latest_location = path_finder.get_latest_location(get_completed_blocks(current_user)) metadata = get_metadata(current_user) if latest_location.block_id is 'summary': answers = get_answer_store(current_user) schema_context = build_schema_context(metadata, g.schema.aliases, answers) rendered_schema_json = renderer.render(g.schema_json, **schema_context) summary_context = build_summary_rendering_context(rendered_schema_json, answer_store, metadata) return _build_template(current_location=latest_location, context=summary_context) return redirect(latest_location.url(metadata))
def post_block(eq_id, form_type, collection_id, group_id, group_instance, block_id): # pylint: disable=too-many-locals current_location = Location(group_id, group_instance, block_id) metadata = get_metadata(current_user) answer_store = get_answer_store(current_user) path_finder = PathFinder(g.schema_json, answer_store, metadata) valid_group = group_id in SchemaHelper.get_group_ids(g.schema_json) full_routing_path = path_finder.get_routing_path() is_valid_location = valid_group and current_location in path_finder.get_routing_path( group_id, group_instance) if not is_valid_location: latest_location = path_finder.get_latest_location( get_completed_blocks(current_user), routing_path=full_routing_path) return _redirect_to_location(collection_id, eq_id, form_type, latest_location) error_messages = SchemaHelper.get_messages(g.schema_json) block = _render_schema(current_location) disable_mandatory = 'action[save_sign_out]' in request.form form, _ = post_form_for_location(block, current_location, answer_store, request.form, error_messages, disable_mandatory=disable_mandatory) if 'action[save_sign_out]' in request.form: return _save_sign_out(collection_id, eq_id, form_type, current_location, form) elif _is_invalid_form(form): context = {'form': form, 'block': block} return _build_template(current_location, context, template=block['type'], routing_path=full_routing_path) else: _update_questionnaire_store(current_location, form) next_location = path_finder.get_next_location( current_location=current_location) if next_location is None and block['type'] in [ "Summary", "Confirmation" ]: return submit_answers(eq_id, form_type, collection_id, metadata, answer_store) return redirect(next_location.url(metadata))
def login(): """ Initial url processing - expects a token parameter and then will authenticate this token. Once authenticated it will be placed in the users session :return: a 302 redirect to the next location for the user """ decrypted_token = decrypt_token(request.args.get('token')) metadata = parse_metadata(decrypted_token) eq_id = metadata["eq_id"] form_type = metadata["form_type"] tx_id = metadata["tx_id"] ru_ref = metadata["ru_ref"] logger.bind(eq_id=eq_id, form_type=form_type, tx_id=tx_id, ru_ref=ru_ref) logger.info("decrypted token and parsed metadata") if not eq_id or not form_type: logger.error("missing eq id or form type in jwt") raise NotFound # logging in again clears any session state if session: session.clear() jti_claim = metadata.get('jti') if jti_claim is None: logger.debug('jti claim not provided') else: try: jti_claim_storage = JtiClaimStorage(current_app.eq['database']) jti_claim_storage.use_jti_claim(jti_claim) except JtiTokenUsed as e: raise Unauthorized from e store_session(metadata) json = load_schema_from_metadata(metadata) navigator = PathFinder(json, get_answer_store(current_user), metadata) current_location = navigator.get_latest_location(get_completed_blocks(current_user)) return redirect(current_location.url(metadata))