Skip to content
Merged
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
13 changes: 7 additions & 6 deletions src/shop/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -615,7 +615,7 @@ def labels(self) -> list:
"text": "Bundle",
})

if self.stock_amount is not None:
if self.left_in_stock is not None:
if self.left_in_stock < 1 or not self.is_time_available:
labels.insert(0, {
"type": "sold_out",
Expand All @@ -631,11 +631,12 @@ def labels(self) -> list:
"text": f"Only {self.left_in_stock} left!",
})

if self.available_for_days < 20:
labels.append({
"type": "ending_soon",
"text": f"Sales end in {self.available_for_days} days!",
})
if self.available_for_days is not None:
if self.available_for_days > 0 and self.available_for_days < 20:
labels.append({
"type": "ending_soon",
"text": f"Sales end in {self.available_for_days} days!",
})

return labels

Expand Down
19 changes: 18 additions & 1 deletion src/shop/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,29 @@ def test_labels_for_product_with_stock_below_or_equal_to_10(self):

def test_labels_for_product_ending_within_20_days(self):
"""Test the product returns a 'ending_soon' label object."""
# Without upper availability
available_in = DateTimeTZRange(
lower=timezone.now(),
upper=timezone.now() + timezone.timedelta(6),
upper=None,
)
product = ProductFactory(available_in=available_in)

assert len(product.labels) == 0

# Without lower availability
available_in = DateTimeTZRange(
lower=None,
upper=timezone.now(),
)
product = ProductFactory(available_in=available_in)

assert len(product.labels) == 0

# With stock
available_in = DateTimeTZRange(
lower=timezone.now(),
upper=timezone.now() + timezone.timedelta(6),
)
product = ProductFactory(stock_amount=11, available_in=available_in)

result = product.labels[0]
Expand Down
Loading