Exemple #1
0
    def get(code):
        """Get a preset registering or secured party by client code."""
        try:
            if code is None:
                return resource_utils.path_param_error_response('code')

            # Quick check: must be staff or provide an account ID.
            account_id = resource_utils.get_account_id(request)
            if not is_staff(jwt) and account_id is None:
                return resource_utils.account_required_response()

            # Verify request JWT and account ID
            if not authorized(account_id, jwt):
                return resource_utils.unauthorized_error_response(account_id)

            # Try to fetch client party by code
            current_app.logger.debug(
                f'Getting party code for account {account_id} with code = {code}.'
            )
            party = ClientCode.find_by_code(code)
            if not party:
                return resource_utils.not_found_error_response('party', code)

            return party, HTTPStatus.OK

        except BusinessException as exception:
            return resource_utils.business_exception_response(exception)
        except Exception as default_exception:  # noqa: B902; return nicer default error
            return resource_utils.default_exception_response(default_exception)
Exemple #2
0
    def patch():
        """Update user profile UI settings for the user represented by the request JWT."""
        try:
            # Quick check: always require an account ID.
            account_id = resource_utils.get_account_id(request)
            if not is_staff(jwt) and account_id is None:
                return resource_utils.account_required_response()

            # Verify request JWT and account ID
            if not authorized(account_id, jwt):
                return resource_utils.unauthorized_error_response(account_id)

            request_json = request.get_json(silent=True)
            current_app.logger.debug(
                f'Updating user profile for {account_id} with values: {request_json}'
            )
            # Validate against the schema.
            if not bypass_validation(request_json):
                valid_format, errors = schema_utils.validate(
                    request_json, 'userProfile', 'common')
                if not valid_format:
                    return resource_utils.validation_error_response(
                        errors, VAL_ERROR)

            token = g.jwt_oidc_token_info
            current_app.logger.debug(
                f'Updating user profile for {account_id} with token: {token}')

            # Try to fetch existing user from JWT.
            user = User.find_by_jwt_token(token)
            if not user:
                # If user does not exist, create user and user profile with the default settings.
                current_app.logger.error(
                    f'Update user profile no user found for {account_id} request token.'
                )
                return resource_utils.not_found_error_response(
                    'user profile', account_id)

            user_profile = user.user_profile
            user_profile.update_profile(request_json)
            return user_profile.json, HTTPStatus.OK

        except BusinessException as exception:
            return resource_utils.business_exception_response(exception)
        except Exception as default_exception:  # noqa: B902; return nicer default error
            current_app.logger.error(
                f'Get user profile {account_id} failed: ' +
                repr(default_exception))
            return resource_utils.default_exception_response(default_exception)
Exemple #3
0
    def put(search_id):
        """Execute a search selection update request replacing the current value with the request body contents."""
        try:
            if search_id is None:
                return resource_utils.path_param_error_response('search ID')

            # Quick check: must be staff or provide an account ID.
            account_id = resource_utils.get_account_id(request)
            if account_id is None:
                return resource_utils.account_required_response()

            # Verify request JWT and account ID
            if not authorized(account_id, jwt):
                return resource_utils.unauthorized_error_response(account_id)

            request_json = request.get_json(silent=True)
            # Validate schema.
            valid_format, errors = schema_utils.validate(
                request_json, 'searchSummary', 'ppr')
            if not valid_format:
                return resource_utils.validation_error_response(
                    errors, VAL_ERROR)

            search_request = SearchRequest.find_by_id(search_id)
            if not search_request:
                return resource_utils.not_found_error_response(
                    'searchId', search_id)

            # Save the updated search selection.
            search_request.update_search_selection(request_json)
            return jsonify(
                search_request.updated_selection), HTTPStatus.ACCEPTED

        except BusinessException as exception:
            return resource_utils.business_exception_response(exception)
        except Exception as default_exception:  # noqa: B902; return nicer default error
            return resource_utils.default_exception_response(default_exception)
Exemple #4
0
    def get(search_id):
        """Get search detail information for a previous search."""
        try:
            if search_id is None:
                return resource_utils.path_param_error_response('search ID')

            # Quick check: must have an account ID.
            account_id = resource_utils.get_account_id(request)
            if account_id is None:
                return resource_utils.account_required_response()

            # Verify request JWT and account ID
            if not authorized(account_id, jwt):
                return resource_utils.unauthorized_error_response(account_id)

            # Try to fetch search detail by search id.
            current_app.logger.info(f'Fetching search detail for {search_id}.')
            search_detail = SearchResult.find_by_search_id(search_id, True)
            if not search_detail:
                return resource_utils.not_found_error_response(
                    'searchId', search_id)

            response_data = search_detail.json
            if resource_utils.is_pdf(request):
                token = g.jwt_oidc_token_info
                # Return report if request header Accept MIME type is application/pdf.
                return get_pdf(response_data, account_id,
                               ReportTypes.SEARCH_DETAIL_REPORT.value,
                               token['name'])

            return jsonify(response_data), HTTPStatus.OK

        except BusinessException as exception:
            return resource_utils.business_exception_response(exception)
        except Exception as default_exception:  # noqa: B902; return nicer default error
            return resource_utils.default_exception_response(default_exception)