# Path: models.py
# class Item(BaseModel):
#     """
#     Item describes a product for the e-commerce platform.
# 
#     Attributes:
#         uuid (UUID): Item UUID
#         name (str): Name for the product
#         price (decimal.Decimal): Price for a single product
#         description (str): Product description
#         availability (int): Quantity of items available
#         category (str): Category group of the item
# 
#     """
#     uuid = UUIDField(unique=True)
#     name = CharField()
#     price = DecimalField(auto_round=True)
#     description = TextField()
#     availability = IntegerField()
#     category = TextField()
#     _schema = ItemSchema
#     _search_attributes = ['name', 'category', 'description']
# 
#     def __str__(self):
#         return '{}, {}, {}, {}'.format(
#             self.uuid,
#             self.name,
#             self.price,
#             self.description)
# 
#     def is_favorite(self, item):
#         for f in self.favorites:
#             if f.item_id == item.id:
#                 return True
#         return False
# 
# class Picture(BaseModel):
#     """
#     A Picture model describes and points to a stored image file. Allows linkage
#     between the image files and one :any:`Item` resource.
# 
#     Attributes:
#         uuid (UUID): Picture's uuid
#         extension (str): Extension of the image file the Picture's model refer to
#         item (:any:`Item`): Foreign key referencing the Item related to the Picture.
#             A ``pictures`` field can be used from ``Item`` to access the Item resource
#             pictures
#     """
#     uuid = UUIDField(unique=True)
#     extension = CharField()
#     item = ForeignKeyField(Item, related_name='pictures')
#     _schema = PictureSchema
# 
#     @property
#     def filename(self):
#         """Full name (uuid.ext) of the file that the Picture model reference."""
#         return '{}.{}'.format(
#             self.uuid,
#             self.extension)
# 
#     def __str__(self):
#         return '{}.{} -> item: {}'.format(
#             self.uuid,
#             self.extension,
#             self.item.uuid)
# 
# Path: utils.py
# def generate_response(data, status, mimetype='application/vnd.api+json'):
#     """
#     Given a resource model that extends from `BaseModel` generate a Reponse
#     object to be returned from the application endpoints'
#     """
#     return Response(
#         response=data,
#         status=status,
#         mimetype=mimetype
#     )


import http.client as client
import os
import uuid
import utils
from flask import request, send_from_directory
from flask_restful import Resource
from models import Item, Picture
from utils import generate_response

        try:
            item = Item.get(Item.uuid == item_uuid)
        except Item.DoesNotExist:
            return None, client.NOT_FOUND

        file = request.files['image']
        picture_uuid = uuid.uuid4()
        extension = os.path.splitext(file.filename)[1][1:]

        if extension not in ALLOWED_EXTENSION:
            return {"message": "File extension not allowed"},\
                client.BAD_REQUEST

        utils.save_image(file, picture_uuid, extension)

        return Picture.create(
            uuid=picture_uuid,
            extension=extension,
            item=item
        ).json(), client.CREATED


class PictureHandler(Resource):
    """Handler of a specific picture"""

    def get(self, picture_uuid):
        """Retrieve the picture specified by picture_uuid"""
        try:
            picture = Picture.get(Picture.uuid == picture_uuid)

# Path: models.py
# class Item(BaseModel):
#     """
#     Item describes a product for the e-commerce platform.
# 
#     Attributes:
#         uuid (UUID): Item UUID
#         name (str): Name for the product
#         price (decimal.Decimal): Price for a single product
#         description (str): Product description
#         availability (int): Quantity of items available
#         category (str): Category group of the item
# 
#     """
#     uuid = UUIDField(unique=True)
#     name = CharField()
#     price = DecimalField(auto_round=True)
#     description = TextField()
#     availability = IntegerField()
#     category = TextField()
#     _schema = ItemSchema
#     _search_attributes = ['name', 'category', 'description']
# 
#     def __str__(self):
#         return '{}, {}, {}, {}'.format(
#             self.uuid,
#             self.name,
#             self.price,
#             self.description)
# 
#     def is_favorite(self, item):
#         for f in self.favorites:
#             if f.item_id == item.id:
#                 return True
#         return False
# 
# class Picture(BaseModel):
#     """
#     A Picture model describes and points to a stored image file. Allows linkage
#     between the image files and one :any:`Item` resource.
# 
#     Attributes:
#         uuid (UUID): Picture's uuid
#         extension (str): Extension of the image file the Picture's model refer to
#         item (:any:`Item`): Foreign key referencing the Item related to the Picture.
#             A ``pictures`` field can be used from ``Item`` to access the Item resource
#             pictures
#     """
#     uuid = UUIDField(unique=True)
#     extension = CharField()
#     item = ForeignKeyField(Item, related_name='pictures')
#     _schema = PictureSchema
# 
#     @property
#     def filename(self):
#         """Full name (uuid.ext) of the file that the Picture model reference."""
#         return '{}.{}'.format(
#             self.uuid,
#             self.extension)
# 
#     def __str__(self):
#         return '{}.{} -> item: {}'.format(
#             self.uuid,
#             self.extension,
#             self.item.uuid)
# 
# Path: utils.py
# def generate_response(data, status, mimetype='application/vnd.api+json'):
#     """
#     Given a resource model that extends from `BaseModel` generate a Reponse
#     object to be returned from the application endpoints'
#     """
#     return Response(
#         response=data,
#         status=status,
#         mimetype=mimetype
#     )


# Path: views/pictures.py
import http.client as client
import os
import uuid
import utils
from flask import request, send_from_directory
from flask_restful import Resource
from models import Item, Picture
from utils import generate_response


        try:
            item = Item.get(Item.uuid == item_uuid)
        except Item.DoesNotExist:
            return None, client.NOT_FOUND

        file = request.files['image']
        picture_uuid = uuid.uuid4()
        extension = os.path.splitext(file.filename)[1][1:]

        if extension not in ALLOWED_EXTENSION:
            return {"message": "File extension not allowed"},\
                client.BAD_REQUEST

        utils.save_image(file, picture_uuid, extension)

        return Picture.create(
            uuid=picture_uuid,
            extension=extension,
            item=item
        ).json(), client.CREATED


class PictureHandler(Resource):
    """Handler of a specific picture"""

    def get(self, picture_uuid):
        """Retrieve the picture specified by picture_uuid"""
        try:
            picture = Picture.get(Picture.uuid == picture_uuid)

        except Picture.DoesNotExist: