Skip to content

Commit

Permalink
Merge pull request #85 from gadventures/price-promotion-fix
Browse files Browse the repository at this point in the history
PricePromotion inherits from Promotion
  • Loading branch information
marz619 committed Dec 1, 2017
2 parents 57ec480 + db791ef commit 1a31f44
Showing 1 changed file with 14 additions and 22 deletions.
36 changes: 14 additions & 22 deletions gapipy/models/price_promotion.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,18 @@
from ..resources.base import Resource
from ..utils import get_resource_class_from_resource_name
from gapipy.utils import get_resource_class_from_class_name

# get the Promotion class
#
# NOTE: this is intentional, and needed to ensure we
# are loading the `correct` Promotion class
Promotion = get_resource_class_from_class_name('Promotion')

class PricePromotion(Resource):
"""
The `promotions` referenced in a `Price` are not valid, as they include an
`amount` field that is not part of the Promotion object.

Thus, this intermediatary model must be used to represent a pseudo-Resource.
class PricePromotion(Promotion):
"""
def __init__(self, data, **kwargs):
self._resource_name = 'promotions'

klass = get_resource_class_from_resource_name('promotions')
# Python 2 and 3
# inefficient on Python 2 to list items()
for k, v in list(klass.__dict__.items()):
if 'fields' in k and isinstance(v, list):
setattr(self, k, getattr(klass, k))

super(PricePromotion, self).__init__(data, **kwargs)

# Add the "fake" amount field.
self._price_fields.append('amount')
setattr(self, 'amount', data['amount'])
This extends the definition of the Promotion class
adding the ``amount`` price field, for use in the
`Prices` model
"""
@property
def _price_fields(self):
return super(PricePromotion, self)._price_fields + ['amount']

0 comments on commit 1a31f44

Please sign in to comment.