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

Fix: Date value out of range when changing default TIME_ZONE variable to another value #879

Open
wants to merge 1 commit 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
17 changes: 12 additions & 5 deletions shop/models/inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from shop.conf import app_settings
from shop.models.product import Availability, BaseReserveProductMixin
from shop.exceptions import ProductNotAvailable
import datetime


class AvailableProductMixin:
Expand All @@ -18,6 +19,7 @@ class AvailableProductMixin:

The product class must implement a field named ``quantity`` accepting numerical values.
"""

def get_availability(self, request, **kwargs):
"""
Returns the current available quantity for this product.
Expand All @@ -39,12 +41,14 @@ def create_availability(**kwargs):
return Availability(quantity=quantity, earliest=earliest, latest=latest, **kwargs)

now = timezone.now()
inventory_set = self.inventory_set.filter(earliest__lt=now, latest__gt=now, quantity__gt=0)
inventory_set = self.inventory_set.filter(
earliest__lt=now, latest__gt=now, quantity__gt=0)
if inventory_set.exists():
return create_availability()
# check, if we can sell short
later = now + app_settings.SHOP_SELL_SHORT_PERIOD
inventory_set = self.inventory_set.filter(earliest__lt=later, latest__gt=now, quantity__gt=0)
inventory_set = self.inventory_set.filter(
earliest__lt=later, latest__gt=now, quantity__gt=0)
if inventory_set.exists():
return create_availability(sell_short=True)
return Availability(quantity=0)
Expand Down Expand Up @@ -104,13 +108,15 @@ class BaseInventory(models.Model):
"""
earliest = models.DateTimeField(
_("Available after"),
default=timezone.datetime.min.replace(tzinfo=timezone.get_current_timezone()),
default=timezone.datetime.min.replace(
tzinfo=timezone.get_current_timezone()) + datetime.timedelta(weeks=1),
db_index=True,
)

latest = models.DateTimeField(
_("Available before"),
default=timezone.datetime.max.replace(tzinfo=timezone.get_current_timezone()),
default=timezone.datetime.max.replace(
tzinfo=timezone.get_current_timezone()) - datetime.timedelta(weeks=1),
db_index=True,
)

Expand All @@ -134,7 +140,8 @@ def check(cls, **kwargs):
if field.attname == 'quantity':
if field.get_internal_type() != cart_field.get_internal_type():
msg = "Field `{}.quantity` must be of same type as `{}.quantity`."
errors.append(checks.Error(msg.format(cls.__name__, CartItemModel.__name__)))
errors.append(checks.Error(msg.format(
cls.__name__, CartItemModel.__name__)))
break
else:
msg = "Class `{}` must implement a field named `quantity`."
Expand Down
11 changes: 8 additions & 3 deletions tests/test_inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from pytest_factoryboy import register
from conftest import CommodityFactory
from testshop.models import MyProduct, MyProductInventory
import datetime


class MyProductFactory(CommodityFactory):
Expand All @@ -21,8 +22,11 @@ class Meta:

product = factory.SubFactory(MyProductFactory)

datetime_min = timezone.datetime.min.replace(tzinfo=timezone.get_current_timezone())
datetime_max = timezone.datetime.max.replace(tzinfo=timezone.get_current_timezone())

datetime_min = timezone.datetime.min.replace(
tzinfo=timezone.get_current_timezone()) + datetime.timedelta(weeks=1)
datetime_max = timezone.datetime.max.replace(
tzinfo=timezone.get_current_timezone()) - datetime.timedelta(weeks=1)


@pytest.mark.django_db
Expand Down Expand Up @@ -59,7 +63,8 @@ def test_limited_offer(api_rf, inventory_factory):
now = timezone.now()
earliest = now
latest = now + app_settings.SHOP_LIMITED_OFFER_PERIOD / 2
inventory = inventory_factory(earliest=earliest, latest=latest, quantity=10)
inventory = inventory_factory(
earliest=earliest, latest=latest, quantity=10)
availability = inventory.product.get_availability(request)
assert availability.quantity == 10
assert availability.earliest == earliest
Expand Down