Exemple #1
0
    def put(self, menu_id):
        """
        This endpoint is used to upload an image for a menu item. If there is already an image
        for that menu item, it will overwrite the existing file.
        """
        data = self.parser.parse_args()

        if not is_admin(current_identity):
            return {'message': 'Admin privilege not satisfied.'}, 401

        menu = MenuModel.find_by_id(menu_id)
        if not menu:
            return {'message': NOT_FOUND_ERROR.format(f'Menu item <id={menu_id}>')}, 404

        menu_image = MenuImageModel(menu_id)
        # check if the image file exists
        filename = menu_image.find_image_for_menu()
        if filename:  # image for this menu already exists
            # delete the existing image first
            try:
                menu_image.delete_from_disk(filename)
            except IOError:
                traceback.print_exc()
                return {'message': INTERNAL_ERROR.format('Failed to update menu image.')}, 500
        try:
            file = menu_image.save_to_disk(data['image'])
            menu.image = menu_image.get_image_path(file)
            menu.save_to_db()
            return {'message': f'Menu image <{file}> saved!'}, 200
        except UploadNotAllowed:  # forbidden file type
            extension = menu_image.get_extension(data['image'].filename)
            return {'message': f'Extension <{extension}> is not allowed.'}, 400
        except:
            traceback.print_exc()
            return {'message': INTERNAL_ERROR.format('Failed to update menu info.')}, 500
Exemple #2
0
    def post(self):  # post a new menu item
        if not is_admin(current_identity):
            return {'message': 'Admin privilege not satisfied.'}, 401

        data = self.parser.parse_args()
        if MenuModel.find_by_name(data['restaurantID'], data['name']):
            return {'message': ALREADY_EXISTS_ERROR.format(data['name'], data['restaurantID'])}, 400

        # set default values if not specified
        if data['spicy'] is None:
            data['spicy'] = 0  # not spicy by default

        if data['isAvailable'] is None:
            data['isAvailable'] = True  # available by default

        if data['isRecommended'] is None:
            data['isRecommended'] = False  # not recommended by default

        menu = MenuModel(None, **data)
        try:
            menu.save_to_db()
        except:
            traceback.print_exc()
            return {'message': INTERNAL_ERROR.format('Failed to create menu item')}, 500
        return menu.json(), 201
Exemple #3
0
    def put(self, id):
        if not is_admin(current_identity):
            return {'message': 'Admin privilege not satisfied.'}, 401

        menu = MenuModel.find_by_id(id)
        if not menu:
            return {'message': NOT_FOUND_ERROR.format(id)}, 404

        # update with the given params
        data = self.parser.parse_args()
        # if data['name']:
        #     menu.name = data['name']
        # if data['price']:
        #     menu.price = data['price']
        # if data['category']:
        #     menu.category = data['category']
        # if data['description']:
        #     menu.description = data['description']
        # if data['spicy']:
        #     menu.spicy = data['spicy']
        # if data['isAvailable'] is not None:
        #     menu.isAvailable = data['isAvailable']
        # if data['isRecommended'] is not None:
        #     menu.isRecommended = data['isRecommended']
        for key in data.keys():
            if data[key] is not None:
                setattr(menu, key, data[key])
        try:
            menu.save_to_db()
        except:
            traceback.print_exc()
            return {'message': INTERNAL_ERROR.format('Failed to update menu.')}, 500
        return menu.json(), 200
Exemple #4
0
    def get(self, userID):  # find user by id
        if not is_admin(current_identity):
            return {'message': 'Admin privilege not satisfied.'}, 401

        user = UserModel.find_by_id(userID)
        if user:
            return {'user': user.json()}, 200
        return {'message': NOT_FOUND_ERROR.format(userID)}, 404
Exemple #5
0
    def get(self):  # view all users
        if not is_admin(current_identity):
            return {'message': 'Admin privilege not satisfied.'}, 401

        users = []
        result = UserModel.find_all()
        for user in result:
            users.append({
                'id': user.id,
                'stripeID': user.stripeID,
                'email': user.email,
                'phone': user.phone
            })
        return {'users': users}, 200
Exemple #6
0
    def delete(self, userID):
        if not is_admin(current_identity):
            return {'message': 'Admin privilege not satisfied.'}, 401

        user = UserModel.find_by_id(userID)
        if not user:
            return {'message': NOT_FOUND_ERROR.format(userID)}, 404
        try:
            user.delete_from_db()
        except:
            traceback.print_exc()
            return {
                'message':
                INTERNAL_ERROR.format(
                    'Failed to delete user<id:{}>.'.format(userID))
            }, 500
        return {'message': 'User deleted!'}, 200
Exemple #7
0
    def delete(self, menu_id):
        """
        This endpoint is used to delete the requested image under the user's folder.
        It uses the JWT to retrieve user information.
        """
        if not is_admin(current_identity):
            return {'message': 'Admin privilege not satisfied.'}, 401

        menu_image = MenuImageModel(menu_id)

        filename = menu_image.find_image_for_menu()
        if filename:
            try:
                menu_image.delete_from_disk(filename)
                return {'message': f'Image for menu item <id={menu_id}> deleted!'}, 200
            except IOError:
                traceback.print_exc()
                return {'message': INTERNAL_ERROR.format('Failed to delete menu image.')}, 500
        return {'message': f'Image for menu <id={menu_id}> not found.'}, 404
Exemple #8
0
    def put(self, id):  # update a restaurant
        if not is_admin(current_identity):
            return {'message': 'Admin privilege not satisfied.'}, 401

        data = self.parser.parse_args()
        restaurant = RestaurantModel.find_by_id(id)
        if restaurant is None:
            return {'message': NOT_FOUND_ERROR.format(data['name'])}, 404
        # else update with given info restaurant
        if data['name']:
            restaurant.name = data['name']
        if data['fee']:
            restaurant.fee = data['fee']
        if data['limit']:
            restaurant.limit = data['limit']
        if data['address']:
            restaurant.address = data['address']
        if data['openTime']:
            restaurant.openTime = data['openTime']
        if data['closeTime']:
            restaurant.closeTime = data['closeTime']
        if data['isOpen'] is not None:
            restaurant.isOpen = data['isOpen']
        if data['logo']:
            restaurant.logo = data['logo']
        if data['promo']:
            restaurant.promo = data['promo']
        if data['phone']:
            restaurant.phone = data['phone']

        try:
            restaurant.save_to_db()
        except:
            traceback.print_exc()
            return {
                'message':
                INTERNAL_ERROR.format('Failed to update restaurant.')
            }, 500
        return restaurant.json(), 200
Exemple #9
0
    def get(self):  # get all menus, for testing only
        if not is_admin(current_identity):
            return {'message': 'Admin privilege not satisfied.'}, 401

        return {'menus': [menu.json() for menu in MenuModel.find_all()]}, 200