# Path: cart/models.py
# class Cart(models.Model):
#     creation_date = models.DateTimeField(default=datetime.now,
#                                          verbose_name=_('creation date'))
#     checked_out = models.BooleanField(default=False,
#                                       verbose_name=_('checked out'))
# 
#     class Meta:
#         verbose_name = _('cart')
#         verbose_name_plural = _('carts')
#         ordering = ('-creation_date',)
# 
#     def __str__(self):
#         return str(self.creation_date)
# 
# class Item(models.Model):
#     cart = models.ForeignKey(Cart, verbose_name=_('cart'))
#     quantity = models.PositiveIntegerField(verbose_name=_('quantity'))
#     unit_price = models.DecimalField(max_digits=18, decimal_places=2,
#                                      verbose_name=_('unit price'))
# 
#     content_type = models.ForeignKey(ContentType)
#     object_id = models.PositiveIntegerField()
# 
#     objects = ItemManager()
# 
#     class Meta:
#         verbose_name = _('item')
#         verbose_name_plural = _('items')
#         ordering = ('cart',)
# 
#     def __unicode__(self):
#         return u'%d units of %s' % (self.quantity,
#                                     self.product.__class__.__name__)
# 
#     def total_price(self):
#         return self.quantity * self.unit_price
#     total_price = property(total_price)
# 
#     def get_product(self):
#         return self.content_type.get_object_for_this_type(pk=self.object_id)
# 
#     def set_product(self, product):
#         self.content_type = ContentType.objects.get_for_model(type(product))
#         self.object_id = product.pk
# 
#     product = property(get_product, set_product)
# 
# Path: cart/serializers.py
# class CartSerializerDefault(serializers.ModelSerializer):
#     class Meta:
#         model = Cart
#         fields = '__all__'
# 
# class CartSerializerPOST(serializers.ModelSerializer):
#     class Meta:
#         model = Cart
#         fields = ('pk', 'creation_date', 'checked_out')
# 
#     def create(self, validated_data):
#         cart = Cart(**validated_data)
#         return cart
# 
# class ItemSerializerDefault(serializers.ModelSerializer):
#     class Meta:
#         model = Item
#         fields = '__all__'
# 
# class ItemSerializerPOST(serializers.ModelSerializer):
#     class Meta:
#         model = Item
#         fields = ('pk', 'quantity', 'object_id', 'unit_price', 'cart',
#                   'content_type')
# 
#     def create(self, validated_data):
#         item = Item(**validated_data)
#         return item


from rest_framework.permissions import IsAuthenticated
from rest_framework.viewsets import ModelViewSet
from .models import (
    Cart,
    Item
    )
from .serializers import (
    CartSerializerDefault,
    CartSerializerPOST,
    ItemSerializerDefault,
    ItemSerializerPOST
    )


class CartViewSet(ModelViewSet):
    """
    API endpoint that allows Cart to be
    viewed, created, deleted or edited.
    """
    queryset = Cart.objects.all()

# Path: cart/models.py
# class Cart(models.Model):
#     creation_date = models.DateTimeField(default=datetime.now,
#                                          verbose_name=_('creation date'))
#     checked_out = models.BooleanField(default=False,
#                                       verbose_name=_('checked out'))
# 
#     class Meta:
#         verbose_name = _('cart')
#         verbose_name_plural = _('carts')
#         ordering = ('-creation_date',)
# 
#     def __str__(self):
#         return str(self.creation_date)
# 
# class Item(models.Model):
#     cart = models.ForeignKey(Cart, verbose_name=_('cart'))
#     quantity = models.PositiveIntegerField(verbose_name=_('quantity'))
#     unit_price = models.DecimalField(max_digits=18, decimal_places=2,
#                                      verbose_name=_('unit price'))
# 
#     content_type = models.ForeignKey(ContentType)
#     object_id = models.PositiveIntegerField()
# 
#     objects = ItemManager()
# 
#     class Meta:
#         verbose_name = _('item')
#         verbose_name_plural = _('items')
#         ordering = ('cart',)
# 
#     def __unicode__(self):
#         return u'%d units of %s' % (self.quantity,
#                                     self.product.__class__.__name__)
# 
#     def total_price(self):
#         return self.quantity * self.unit_price
#     total_price = property(total_price)
# 
#     def get_product(self):
#         return self.content_type.get_object_for_this_type(pk=self.object_id)
# 
#     def set_product(self, product):
#         self.content_type = ContentType.objects.get_for_model(type(product))
#         self.object_id = product.pk
# 
#     product = property(get_product, set_product)
# 
# Path: cart/serializers.py
# class CartSerializerDefault(serializers.ModelSerializer):
#     class Meta:
#         model = Cart
#         fields = '__all__'
# 
# class CartSerializerPOST(serializers.ModelSerializer):
#     class Meta:
#         model = Cart
#         fields = ('pk', 'creation_date', 'checked_out')
# 
#     def create(self, validated_data):
#         cart = Cart(**validated_data)
#         return cart
# 
# class ItemSerializerDefault(serializers.ModelSerializer):
#     class Meta:
#         model = Item
#         fields = '__all__'
# 
# class ItemSerializerPOST(serializers.ModelSerializer):
#     class Meta:
#         model = Item
#         fields = ('pk', 'quantity', 'object_id', 'unit_price', 'cart',
#                   'content_type')
# 
#     def create(self, validated_data):
#         item = Item(**validated_data)
#         return item


# Path: cart/viewsets.py
from rest_framework.permissions import IsAuthenticated
from rest_framework.viewsets import ModelViewSet
from .models import (
    Cart,
    Item
    )
from .serializers import (
    CartSerializerDefault,
    CartSerializerPOST,
    ItemSerializerDefault,
    ItemSerializerPOST
    )



class CartViewSet(ModelViewSet):
    """
    API endpoint that allows Cart to be
    viewed, created, deleted or edited.
    """
    queryset = Cart.objects.all()

    serializer_class = CartSerializerDefault