Skip to content

Commit

Permalink
handle Customer meta_data with Metadatum class, get_meta and set_meta…
Browse files Browse the repository at this point in the history
… functions
  • Loading branch information
japerk committed Apr 8, 2011
1 parent 4185098 commit 1fc2b08
Showing 1 changed file with 49 additions and 9 deletions.
58 changes: 49 additions & 9 deletions pycheddar/__init__.py
Expand Up @@ -284,10 +284,8 @@ def _load_data_from_xml(self, xml, clean = True):
clean = False. clean = False.
This method should be considered opaque.""" This method should be considered opaque."""

self._id = xml.get('id') self._id = xml.get('id')
self._code = xml.get('code') self._code = xml.get('code')

# denote relationships where there will only # denote relationships where there will only
# be one child object, rather than an arbitrary set # be one child object, rather than an arbitrary set
singles = ( singles = (
Expand All @@ -297,17 +295,18 @@ def _load_data_from_xml(self, xml, clean = True):
) )


for child in xml.getchildren(): for child in xml.getchildren():
key = to_underscores(child.tag)
# is this an element with children? if so, it's an object # is this an element with children? if so, it's an object
# relationship, not just an attribute # relationship, not just an attribute
if len(child.getchildren()) > 0: if len(child.getchildren()):
# is this a single-esque relationship, as opposed to one # is this a single-esque relationship, as opposed to one
# where the object should contain a list? # where the object should contain a list?
if (xml.tag, child.tag) in singles: if (xml.tag, child.tag) in singles:
single_xml = child.getchildren()[0] single_xml = child.getchildren()[0]
class_name = single_xml.tag.capitalize() class_name = single_xml.tag.capitalize()


if hasattr(sys.modules[__name__], class_name): if hasattr(sys.modules[__name__], class_name):
klass = getattr(sys.modules[__name__], single_xml.tag.capitalize()) klass = getattr(sys.modules[__name__], class_name)
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 # denote a clean version as well
Expand All @@ -317,36 +316,44 @@ def _load_data_from_xml(self, xml, clean = True):


# okay, it's not a single relationship -- follow my normal # okay, it's not a single relationship -- follow my normal
# process for a many to many # process for a many to many
setattr(self, child.tag, []) setattr(self, key, [])

for indiv_xml in child.getchildren(): for indiv_xml in child.getchildren():
# get the class that this item is # get the class that this item is
try: try:
klass = getattr(sys.modules[__name__], indiv_xml.tag.capitalize()) klass = getattr(sys.modules[__name__], indiv_xml.tag.capitalize())


# the XML underneath here constitutes the necessary # the XML underneath here constitutes the necessary
# XML to generate that object; call its XML function # XML to generate that object; call its XML function
getattr(self, child.tag).append(klass.from_xml(indiv_xml, parent = self)) getattr(self, key).append(klass.from_xml(indiv_xml, parent = self))
except AttributeError: except AttributeError:
break break


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

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


# get the element value -- if it's numeric, convert it # get the element value -- if it's numeric, convert it
value = child.text value = child.text

if value is not None: if value is not None:
if re.match(r'^[\d]+$', value): if re.match(r'^[\d]+$', value):
value = int(value) value = int(value)
elif re.match(r'^[\d.]+$', value): elif re.match(r'^[\d.]+$', value):
value = float(value) value = float(value)
elif (xml.tag, child.tag) in singles:
class_name = child.tag.capitalize()

if hasattr(sys.modules[__name__], class_name):
klass = getattr(sys.modules[__name__], class_name)
setattr(self, key, klass(parent = self))


# set the data dictionaries in my object to # set the data dictionaries in my object to
# these values # these values
key = to_underscores(child.tag)
self._data[key] = value self._data[key] = value

if clean is True: if clean is True:
self._clean_data[key] = value self._clean_data[key] = value


Expand Down Expand Up @@ -541,6 +548,10 @@ def save(self):
# build the list of arguments # build the list of arguments
kwargs = self._build_kwargs() kwargs = self._build_kwargs()


if self.meta_data:
for datum in self.meta_data:
kwargs['metaData[%s]' % datum.name] = datum.value

# if this is a new item, then CheddarGetter requires me # if this is a new item, then CheddarGetter requires me
# to send subscription data as well # to send subscription data as well
if self.is_new(): if self.is_new():
Expand Down Expand Up @@ -596,6 +607,7 @@ def get_item(self, item_code):




def add_charge(self, charge_code, item_code, amount = 0.0, quantity = 1, description = None): def add_charge(self, charge_code, item_code, amount = 0.0, quantity = 1, description = None):
"""Increment item quantity for additional charges."""
# set up the kwargs that CheddarGetter expects # set up the kwargs that CheddarGetter expects
kwargs = { kwargs = {
'item_code': item_code, 'item_code': item_code,
Expand All @@ -608,6 +620,31 @@ def add_charge(self, charge_code, item_code, amount = 0.0, quantity = 1, descrip


# send the request to CheddarGetter # send the request to CheddarGetter
xml = CheddarGetter.request('/customers/add-charge/', product_code = self._product_code, code = self.code, **kwargs) xml = CheddarGetter.request('/customers/add-charge/', product_code = self._product_code, code = self.code, **kwargs)

def get_meta(self, name, default=None):
"""Get a meta data value."""
if not self.meta_data: return default

for datum in self.meta_data:
if datum.name == name:
return datum.value

return default

def set_meta(self, name, value):
"""Set a meta data value. To delete a meta data value, set it to an
empty string.
"""
if not self.meta_data:
self.meta_data = [Metadatum(name=name, value=value)]
return

for datum in self.meta_data:
if datum.name == name:
datum.value = value
break
else:
self.meta_data.append(Metadatum(name=name, value=value))




class Subscription(CheddarObject): class Subscription(CheddarObject):
Expand Down Expand Up @@ -830,6 +867,9 @@ class Charge(CheddarObject):
class Transaction(CheddarObject): class Transaction(CheddarObject):
"""An object representing a CheddarGetter transaction.""" """An object representing a CheddarGetter transaction."""


class Metadatum(CheddarObject):
"""An object for holding customer metadata"""

# if we are using Django, and if the appropriate settings # if we are using Django, and if the appropriate settings
# are already set in Django, just import them automatically # are already set in Django, just import them automatically
try: try:
Expand Down

0 comments on commit 1fc2b08

Please sign in to comment.