Skip to content

Commit

Permalink
Fully migrate codebase to new API
Browse files Browse the repository at this point in the history
Tests now pass and no deprecation warnings are thrown. Beautiful.
  • Loading branch information
maiksprenger committed Jun 20, 2014
1 parent 7b2ed21 commit c3f410b
Show file tree
Hide file tree
Showing 15 changed files with 54 additions and 49 deletions.
28 changes: 14 additions & 14 deletions oscar/apps/basket/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,36 +144,36 @@ def __init__(self, basket, product, *args, **kwargs):
super(AddToBasketForm, self).__init__(*args, **kwargs)

# Dynamically build fields
if product.is_group:
self._create_group_product_fields(product)
if product.is_parent:
self._create_parent_product_fields(product)
self._create_product_fields(product)

# Dynamic form building methods

def _create_group_product_fields(self, product):
def _create_parent_product_fields(self, product):
"""
Adds the fields for a "group"-type product (eg, a parent product with a
list of variants.
list of children.
Currently requires that a stock record exists for the variant
Currently requires that a stock record exists for the children
"""
choices = []
disabled_values = []
for variant in product.variants.all():
# Build a description of the variant, including any pertinent
for child in product.children.all():
# Build a description of the child, including any pertinent
# attributes
attr_summary = variant.attribute_summary
attr_summary = child.attribute_summary
if attr_summary:
summary = attr_summary
else:
summary = variant.get_title()
summary = child.get_title()

# Check if it is available to buy
info = self.basket.strategy.fetch_for_product(variant)
info = self.basket.strategy.fetch_for_product(child)
if not info.availability.is_available_to_buy:
disabled_values.append(variant.id)
disabled_values.append(child.id)

choices.append((variant.id, summary))
choices.append((child.id, summary))

