From 5a9c877041b6ae7310fc54e6ef05dd0b93dbf03d Mon Sep 17 00:00:00 2001 From: Boris Pfahringer Date: Wed, 18 Jul 2012 15:53:26 +1200 Subject: [PATCH 1/5] Add support for django 1.4 tz-aware datetimes. In django 1.4, tz-aware datetimes have been added. To support this we try to import django.utils.timezone first and fall back to datetime.datetime if it's django < 1.4. Also the default value for the creation time field has been moved into the model. --- cart/cart.py | 3 +-- cart/models.py | 8 +++++++- cart/tests.py | 9 ++++++--- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/cart/cart.py b/cart/cart.py index ff08dc1..8a00327 100644 --- a/cart/cart.py +++ b/cart/cart.py @@ -1,4 +1,3 @@ -import datetime import models CART_ID = 'CART-ID' @@ -26,7 +25,7 @@ def __iter__(self): yield item def new(self, request): - cart = models.Cart(creation_date=datetime.datetime.now()) + cart = models.Cart() cart.save() request.session[CART_ID] = cart.id return cart diff --git a/cart/models.py b/cart/models.py index 6ee264b..64c9ef3 100644 --- a/cart/models.py +++ b/cart/models.py @@ -2,9 +2,15 @@ from django.utils.translation import ugettext_lazy as _ from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes import generic +try: + from django.utils import timezone +except ImportError: + from datetime import datetime as timezone + class Cart(models.Model): - creation_date = models.DateTimeField(verbose_name=_('creation date')) + creation_date = models.DateTimeField( + verbose_name=_('creation date'), default=timezone.now) checked_out = models.BooleanField(default=False, verbose_name=_('checked out')) class Meta: diff --git a/cart/tests.py b/cart/tests.py index 8a0f5a8..3d9e23e 100644 --- a/cart/tests.py +++ b/cart/tests.py @@ -1,12 +1,15 @@ from django.test import TestCase from models import Cart, Item from django.contrib.auth.models import User -import datetime from decimal import Decimal +try: + from django.utils import timezone +except ImportError: + from datetime import datetime as timezone class CartAndItemModelsTestCase(TestCase): - def _create_cart_in_database(self, creation_date=datetime.datetime.now(), + def _create_cart_in_database(self, creation_date=timezone.now(), checked_out=False): """ Helper function so I don't repeat myself @@ -41,7 +44,7 @@ def _create_user_in_database(self): return user def test_cart_creation(self): - creation_date = datetime.datetime.now() + creation_date = timezone.now() cart = self._create_cart_in_database(creation_date) id = cart.id From bf553319de59de8973af5d7b9c31bbc20f908107 Mon Sep 17 00:00:00 2001 From: Boris Pfahringer Date: Wed, 18 Jul 2012 16:08:01 +1200 Subject: [PATCH 2/5] Bump version --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index da1bbc4..fa11236 100644 --- a/setup.py +++ b/setup.py @@ -8,7 +8,7 @@ def read(fname): setup( name='django-cart', - version='1.0.1', + version='1.0.2', description='Django simple shopping cart, tests and south migrations included', maintainer='Bruno Carvalho', maintainer_email='bmentges@gmail.com', From 9d3275cd3ed305f3fb945f31cdf9a4a1defc7023 Mon Sep 17 00:00:00 2001 From: Boris Pfahringer Date: Wed, 18 Jul 2012 17:16:35 +1200 Subject: [PATCH 3/5] Fill-in update method --- cart/cart.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/cart/cart.py b/cart/cart.py index 8a00327..1370698 100644 --- a/cart/cart.py +++ b/cart/cart.py @@ -65,6 +65,9 @@ def update(self, product, quantity, unit_price=None): cart=self.cart, product=product, ) + item.quantity = int(quantity) + item.unit_price = unit_price if unit_price is not None + item.save() except models.Item.DoesNotExist: raise ItemDoesNotExist From 25e79c0e5181f2552237602d3ada428e9b1d245a Mon Sep 17 00:00:00 2001 From: Boris Pfahringer Date: Wed, 18 Jul 2012 17:37:09 +1200 Subject: [PATCH 4/5] Fix silly mistake. --- cart/cart.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cart/cart.py b/cart/cart.py index 1370698..fce5711 100644 --- a/cart/cart.py +++ b/cart/cart.py @@ -66,7 +66,8 @@ def update(self, product, quantity, unit_price=None): product=product, ) item.quantity = int(quantity) - item.unit_price = unit_price if unit_price is not None + if unit_price is not None: + item.unit_price = unit_price item.save() except models.Item.DoesNotExist: raise ItemDoesNotExist From 60772439c065c62de636bc045cad2d0dfd5da41a Mon Sep 17 00:00:00 2001 From: furins Date: Thu, 16 Aug 2012 23:50:20 +0300 Subject: [PATCH 5/5] Update cart/models.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit solves a bug if the model has not an 'id' field. e.g. when a field has 'primary_key=True'  --- cart/models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cart/models.py b/cart/models.py index 64c9ef3..0708c10 100644 --- a/cart/models.py +++ b/cart/models.py @@ -53,7 +53,7 @@ def total_price(self): # product def get_product(self): - return self.content_type.get_object_for_this_type(id=self.object_id) + 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))