Exemple #1
0
    def post(self):

        # Find user trying to save article
        user = User.find_by_uuid(get_jwt_identity())

        # Check if article is valid
        try:
            article = self.create_article_schema.load(request.form.to_dict())
        except ValidationError as err:
            error_response = self.error_schema.dump(
                ErrorResponse(details=err.messages, error='validation errors'))
            log.info(
                f'Validation errors during article creation: {error_response}')
            return error_response, 400

        # Try to save images to AWS
        saved_images = upload_request_files(request.files)
        article.images = saved_images

        # Add user to article
        article.user = user

        # Save article
        article.commit()

        # Send notification that there is new article
        users_to_notify = User.find_with_mail_notifications_enabled()
        send_notification_mail([user.email for user in users_to_notify],
                               article.title, article.uuid)

        # Return article model
        return self.article_schema.dump(article), 201
Exemple #2
0
    def get(self, uuid):
        # Try to find user
        try:
            uuid = UUID(uuid)
        except ValueError:
            return self.incorrect_uuid(uuid), 404

        user = User.find_by_uuid(uuid)

        # If user is requesting details for another user
        user_claims = self.user_claims_schema.load(get_jwt_claims())
        if user_claims.is_client() and user.uuid != UUID(get_jwt_identity()):
            error_schema = ErrorResponseSchema()
            error_response = error_schema.dump(
                ErrorResponse(
                    details={'uuid': f'Access to that resource is forbidden'},
                    error='User not found'))
            log.info(f'User not found: {error_response}')
            return error_response, 403

        # If there is no user with given id
        if not user:
            error_schema = ErrorResponseSchema()
            error_response = error_schema.dump(
                ErrorResponse(
                    details={'uuid': f'User with {uuid} uuid does not exist'},
                    error='User not found'))
            log.info(f'User not found: {error_response}')
            return error_response, 404

        # Return user
        return self.user_schema.dump(user), 200
Exemple #3
0
    def delete(self, uuid):
        # Try to find user
        try:
            uuid = UUID(uuid)
        except ValueError:
            return self.incorrect_uuid(uuid), 404

        user = User.find_by_uuid(uuid)

        # If there is no user with given id
        if not user:
            error_schema = ErrorResponseSchema()
            error_response = error_schema.dump(
                ErrorResponse(
                    details={'uuid': f'User with {uuid} uuid does not exist'},
                    error='User not found'))
            log.info(f'User not found: {error_response}')
            return error_response, 404

        # delete user
        user.remove()

        return '', 204
Exemple #4
0
    def post(self):
        # Find user
        uuid = get_jwt_identity()
        user = User.find_by_uuid(uuid)
        if not user:
            error_response = self.error_schema.dump(
                ErrorResponse(
                    details={'uuid': f'User with {uuid} uuid does not exist'},
                    error='User not found'))
            log.info(f'User not found: {error_response}')
            return error_response, 404

        # Deserialize input
        notification_preferences = self.notification_preferences_schema.load(
            api.payload)

        # update notification preferences
        for key, value in notification_preferences.items():
            setattr(user, key, value)
        user.commit()

        # return updated user
        return self.user_schema.dump(user), 200
Exemple #5
0
    def get(self):
        # Get identity of user from refresh token
        current_user_uuid = get_jwt_identity()

        # Try to find user in db
        user = User.find_by_uuid(UUID(current_user_uuid))
        if not user:
            error_schema = ErrorResponseSchema()
            error_response = error_schema.dump(
                ErrorResponse(
                    details={'user': ['There is no user with given email']},
                    error='not existing user'))
            log.warn(f'Non existing user {current_user_uuid}' +
                     f' trying to refresh token: {error_response}')
            return error_response, 404

        # Generate new access token with user
        token = TokenDto(create_access_token(identity=user))

        # Return only access token
        token_schema = TokenSchema(only=['access_token'])
        log.info(f'Access token refresh successful')
        return token_schema.dump(token), 200
Exemple #6
0
    def put(self, uuid):
        # Map input data
        try:
            user_update_schema = UpdateUserSchema()
            update_info = user_update_schema.load(api.payload)
        except ValidationError as err:
            error_schema = ErrorResponseSchema()
            error_response = error_schema.dump(
                ErrorResponse(details=err.messages, error='validation errors'))
            log.info(f'Validation errors during user update: {error_response}')
            return error_response, 400

        # Try to find user
        try:
            uuid = UUID(uuid)
        except ValueError:
            return self.incorrect_uuid(uuid), 404

        user = User.find_by_uuid(uuid)

        # If there is no user with given id
        if not user:
            error_schema = ErrorResponseSchema()
            error_response = error_schema.dump(
                ErrorResponse(
                    details={'uuid': f'User with {uuid} uuid does not exist'},
                    error='User not found'))
            log.info(f'User not found: {error_response}')
            return error_response, 404

        # update user properties
        for key, value in update_info.items():
            setattr(user, key, value)
        user.commit()

        # return updated user
        return self.user_schema.dump(user), 200