# Path: products/models.py
# class ProductCategory(models.Model):
#     """docstring for Category."""
# 
#     category_name = models.CharField(
#         help_text=_("Nome da categoria de produtos"),
#         verbose_name=_("Categoria de Produto"),
#         max_length=100, null=False, blank=False
#     )
#     father_category = models.ForeignKey(
#         'self',
#         null=True,
#         blank=True
#         )
# 
#     class Meta:
#         verbose_name = _('Categoria de Produtos')
#         verbose_name_plural = ('Categorias de produtos')
# 
#     def __str__(self):
#         return self.category_name
# 
# class Product(models.Model):
#     """docstring for Products."""
# 
#     product_name = models.CharField(
#         help_text=_("Nome completo do Produto"),
#         verbose_name=_("Nome do produto"),
#         max_length=100, null=False, blank=False
#     )
#     category_id = models.ForeignKey(ProductCategory)
#     stock_quantity = models.PositiveIntegerField(
#         help_text=_("Quantidade do produto em estoque"),
#         verbose_name=_("Quantidade do produto"),
#         null=False, blank=False
#     )
#     price = models.FloatField(
#         help_text=_("Preço atual do produto"),
#         verbose_name=_("Preço do produto"),
#         null=False, blank=False,
#         validators=[MinValueValidator(0.1)],
#     )
#     weight = models.FloatField(
#         default=0,
#         help_text=_("Peso do produto atual"),
#         verbose_name=_("Peso do produto"),
#         null=False, blank=False,
#         validators=[MinValueValidator(0.1)],
#     )
#     width = models.PositiveIntegerField(
#         default=500,
#         help_text=_("Largura da Imagem"),
#         verbose_name=_("Largura da Imagem")
#     )
#     height = models.PositiveIntegerField(
#         default=500,
#         help_text=_("Altura da Imagem"),
#         verbose_name=_("Altura da Imagem")
#     )
#     PRODUCT_TYPES = (
#         ('PAD', 'Anuncio'),     # PAD, Product Advertising
#         ('STD', 'Padrao'),      # STD, Standard Product
#     )
#     product_type = models.CharField(default='Padrao', max_length=3,
#                                     choices=PRODUCT_TYPES
#                                     )
#     illustration = models.ImageField(null=False, blank=False,
#                                      width_field='width',
#                                      height_field='height',
#                                      help_text=_("Ilustração"),
#                                      verbose_name=_("Imagem"),
#                                      )
# 
#     class Meta:
#         verbose_name = _('Produto')
#         verbose_name_plural = ('Produtos')
#         ordering = ('product_name', 'category_id', 'stock_quantity', 'price')
# 
#     def save(self, *args, **kwargs):
#         image = Image.open(self.illustration)
#         output = BytesIO()
# 
#         image = image.resize((500, 500))
#         image.save(output, format='PNG', quality=100)
#         output.seek(0)
# 
#         self.illustration = InMemoryUploadedFile(output, 'ImageField',
#                                                  "%s.png" % self
#                                                  .illustration
#                                                  .name.split('.')[0],
#                                                  'image/jpeg',
#                                                  sys.getsizeof(output),
#                                                  None)
# 
#         super(Product, self).save(*args, **kwargs)
# 
#     def __str__(self):
#         return self.product_name
# 
# Path: products/serializers.py
# class ProductCategorySerializerDefault(serializers.ModelSerializer):
#     class Meta:
#         model = ProductCategory
#         fields = "__all__"
# 
# class ProductCategorySerializerPOST(serializers.ModelSerializer):
#     class Meta:
#         model = ProductCategory
#         fields = ('pk', 'category_name', 'father_category')
# 
#     def create(self, validated_data):
#         obj = ProductCategory.objects.create(**validated_data)
#         return obj
# 
# class ProductSerializerDefault(serializers.ModelSerializer):
#     class Meta:
#         model = Product
#         fields = "__all__"
# 
# class ProductSerializerPOST(serializers.ModelSerializer):
#     class Meta:
#         model = Product
#         fields = ('pk', 'product_name', 'category_id', 'stock_quantity',
#                   'price', 'weight', 'width', 'height', 'PRODUCT_TYPES',
#                   'product_type', 'illustration')
# 
#     def create(self, validated_data):
#         obj = Product.objects.create(**validated_data)
#         return obj
# 
# Path: products/permissions.py
# class ProductPermissions(permissions.BasePermission):
# 
#     def __init__(self):
#         self.permission = False
# 
#     def has_permission(self, request, view):
# 
#         if 'products' in request.path or 'categories' in request.path:
#             if request.method == 'GET':
#                 self.permission = True
# 
#         elif request.user.is_superuser:
#             self.permission = True
# 
#         else:
#             self.permission = True
# 
#         return self.permission


from rest_framework.viewsets import ModelViewSet
from .models import (
    ProductCategory,
    Product
    )
from .serializers import (
    ProductCategorySerializerDefault,
    ProductCategorySerializerPOST,
    ProductSerializerDefault,
    ProductSerializerPOST
    )
