* Fixes #84
pull/92/head
James Ramm 2017-09-17 17:36:04 +02:00 zatwierdzone przez GitHub
rodzic e87c3faca3
commit a34edc0088
5 zmienionych plików z 49 dodań i 8 usunięć

Wyświetl plik

@ -21,7 +21,7 @@ class ProductBase(Page):
def price_range(self):
''' Calculate the price range of the products variants
'''
ordered = self.variants.order_by('price')
ordered = self.variants.order_by('base_price')
if ordered:
return ordered.first().price, ordered.last().price
else:
@ -39,7 +39,7 @@ class ProductVariantBase(models.Model):
"""
Base model for creating product variants
"""
price = models.DecimalField(max_digits=12, decimal_places=2)
base_price = models.DecimalField(max_digits=12, decimal_places=2)
ref = models.CharField(max_length=32)
stock = models.IntegerField(default=0)
@ -52,7 +52,24 @@ class ProductVariantBase(models.Model):
except AttributeError:
return self.ref
@property
def price(self):
"""Can be overridden in concrete implementations in
order to generate the price dynamically.
Override the property like so:
@ProductVariantBase.price.getter
def price(self):
...
"""
return self.base_price
def get_product_title(self):
"""Retrieve the title of the related product.
If no related product, just return the ``ref`` of this model
"""
try:
return self.product.title
except AttributeError:

Wyświetl plik

@ -14,6 +14,7 @@ class TestProducts(WagtailPageTests):
def test_variant_price(self):
variant = ProductVariantFactory()
self.assertTrue(variant.price == variant.base_price * 10)
self.assertTrue(variant.price > 0)
def test_price_range(self):

Wyświetl plik

@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.5 on 2017-09-17 12:47
from __future__ import unicode_literals
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('products', '0004_auto_20170903_1132'),
]
operations = [
migrations.RenameField(
model_name='productvariant',
old_name='price',
new_name='base_price',
),
]

Wyświetl plik

@ -16,15 +16,18 @@ class Product(ProductBase):
description = RichTextField()
content_panels = ProductBase.content_panels + [
FieldPanel('description'),
InlinePanel('variants')
]
@property
def first_image(self):
return self.images.first()
class ProductVariant(ProductVariantBase):
'''Basic product variant for testing
'''
product = ParentalKey(Product, related_name='variants')
product = ParentalKey('products.Product', related_name='variants')
description = RichTextField()
@ProductVariantBase.price.getter
def price(self):
"""Make the price dynamic to check that longclaw works with ``get_price``
"""
return self.base_price * 10

Wyświetl plik

@ -79,7 +79,7 @@ class ProductVariantFactory(factory.django.DjangoModelFactory):
product = factory.SubFactory(ProductFactory)
description = factory.Faker('text')
price = factory.Faker('pyfloat', positive=True, left_digits=2, right_digits=2)
base_price = factory.Faker('pyfloat', positive=True, left_digits=2, right_digits=2)
ref = factory.Faker('pystr', min_chars=3, max_chars=10)
stock = factory.Faker('pyint')