-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #85 from gadventures/price-promotion-fix
PricePromotion inherits from Promotion
- Loading branch information
Showing
1 changed file
with
14 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'] |