self.fields['variant_id'] = forms.ChoiceField(
choices=tuple(choices), label=_("Variant"),
Expand All @@ -200,13 +200,13 @@ def _add_option_field(self, product, option):

def clean_variant_id(self):
try:
variant = self.base_product.variants.get(
variant = self.base_product.children.get(
id=self.cleaned_data['variant_id'])
except Product.DoesNotExist:
raise forms.ValidationError(
_("Please select a valid product"))

# To avoid duplicate SQL queries, we cache a copy of the loaded variant
# To avoid duplicate SQL queries, we cache a copy of the loaded child
# product as we're going to need it later.
self.variant = variant

Expand Down
2 changes: 1 addition & 1 deletion oscar/apps/catalogue/abstract_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ def __init__(self, *args, **kwargs):
self.attr = ProductAttributesContainer(product=self)

def __unicode__(self):
if self.is_variant:
if self.is_child:
return u"%s (%s)" % (self.get_title(), self.attribute_summary)
return self.get_title()

Expand Down
5 changes: 2 additions & 3 deletions oscar/apps/catalogue/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,8 @@ class ProductClassAdmin(admin.ModelAdmin):


class ProductAdmin(admin.ModelAdmin):
list_display = ('get_title', 'upc', 'get_product_class', 'is_top_level',
'is_variant', 'attribute_summary',
'date_created')
list_display = ('get_title', 'upc', 'get_product_class', 'structure',
'attribute_summary', 'date_created')
prepopulated_fields = {"slug": ("title",)}
inlines = [AttributeInline, CategoryInline, ProductRecommendationInline]

Expand Down
2 changes: 1 addition & 1 deletion oscar/apps/catalogue/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def base_queryset(self):
models to save on queries
"""
return self.select_related('product_class')\
.prefetch_related('variants',
.prefetch_related('children',
'product_options',
'product_class__options',
'stockrecords',
Expand Down
2 changes: 1 addition & 1 deletion oscar/apps/offer/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ def products(self):
return Product.browsable.select_related('product_class',
'stockrecord')\
.filter(is_discountable=True)\
.prefetch_related('variants', 'images',
.prefetch_related('children', 'images',
'product_class__options', 'product_options')
return cond_range.included_products.filter(is_discountable=True)

Expand Down
24 changes: 15 additions & 9 deletions oscar/apps/partner/importers.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,9 @@ def create_product(self, product_class, attribute_codes, row): # noqa
category, partner, sku, price, stock) = row[0:9]

# Create product
is_variant = ptype.lower() == 'variant'
is_group = ptype.lower() == 'group'
is_child = ptype.lower() == 'variant'
is_parent = ptype.lower() == 'group'

if upc:
try:
product = Product.objects.get(upc=upc)
Expand All @@ -181,13 +182,22 @@ def create_product(self, product_class, attribute_codes, row): # noqa
else:
product = Product()

if not is_variant:
if is_child:
product.structure = Product.CHILD
# Assign parent for variants
product.parent = self.parent
elif is_parent:
product.structure = Product.PARENT
else:
product.structure = Product.STANDALONE

if not product.is_child:
product.title = title
product.description = description
product.product_class = product_class

# Attributes
if not is_group:
if not product.is_parent:
for code, value in zip(attribute_codes, row[9:]):
# Need to check if the attribute requires an Option instance
attr = product_class.attributes.get(
Expand All @@ -198,14 +208,10 @@ def create_product(self, product_class, attribute_codes, row): # noqa
value = datetime.strptime(value, "%d/%m/%Y").date()
setattr(product.attr, code, value)

# Assign parent for variants
if is_variant:
product.parent = self.parent

product.save()

# Save a reference to last group product
if is_group:
if is_parent:
self.parent = product

# Category information
Expand Down
4 changes: 2 additions & 2 deletions oscar/apps/search/search_indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def prepare_rating(self, obj):

def prepare_price(self, obj):
result = None
if obj.is_group:
if obj.is_parent:
result = strategy.fetch_for_group(obj)
elif obj.has_stockrecords:
result = strategy.fetch_for_product(obj)
Expand All @@ -70,7 +70,7 @@ def prepare_price(self, obj):

def prepare_num_in_stock(self, obj):
result = None
if obj.is_group:
if obj.is_parent:
# Don't return a stock level for group products
return None
elif obj.has_stockrecords:
Expand Down
2 changes: 1 addition & 1 deletion oscar/templates/oscar/catalogue/partials/product.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ <h3><a href="{{ product.get_absolute_url }}">{{ product.get_title|truncatewords:
{% block product_price %}
<div class="product_price">
{% include "catalogue/partials/stock_record.html" %}
{% if not product.is_group %}
{% if not product.is_parent %}
{% include "catalogue/partials/add_to_basket_form_compact.html" %}
{% endif %}
</div>
Expand Down
4 changes: 2 additions & 2 deletions oscar/templates/oscar/dashboard/catalogue/product_delete.html
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ <h2>{% trans "Delete product" %}</h2>
<p>Delete product <strong>{{ title }}</strong> - are you sure?</p>
{% endblocktrans %}

{% if product.is_group %}
{% if product.is_parent %}
<p> {% trans "This will also delete the following variant products:" %}
<ul>
{% for variant in product.variants.all %}
{% for variant in product.children.all %}
<li><strong>{{ variant.title }}</strong></li>
{% endfor %}
</ul>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ <h2><i class="icon-sitemap icon-large"></i>{{ queryset_description }}</h2>
-
{% endif %}
</td>
<td>{{ product.variants.count }}</td>
<td>{{ product.children.count }}</td>
<td>{{ product.stockrecords.count }}</td>
<td>
<div class="btn-toolbar">
Expand Down
4 changes: 2 additions & 2 deletions oscar/templates/oscar/dashboard/catalogue/product_update.html
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ <h3>{% trans "Sections" %}</h3>
{% endif %}
<li{% if not product %} class="active"{% endif %}><a href="#product_details" data-toggle="tab">{% trans 'Product details' %}</a></li>
<li><a href="#product_recommended" data-toggle="tab">{% trans 'Recommended Products' %}</a></li>
{% if product.variants.exists %}
{% if product.children.exists %}
<li><a href="#product_variants" data-toggle="tab">{% trans 'Product variants' %}</a></li>
{% endif %}
<li><a href="#product_attributes" data-toggle="tab">{% trans 'Product attributes' %}</a></li>
Expand Down Expand Up @@ -178,7 +178,7 @@ <h3>{% trans "Product Details" %}</h3>
{% endblock product_details %}

{% block product_variants %}
{% with variants=product.variants.all %}
{% with variants=product.children.all %}
{% if variants %}
<div class="tab-pane" id="product_variants">
{% block product_variants_content %}
Expand Down
2 changes: 1 addition & 1 deletion oscar/templatetags/basket_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def basket_form(request, product, quantity_type='single'):
return ''

initial = {}
if not product.is_group:
if not product.is_parent:
initial['product_id'] = product.id

form_class = AddToBasketForm
Expand Down
4 changes: 2 additions & 2 deletions oscar/templatetags/purchase_info_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ def render(self, context):
except template.VariableDoesNotExist:
return ''

if product.is_group:
context[self.name_var] = request.strategy.fetch_for_group(product)
if product.is_parent:
context[self.name_var] = request.strategy.fetch_for_parent(product)
else:
context[self.name_var] = request.strategy.fetch_for_product(
product)
Expand Down
12 changes: 6 additions & 6 deletions tests/integration/catalogue/product_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,27 +87,27 @@ def test_variant_products_inherit_parent_titles(self):
p = Product.objects.create(parent=self.parent, product_class=self.product_class)
self.assertEqual("Parent product", p.get_title())

def test_variant_products_inherit_product_class(self):
def test_child_products_inherit_product_class(self):
p = Product.objects.create(parent=self.parent)
self.assertEqual("Clothing", p.get_product_class().name)

def test_variant_products_are_not_part_of_browsable_set(self):
def test_child_products_are_not_part_of_browsable_set(self):
Product.objects.create(
product_class=self.product_class, parent=self.parent)
self.assertEqual(set([self.parent]), set(Product.browsable.all()))


class TestAVariant(TestCase):
class TestAChildProduct(TestCase):

def setUp(self):
clothing = ProductClass.objects.create(
name='Clothing', requires_shipping=True)
self.parent = clothing.products.create(
title="Parent")
self.variant = self.parent.variants.create()
title="Parent", structure=Product.PARENT)
self.child = self.parent.children.create()

def test_delegates_requires_shipping_logic(self):
self.assertTrue(self.variant.is_shipping_required)
self.assertTrue(self.child.is_shipping_required)


class ProductAttributeCreationTests(TestCase):
Expand Down
6 changes: 3 additions & 3 deletions tests/integration/partner/strategy_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def setUp(self):
parent = factories.create_product()
for x in range(3):
factories.create_product(parent=parent)
self.info = self.strategy.fetch_for_group(parent)
self.info = self.strategy.fetch_for_parent(parent)

def test_specifies_product_is_unavailable(self):
self.assertFalse(self.info.availability.is_available_to_buy)
Expand All @@ -76,7 +76,7 @@ def setUp(self):
num_in_stock=3)
for x in range(2):
factories.create_product(parent=parent)
self.info = self.strategy.fetch_for_group(parent)
self.info = self.strategy.fetch_for_parent(parent)

def test_specifies_product_is_available(self):
self.assertTrue(self.info.availability.is_available_to_buy)
Expand All @@ -97,7 +97,7 @@ def setUp(self):
num_in_stock=0)
for x in range(2):
factories.create_product(parent=parent)
self.info = self.strategy.fetch_for_group(parent)
self.info = self.strategy.fetch_for_parent(parent)

def test_specifies_product_is_unavailable(self):
self.assertFalse(self.info.availability.is_available_to_buy)
Expand Down

0 comments on commit c3f410b

Please sign in to comment.