# Path: simplestore/products/models/product.py
# class Product(models.Model):
#     name = models.CharField(max_length=255)
#     slug = models.SlugField(unique=True)
#     sku = models.CharField(
#         max_length=50,
#         verbose_name='Reference Number',
#         unique=True
#     )
#     created_at = models.DateTimeField(auto_now=True, db_index=True)
#     updated_at = models.DateTimeField(auto_now_add=True, db_index=True)
#     image = FilerImageField(null=True, blank=True, on_delete=models.DO_NOTHING)
#     price = models.DecimalField(max_digits=10, decimal_places=2)
#     perex = models.TextField(max_length=255)
#     content = models.TextField(verbose_name='Content Description')
#     category = models.ManyToManyField(Category, blank=True)
#     is_active = models.BooleanField(default=True, verbose_name='Active')
#     is_featured = models.BooleanField(default=False, verbose_name='Featured')
# 
#     objects = ProductManager()
# 
#     _metadata = {
#         'description': 'content',
#     }
# 
#     class Meta:
#         ordering = ['-created_at']
# 
#     @property
#     def get_image_url(self):
#         return self.image.url
# 
#     def get_absolute_url(self):
#         return reverse('products:detail', kwargs={'slug': self.slug})
# 
#     def __str__(self):
#         return self.name
# 
# Path: simplestore/cart/forms.py
# class AddToCartForm(forms.ModelForm):
#     class Meta:
#         model = CartItem
#         fields = [
#             'quantity'
#         ]
#         widgets = {
#             'quantity': forms.NumberInput(attrs={'class': 'full-width'})
#         }
# 
# Path: simplestore/cart/models.py
# class CartItem(models.Model):
#     cart = models.ForeignKey(Cart, null=True, related_name='items', blank=True)
#     product = models.ForeignKey(Product, related_name='products', on_delete=models.DO_NOTHING)
#     date_added = models.DateTimeField(auto_now_add=True)
#     quantity = models.PositiveIntegerField(default=1)
#     total_price = models.DecimalField(max_digits=10, decimal_places=2, null=True)
# 
#     class Meta:
#         ordering = ['date_added']
# 
#     def get_absolute_url(self):
#         return self.product.get_absolute_url()
# 
#     def __str__(self):
#         return self.product.name
# 
# Path: simplestore/cart/utils.py
# def get_cart(request, create=False):
#     """
#     Return current Cart in session or create new Cart object if doesn't exists.
#     :return: Cart object
#     """
#     if not request.session.session_key:
#         request.session.create()
# 
#     if request.user.is_authenticated() and request.session.get('user_cart'):
#         kwargs = {
#             'session_key': request.session['user_cart'],
#             'user': request.user
#         }
#     else:
#         kwargs = {'session_key': request.session.session_key}
# 
#     try:
#         return Cart.objects.get(**kwargs)
#     except Cart.DoesNotExist:
#         if create:
#             return Cart.objects.create(**kwargs)
#         else:
#             return None


from django.contrib import messages
from django.core.urlresolvers import reverse_lazy
from django.shortcuts import get_object_or_404
from django.views.generic import DeleteView, FormView, TemplateView
from simplestore.products.models.product import Product
from .forms import AddToCartForm
from .models import CartItem
from .utils import get_cart
        })

        return context


# Removing item from cart
class RemoveCartItemView(DeleteView):
    model = CartItem
    success_url = reverse_lazy('cart:index')
    success_message = "The item has been deleted from your cart."
    http_method_names = ['post']

    def delete(self, request, *args, **kwargs):
        messages.success(self.request, self.success_message)
        return super(RemoveCartItemView, self).delete(self.request, *args, **kwargs)

    def get_object(self, *args, **kwargs):
        cart = get_cart(self.request)
        return CartItem.objects.get(cart=cart, product_id=self.kwargs['product_id'])


class UpdateCartItemView(FormView):
    http_method_names = ['post']
    success_url = reverse_lazy('cart:index')
    form_class = AddToCartForm
    template_name = 'cart_index.html'
    context_object_name = 'cart'

    def post(self, request, *args, **kwargs):
        form = self.get_form()

