Skip to content

Commit

Permalink
Fixed a bug with subscription saving. Also, Customer.save() now impli…
Browse files Browse the repository at this point in the history
…es Subscription.save() (but a check is done to make sure the subscription is not saved superfluously).
  • Loading branch information
Luke Sneeringer committed Feb 2, 2010
1 parent fcf4a6a commit 62cdeb6
Showing 1 changed file with 35 additions and 2 deletions.
37 changes: 35 additions & 2 deletions __init__.py
Expand Up @@ -308,6 +308,9 @@ def _load_data_from_xml(self, xml, clean = True):
klass = getattr(sys.modules[__name__], single_xml.tag.capitalize()) klass = getattr(sys.modules[__name__], single_xml.tag.capitalize())
setattr(self, single_xml.tag, klass.from_xml(single_xml, parent = self)) setattr(self, single_xml.tag, klass.from_xml(single_xml, parent = self))


# denote a clean version as well
setattr(self, '_clean_%s' % single_xml.tag, getattr(self, single_xml.tag))

continue continue


# okay, it's not a single relationship -- follow my normal # okay, it's not a single relationship -- follow my normal
Expand All @@ -324,6 +327,9 @@ def _load_data_from_xml(self, xml, clean = True):
except AttributeError: except AttributeError:
break break


# set the clean version
setattr(self, '_clean_' + child.tag, getattr(self, child.tag))

# done; move to the next child # done; move to the next child
continue continue


Expand Down Expand Up @@ -358,6 +364,15 @@ def _build_kwargs(self):
kwargs[item[0]] = item[1] kwargs[item[0]] = item[1]


return kwargs return kwargs


def _is_clean(self):
"""Return True if this object has not been modified, False otherwise."""

if len(self._build_kwargs()) == 0:
return True

return False




class Plan(CheddarObject): class Plan(CheddarObject):
Expand Down Expand Up @@ -533,6 +548,11 @@ def save(self):
else: else:
# okay, this isn't new -- send the update request # okay, this isn't new -- send the update request
xml = CheddarGetter.request('/customers/edit/', product_code = self._product_code, code = self._code, **kwargs) xml = CheddarGetter.request('/customers/edit/', product_code = self._product_code, code = self._code, **kwargs)

# if the subscription has been altered, save it too
# (this seems like expected behavior)
if not self.subscription._is_clean():
self.subscription.save()


# either way, I should get a well-formed customer XML response # either way, I should get a well-formed customer XML response
# that can now be loaded into this object # that can now be loaded into this object
Expand Down Expand Up @@ -581,7 +601,7 @@ class Subscription(CheddarObject):
"""An object representing a CheddarGetter subscription.""" """An object representing a CheddarGetter subscription."""


def __init__(self, **kwargs): def __init__(self, **kwargs):
self.plan = Plan() self._clean_plan = self.plan = Plan()
super(Subscription, self).__init__(**kwargs) super(Subscription, self).__init__(**kwargs)




Expand Down Expand Up @@ -649,6 +669,19 @@ def validate(self):
return True return True




def _build_kwargs(self):
"""Build keyword arguments. Make sure plan code is included if appropriate."""

# run the superclass method
kwargs = super(Subscription, self)._build_kwargs()

# make sure plan code is reflected accurately
if self.plan != self._clean_plan:
kwargs['plan_code'] = self.plan.code

return kwargs


def save(self): def save(self):
"""Save this object's properties to CheddarGetter.""" """Save this object's properties to CheddarGetter."""


Expand All @@ -670,7 +703,7 @@ def save(self):
# either way, I should get a well-formed customer XML response # either way, I should get a well-formed customer XML response
# that can now be loaded into this object # that can now be loaded into this object
for subscription_xml in xml.iterchildren('subscription'): for subscription_xml in xml.iterchildren('subscription'):
self._load_data_from_xml(customer_xml) self._load_data_from_xml(subscription_xml)
break break


return self return self
Expand Down

0 comments on commit 62cdeb6

Please sign in to comment.