Skip to content

Commit

Permalink
Fix issue with products object within the Promotion resource.
Browse files Browse the repository at this point in the history
Prior to this, the `products` attribute would result in a Query object. This
object always returned a 404, and thus, was not valid.

The correct representation is now a list of product stubs of the appropriate
resource class.
  • Loading branch information
bartek committed Mar 23, 2015
1 parent 475b77f commit a7b666e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 19 deletions.
5 changes: 5 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
History
-------

0.1.37 (2015-03-23)
-------------------

* Fixed: Iterating over ``products`` within the ``promotions`` object now works as expected. Previously, accessing the ``products`` attribute would result in a Query object with incorrect parameters.

0.1.36 (2015-03-17)
-------------------

Expand Down
2 changes: 1 addition & 1 deletion gapipy/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-

__version__ = '0.1.36'
__version__ = '0.1.37'
__title__ = 'gapipy'


Expand Down
35 changes: 17 additions & 18 deletions gapipy/resources/tour/promotion.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
from __future__ import unicode_literals

from ...utils import (
get_resource_class_from_class_name, get_resource_class_from_resource_name
)
from ...utils import get_resource_class_from_resource_name
from ..base import Resource, Product


Expand All @@ -21,18 +19,19 @@ class Promotion(Resource):
'sale_start_date', 'sale_finish_date'
]
_price_fields = ['discount_amount', 'discount_percent']
_resource_collection_fields = [('products', Product)]

def _fill_resource_collection_fields(self, data):
for field, resource_cls in self._resource_collection_fields:
if isinstance(resource_cls, basestring):
resource_cls = get_resource_class_from_class_name(resource_cls)

products = data[field]
if products:
product_type = products[0]['type']
resource_cls = get_resource_class_from_resource_name(product_type)
product_stubs = [resource_cls(p, stub=True) for p in products]
setattr(self, field, product_stubs)
else:
setattr(self, field, [])
_model_collection_fields = [('products', Product)]

def _set_model_collection_field(self, field, data):
# Only products needs special treatment.
if field != 'products':
return super(Promotion, self)._set_model_collection_field(field, data)

product_stubs = []
# Each product can be a different resource, so derive the resource from
# the "type" within the stubbed object.
for product in data:
product_type = product['type']
resource_cls = get_resource_class_from_resource_name(product_type)
stub = resource_cls(product, stub=True)
product_stubs.append(stub)
setattr(self, field, product_stubs)

0 comments on commit a7b666e

Please sign in to comment.