# Path: simplestore/products/models/product.py
# class Product(models.Model):
#     name = models.CharField(max_length=255)
#     slug = models.SlugField(unique=True)
#     sku = models.CharField(
#         max_length=50,
#         verbose_name='Reference Number',
#         unique=True
#     )
#     created_at = models.DateTimeField(auto_now=True, db_index=True)
#     updated_at = models.DateTimeField(auto_now_add=True, db_index=True)
#     image = FilerImageField(null=True, blank=True, on_delete=models.DO_NOTHING)
#     price = models.DecimalField(max_digits=10, decimal_places=2)
#     perex = models.TextField(max_length=255)
#     content = models.TextField(verbose_name='Content Description')
#     category = models.ManyToManyField(Category, blank=True)
#     is_active = models.BooleanField(default=True, verbose_name='Active')
#     is_featured = models.BooleanField(default=False, verbose_name='Featured')
# 
#     objects = ProductManager()
# 
#     _metadata = {
#         'description': 'content',
#     }
# 
#     class Meta:
#         ordering = ['-created_at']
# 
#     @property
#     def get_image_url(self):
#         return self.image.url
# 
#     def get_absolute_url(self):
#         return reverse('products:detail', kwargs={'slug': self.slug})
# 
#     def __str__(self):
#         return self.name
# 
# Path: simplestore/cart/forms.py
# class AddToCartForm(forms.ModelForm):
#     class Meta:
#         model = CartItem
#         fields = [
#             'quantity'
#         ]
#         widgets = {
#             'quantity': forms.NumberInput(attrs={'class': 'full-width'})
#         }
# 
# Path: simplestore/cart/models.py
# class CartItem(models.Model):
#     cart = models.ForeignKey(Cart, null=True, related_name='items', blank=True)
#     product = models.ForeignKey(Product, related_name='products', on_delete=models.DO_NOTHING)
#     date_added = models.DateTimeField(auto_now_add=True)
#     quantity = models.PositiveIntegerField(default=1)
#     total_price = models.DecimalField(max_digits=10, decimal_places=2, null=True)
# 
#     class Meta:
#         ordering = ['date_added']
# 
#     def get_absolute_url(self):
#         return self.product.get_absolute_url()
# 
#     def __str__(self):
#         return self.product.name
# 
# Path: simplestore/cart/utils.py
# def get_cart(request, create=False):
#     """
#     Return current Cart in session or create new Cart object if doesn't exists.
#     :return: Cart object
#     """
#     if not request.session.session_key:
#         request.session.create()
# 
#     if request.user.is_authenticated() and request.session.get('user_cart'):
#         kwargs = {
#             'session_key': request.session['user_cart'],
#             'user': request.user
#         }
#     else:
#         kwargs = {'session_key': request.session.session_key}
# 
#     try:
#         return Cart.objects.get(**kwargs)
#     except Cart.DoesNotExist:
#         if create:
#             return Cart.objects.create(**kwargs)
#         else:
#             return None


# Path: simplestore/cart/views.py
from django.contrib import messages
from django.core.urlresolvers import reverse_lazy
from django.shortcuts import get_object_or_404
from django.views.generic import DeleteView, FormView, TemplateView
from simplestore.products.models.product import Product
from .forms import AddToCartForm
from .models import CartItem
from .utils import get_cart

        })

        return context


# Removing item from cart
class RemoveCartItemView(DeleteView):
    model = CartItem
    success_url = reverse_lazy('cart:index')
    success_message = "The item has been deleted from your cart."
    http_method_names = ['post']

    def delete(self, request, *args, **kwargs):
        messages.success(self.request, self.success_message)
        return super(RemoveCartItemView, self).delete(self.request, *args, **kwargs)

    def get_object(self, *args, **kwargs):
        cart = get_cart(self.request)
        return CartItem.objects.get(cart=cart, product_id=self.kwargs['product_id'])


class UpdateCartItemView(FormView):
    http_method_names = ['post']
    success_url = reverse_lazy('cart:index')
    form_class = AddToCartForm
    template_name = 'cart_index.html'
    context_object_name = 'cart'

    def post(self, request, *args, **kwargs):
        form = self.get_form()

        cart = get_cart(request)