Beispiel #1
0
    def patch(self, id):
        """
        Update a Game Icon
        """
        with db.session.no_autoflush:
            req = api.payload

            # Fetch Game
            game = Game.query.filter_by(id=id).first()
            if game is None:
                return { 'message': 'Game does not exist'}, 404

            # Validate
            # Call method to check if string is base64 and png/jpg/gif  
            # return { 'message': 'Icon not valid'}, 404   

            # Update Icon
            icon = base64_to_pillow_img(req['icon'])
            icon_file_name = '{}.{}'.format(game.id, icon.format.lower())
            icon_bytes = pillow_img_to_bytes(icon)
            delete_img(game.icon, Prefix='games/icons')
            upload_img(icon_bytes, icon.format.lower(), Key=icon_file_name, Prefix='games/icons')
            game.icon = icon_file_name                

            try:
                db.session.commit()
            except Exception:
                return { 'message': 'Unable to edit Game icon'}, 500

            return { 'message': 'Game icon updated successfully' }
Beispiel #2
0
    def post(self):
        with db.session.no_autoflush:
            """
            Add new Game
            """
            req = api.payload
            
            # Set Authenticated User
            current_user = flask_praetorian.current_user()
            req['user'] = user_schema.dump(current_user)
            
            # Validate        
            try:
                new_game = game_schema.load(req)
            except ValidationError as err:
                return { 'error': err.messages }

            # Retrieve auto increment value for "games" table
            try:
                AUTO_INCREMENT = get_auto_increment('GAMES')
            except IndexError:
                return { 'error': 'Unable to retrieve database auto increment value' }, 500

            # Upload Icon
            icon_base64_string = req['icon'].split(',')[1]
            icon = base64_to_pillow_img(icon_base64_string)
            icon_file_name = '{}.{}'.format(AUTO_INCREMENT, icon.format.lower())
            icon_bytes = pillow_img_to_bytes(icon)
            upload_img(icon_bytes, icon.format.lower(), Key=icon_file_name, Prefix='games/icons')
            new_game.icon = icon_file_name

            # Upload Banner
            banner_base64_string = req['banner'].split(',')[1]
            banner = base64_to_pillow_img(banner_base64_string)
            banner_file_name = '{}.{}'.format(AUTO_INCREMENT, banner.format.lower())
            banner_bytes = pillow_img_to_bytes(banner)
            upload_img(banner_bytes, banner.format.lower(), Key=banner_file_name, Prefix='games/banners')
            new_game.banner = banner_file_name

            # Perform SQL Insertion Query
            try:
                db.session.add(new_game)
                db.session.commit()
            except SQLAlchemyError:
                delete_img(new_game.icon, Prefix='games/icons')
                delete_img(new_game.banner, Prefix='games/banners')
                db.session.rollback()
                return { 'error': 'Unable to add Game' }, 500
            
            return { 'message': 'Game added successfully' }
Beispiel #3
0
    def put(self, id):
        """
        Update a Game
        """
        with db.session.no_autoflush:
            req = api.payload
            
            # Fetch Game
            game = Game.query.filter_by(id=id).first()
            if game is None:
                return { 'message': 'Game does not exist'}, 404

            icon_name = game.icon
            banner_name = game.banner

            # Validate        
            try:
                edit_game = game_schema.load(req)
            except ValidationError as err:
                return { 'error': err.messages }
            
            # Update Icon
            if edit_game.icon != icon_name:
                icon_base64_string = req['icon'].split(',')[1]
                icon = base64_to_pillow_img(icon_base64_string)
                icon_file_name = '{}.{}'.format(game.id, icon.format.lower())
                icon_bytes = pillow_img_to_bytes(icon)
                delete_img(icon_name, Prefix='games/icons')
                upload_img(icon_bytes, icon.format.lower(), Key=icon_file_name, Prefix='games/icons')
                edit_game.icon = icon_file_name

            # Update Banner
            if edit_game.banner != banner_name:
                banner_base64_string = req['banner'].split(',')[1]
                banner = base64_to_pillow_img(banner_base64_string)
                banner_file_name = '{}.{}'.format(game.id, banner.format.lower())
                banner_bytes = pillow_img_to_bytes(banner)
                delete_img(banner_name, Prefix='games/banners')
                upload_img(banner_bytes, banner.format.lower(), Key=banner_file_name, Prefix='games/banners')
                edit_game.banner = banner_file_name

            try:
                # db.session.expunge(edit_game)
                db.session.commit()
            except Exception:
                return { 'message': 'Unable to edit Game'}, 500

            return { 'message': 'Game updated successfully' }
Beispiel #4
0
    def patch(self):
        """
        Update User Banner
        """
        req = api.payload

        # Check User permission
        user = flask_praetorian.current_user()
        if user is None:
            return {'message': 'Unauthorized to edit banner'}, 401

        img_base64_string = req['image'].split(',')[1]
        file_type = get_base64_file_type(img_base64_string)

        # Validate
        validation = base64_validation(img_base64_string,
                                       app.config['MAX_USER_BANNER_SIZE'])
        if validation == False:
            return {
                'message': 'Exceeds file size limit or incorrect file type'
            }, 403

        # Edit Banner
        img = base64_to_pillow_img(img_base64_string)
        img_file_name = '{}.{}'.format(user.id, file_type)
        img_bytes = pillow_img_to_bytes(img, file_type)
        if user.banner:
            delete_img(user.banner, Prefix='users/banners')
        upload_img(img_bytes,
                   file_type,
                   Key=img_file_name,
                   Prefix='users/banners')
        user.banner = img_file_name

        try:
            db.session.commit()
        except Exception:
            return {'message': 'Unable to edit banner'}, 500

        response = {
            'user': user_schema.dump(user),
            'message': 'Banner updated successfully'
        }

        return response
Beispiel #5
0
    def delete(self, id):
        """
        Delete a Game by id
        """
        game = Game.query.filter_by(id=id).first()
        if game is None:
            return { 'message': 'Game does not exist'}, 404

        try:
            db.session.delete(game)
            db.session.commit()
        except Exception:
            return { 'message': 'Unable to delete Game'}, 500

        del_icon = delete_img(game.icon, Prefix='games/icons')
        if del_icon["ResponseMetadata"]["HTTPStatusCode"] != 204:
            return { 'message': 'Unable to delete icon'}, 500

        del_banner = delete_img(game.banner, Prefix='games/banners')
        if del_banner["ResponseMetadata"]["HTTPStatusCode"] != 204:
            return { 'message': 'Unable to delete banner'}, 500
        
        return { 'message': 'Game deleted successfully' }