Пример #1
0
    def put(self) -> Iterable[Union[Mapping, int, None]]:
        """
        Create or update a user. Serializes the data in the request body
        using the UserSchema, validating the inputs in the process to ensure
        all downstream processes leverage clean data, and passes the User
        object to the client to create or update the user record.
        """
        if not request.data:
            return {
                'message': 'No user information provided in the request.'
            }, HTTPStatus.BAD_REQUEST

        try:
            user_attributes = json.loads(request.data)
            schema = UserSchema()
            user = schema.load(user_attributes)

            new_user, user_created = self.client.create_update_user(user=user)
            resp_code = HTTPStatus.CREATED if user_created else HTTPStatus.OK
            return schema.dumps(new_user), resp_code

        except SchemaValidationError as schema_err:
            err_msg = 'User inputs provided are not valid: %s' % schema_err
            return {'message': err_msg}, HTTPStatus.BAD_REQUEST

        except Exception:
            LOGGER.exception('UserDetailAPI PUT Failed')
            return {
                'message': 'Internal server error!'
            }, HTTPStatus.INTERNAL_SERVER_ERROR
Пример #2
0
def load_user(user_data: Dict) -> User:
    try:
        schema = UserSchema()
        # In order to call 'GET_PROFILE_URL' we make sure the user id exists
        if _str_no_value(user_data.get('user_id')):
            user_data['user_id'] = user_data.get('email')
        # Add profile_url from optional 'GET_PROFILE_URL' configuration method.
        # This methods currently exists for the case where the 'profile_url' is not included
        # in the user metadata.
        if _str_no_value(user_data.get(
                'profile_url')) and app.config['GET_PROFILE_URL']:
            user_data['profile_url'] = app.config['GET_PROFILE_URL'](
                user_data['user_id'])
        return schema.load(user_data)
    except ValidationError as err:
        return err.messages