# Path: cart/cart.py
# class Cart:
#     def __init__(self, request):
#         cart_id = request.session.get(CART_ID)
#         if cart_id:
#             try:
#                 cart = models.Cart.objects.get(id=cart_id, checked_out=False)
#             except models.Cart.DoesNotExist:
#                 cart = self.new(request)
#         else:
#             cart = self.new(request)
#         self.cart = cart
# 
#     def __iter__(self):
#         for item in self.cart.item_set.all():
#             yield item
# 
#     def new(self, request):
#         cart = models.Cart(creation_date=datetime.datetime.now())
#         cart.save()
#         request.session[CART_ID] = cart.id
#         return cart
# 
#     def add(self, product, unit_price, quantity=1):
#         try:
#             item = models.Item.objects.get(
#                 cart=self.cart,
#                 product=product,
#             )
#         except models.Item.DoesNotExist:
#             item = models.Item()
#             item.cart = self.cart
#             item.product = product
#             item.unit_price = unit_price
#             item.quantity = quantity
#             item.save()
#         else:
#             item.unit_price = unit_price
#             item.quantity += int(quantity)
#             item.save()
# 
#     def remove(self, product):
#         try:
#             item = models.Item.objects.get(
#                 cart=self.cart,
#                 product=product,
#             )
#         except models.Item.DoesNotExist:
#             raise ItemDoesNotExist
#         else:
#             item.delete()
# 
#     def update(self, product, quantity, unit_price=None):
#         try:
#             item = models.Item.objects.get(
#                 cart=self.cart,
#                 product=product,
#             )
#         except models.Item.DoesNotExist:
#             raise ItemDoesNotExist
#         else:
#             if quantity == 0:
#                 item.delete()
#             else:
#                 item.unit_price = unit_price
#                 item.quantity = int(quantity)
#                 item.save()
# 
#     def count(self):
#         result = 0
#         for item in self.cart.item_set.all():
#             result += 1 * item.quantity
#         return result
# 
#     def summary(self):
#         result = 0
#         for item in self.cart.item_set.all():
#             result += item.total_price
#         return result
# 
#     def clear(self):
#         for item in self.cart.item_set.all():
#             item.delete()
# 
# Path: products/models.py
# 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


from cart.cart import Cart
from django.shortcuts import redirect
from django.shortcuts import render_to_response
from django.views.decorators.csrf import csrf_exempt
from django.views.generic import View
from products.models import Product


class CartManagement(View):

    def addToCart(request, product_id, quantity):
        product = Product.objects.get(id=product_id)

# Path: cart/cart.py
# class Cart:
#     def __init__(self, request):
#         cart_id = request.session.get(CART_ID)
#         if cart_id:
#             try:
#                 cart = models.Cart.objects.get(id=cart_id, checked_out=False)
#             except models.Cart.DoesNotExist:
#                 cart = self.new(request)
#         else:
#             cart = self.new(request)
#         self.cart = cart
# 
#     def __iter__(self):
#         for item in self.cart.item_set.all():
#             yield item
# 
#     def new(self, request):
#         cart = models.Cart(creation_date=datetime.datetime.now())
#         cart.save()
#         request.session[CART_ID] = cart.id
#         return cart
# 
#     def add(self, product, unit_price, quantity=1):
#         try:
#             item = models.Item.objects.get(
#                 cart=self.cart,
#                 product=product,
#             )
#         except models.Item.DoesNotExist:
#             item = models.Item()
#             item.cart = self.cart
#             item.product = product
#             item.unit_price = unit_price
#             item.quantity = quantity
#             item.save()
#         else:
#             item.unit_price = unit_price
#             item.quantity += int(quantity)
#             item.save()
# 
#     def remove(self, product):
#         try:
#             item = models.Item.objects.get(
#                 cart=self.cart,
#                 product=product,
#             )
#         except models.Item.DoesNotExist:
#             raise ItemDoesNotExist
#         else:
#             item.delete()
# 
#     def update(self, product, quantity, unit_price=None):
#         try:
#             item = models.Item.objects.get(
#                 cart=self.cart,
#                 product=product,
#             )
#         except models.Item.DoesNotExist:
#             raise ItemDoesNotExist
#         else:
#             if quantity == 0:
#                 item.delete()
#             else:
#                 item.unit_price = unit_price
#                 item.quantity = int(quantity)
#                 item.save()
# 
#     def count(self):
#         result = 0
#         for item in self.cart.item_set.all():
#             result += 1 * item.quantity
#         return result
# 
#     def summary(self):
#         result = 0
#         for item in self.cart.item_set.all():
#             result += item.total_price
#         return result
# 
#     def clear(self):
#         for item in self.cart.item_set.all():
#             item.delete()
# 
# Path: products/models.py
# 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: cart/views.py
from cart.cart import Cart
from django.shortcuts import redirect
from django.shortcuts import render_to_response
from django.views.decorators.csrf import csrf_exempt
from django.views.generic import View
from products.models import Product



class CartManagement(View):

    def addToCart(request, product_id, quantity):
        product = Product.objects.get(id=product_id)

        cart = Cart(request)