Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Django 1.4 timezone aware datetime support #5

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions cart/cart.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import datetime
import models

CART_ID = 'CART-ID'
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -66,6 +65,10 @@ def update(self, product, quantity, unit_price=None):
cart=self.cart,
product=product,
)
item.quantity = int(quantity)
if unit_price is not None:
item.unit_price = unit_price
item.save()
except models.Item.DoesNotExist:
raise ItemDoesNotExist

Expand Down
10 changes: 8 additions & 2 deletions cart/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -47,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))
Expand Down
9 changes: 6 additions & 3 deletions cart/tests.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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='[email protected]',
Expand Down