from .permissions import (
    ProductPermissions
    )
        }
        ```
        """
        response = super(ProductCategoryViewSet, self).\
            partial_update(request, pk, **kwargs)
        return response

    def update(self, request, pk=None, **kwargs):
        """
        API endpoint that allows category to be updated.
        Parameters:
        Cart ID and a JSON with all with all required attributes.
        Example:
        ```
        {
            "category_name": "string"
        }
        ```
        """
        response = super(ProductCategoryViewSet, self).update(
            request, pk, **kwargs
            )
        return response


class ProductViewSet(ModelViewSet):
    """
    API endpoint that allows products to be\
    viewed, created, deleted or edited.
    """

# Path: products/models.py
# class ProductCategory(models.Model):
#     """docstring for Category."""
# 
#     category_name = models.CharField(
#         help_text=_("Nome da categoria de produtos"),
#         verbose_name=_("Categoria de Produto"),
#         max_length=100, null=False, blank=False
#     )
#     father_category = models.ForeignKey(
#         'self',
#         null=True,
#         blank=True
#         )
# 
#     class Meta:
#         verbose_name = _('Categoria de Produtos')
#         verbose_name_plural = ('Categorias de produtos')
# 
#     def __str__(self):
#         return self.category_name
# 
# class Product(models.Model):
#     """docstring for Products."""
# 
#     product_name = models.CharField(
#         help_text=_("Nome completo do Produto"),
#         verbose_name=_("Nome do produto"),
#         max_length=100, null=False, blank=False
#     )
#     category_id = models.ForeignKey(ProductCategory)
#     stock_quantity = models.PositiveIntegerField(
#         help_text=_("Quantidade do produto em estoque"),
#         verbose_name=_("Quantidade do produto"),
#         null=False, blank=False
#     )
#     price = models.FloatField(
#         help_text=_("Preço atual do produto"),
#         verbose_name=_("Preço do produto"),
#         null=False, blank=False,
#         validators=[MinValueValidator(0.1)],
#     )
#     weight = models.FloatField(
#         default=0,
#         help_text=_("Peso do produto atual"),
#         verbose_name=_("Peso do produto"),
#         null=False, blank=False,
#         validators=[MinValueValidator(0.1)],
#     )
#     width = models.PositiveIntegerField(
#         default=500,
#         help_text=_("Largura da Imagem"),
#         verbose_name=_("Largura da Imagem")
#     )
#     height = models.PositiveIntegerField(
#         default=500,
#         help_text=_("Altura da Imagem"),
#         verbose_name=_("Altura da Imagem")
#     )
#     PRODUCT_TYPES = (
#         ('PAD', 'Anuncio'),     # PAD, Product Advertising
#         ('STD', 'Padrao'),      # STD, Standard Product
#     )
#     product_type = models.CharField(default='Padrao', max_length=3,
#                                     choices=PRODUCT_TYPES
#                                     )
#     illustration = models.ImageField(null=False, blank=False,
#                                      width_field='width',
#                                      height_field='height',
#                                      help_text=_("Ilustração"),
#                                      verbose_name=_("Imagem"),
#                                      )
# 
#     class Meta:
#         verbose_name = _('Produto')
#         verbose_name_plural = ('Produtos')
#         ordering = ('product_name', 'category_id', 'stock_quantity', 'price')
# 
#     def save(self, *args, **kwargs):
#         image = Image.open(self.illustration)
#         output = BytesIO()
# 
#         image = image.resize((500, 500))
#         image.save(output, format='PNG', quality=100)
#         output.seek(0)
# 
#         self.illustration = InMemoryUploadedFile(output, 'ImageField',
#                                                  "%s.png" % self
#                                                  .illustration
#                                                  .name.split('.')[0],
#                                                  'image/jpeg',
#                                                  sys.getsizeof(output),
#                                                  None)
# 
#         super(Product, self).save(*args, **kwargs)
# 
#     def __str__(self):
#         return self.product_name
# 
# Path: products/serializers.py
# class ProductCategorySerializerDefault(serializers.ModelSerializer):
#     class Meta:
#         model = ProductCategory
#         fields = "__all__"
# 
# class ProductCategorySerializerPOST(serializers.ModelSerializer):
#     class Meta:
#         model = ProductCategory
#         fields = ('pk', 'category_name', 'father_category')
# 
#     def create(self, validated_data):
#         obj = ProductCategory.objects.create(**validated_data)
#         return obj
# 
# class ProductSerializerDefault(serializers.ModelSerializer):
#     class Meta:
#         model = Product
#         fields = "__all__"
# 
# class ProductSerializerPOST(serializers.ModelSerializer):
#     class Meta:
#         model = Product
#         fields = ('pk', 'product_name', 'category_id', 'stock_quantity',
#                   'price', 'weight', 'width', 'height', 'PRODUCT_TYPES',
#                   'product_type', 'illustration')
# 
#     def create(self, validated_data):
#         obj = Product.objects.create(**validated_data)
#         return obj
# 
# Path: products/permissions.py
# class ProductPermissions(permissions.BasePermission):
# 
#     def __init__(self):
#         self.permission = False
# 
#     def has_permission(self, request, view):
# 
#         if 'products' in request.path or 'categories' in request.path:
#             if request.method == 'GET':
#                 self.permission = True
# 
#         elif request.user.is_superuser:
#             self.permission = True
# 
#         else:
#             self.permission = True
# 
#         return self.permission


# Path: products/viewsets.py
from rest_framework.viewsets import ModelViewSet
from .models import (
    ProductCategory,
    Product
    )
from .serializers import (
    ProductCategorySerializerDefault,
    ProductCategorySerializerPOST,
    ProductSerializerDefault,
    ProductSerializerPOST
    )
from .permissions import (
    ProductPermissions
    )

        }
        ```
        """
        response = super(ProductCategoryViewSet, self).\
            partial_update(request, pk, **kwargs)
        return response

    def update(self, request, pk=None, **kwargs):
        """
        API endpoint that allows category to be updated.
        Parameters:
        Cart ID and a JSON with all with all required attributes.
        Example:
        ```
        {
            "category_name": "string"
        }
        ```
        """
        response = super(ProductCategoryViewSet, self).update(
            request, pk, **kwargs
            )
        return response


class ProductViewSet(ModelViewSet):
    """
    API endpoint that allows products to be\
    viewed, created, deleted or edited.
    """

    queryset = Product.objects.all()