Skip to content

Commit

Permalink
Add useful __repr__() methods to Models & Result
Browse files Browse the repository at this point in the history
Uses the Model.fields[0] by default, but can be overridden via `Model.repr_field`
  • Loading branch information
rcoup committed Feb 7, 2018
1 parent 54a03bf commit d4d6b08
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 7 deletions.
15 changes: 9 additions & 6 deletions chargebee/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,25 @@


class Model(object):

fields = []

def __init__(self, values, sub_types=None, dependant_types=None):
if sub_types is None:
sub_types = {}
if dependant_types is None:
dependant_types = {}

self.values = values
self.sub_types = sub_types
self.dependant_types = dependant_types
for field in self.fields:
setattr(self, field, None)

def __repr__(self):
repr_field = getattr(self, "repr_field", self.fields[0])
return "<chargebee.{}: {}={}>".format(self.__class__.__name__, repr_field, getattr(self, repr_field))

def __str__(self):
return json.dumps(self.values, indent=4)

Expand All @@ -40,16 +44,16 @@ def load(self, values):

# Returns null for any attribute that starts with cf_ to access the custom fields.
def __getattr__(self, name):
if( name[0:3] == "cf_"):
if( name[0:3] == "cf_"):
return None
raise AttributeError("Attribute %s not found " % name)
raise AttributeError("Attribute %s not found " % name)

@classmethod
def construct(cls, values, sub_types=None, dependant_types=None):
obj = cls(values, sub_types, dependant_types)
obj.load(values)
return obj

def init_dependant(self, obj, type, sub_types={}):
if obj.get(type) != None:
if isinstance(obj, dict) and type in self.dependant_types:
Expand All @@ -63,4 +67,3 @@ def init_dependant_list(self, obj, type, sub_types={}):
set_val = [self.dependant_types[type].construct(dt, sub_types) for dt in obj[type]]
setattr(self, type, set_val)


1 change: 1 addition & 0 deletions chargebee/models/customer.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
class Customer(Model):
class BillingAddress(Model):
fields = ["first_name", "last_name", "email", "company", "phone", "line1", "line2", "line3", "city", "state_code", "state", "country", "zip", "validation_status"]
repr_field = "zip"
pass
class ReferralUrl(Model):
fields = ["external_customer_id", "referral_sharing_url", "created_at", "updated_at", "referral_campaign_id", "referral_account_id", "referral_external_campaign_id", "referral_system"]
Expand Down
2 changes: 2 additions & 0 deletions chargebee/models/invoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@ class Note(Model):
pass
class ShippingAddress(Model):
fields = ["first_name", "last_name", "email", "company", "phone", "line1", "line2", "line3", "city", "state_code", "state", "country", "zip", "validation_status"]
repr_field = "zip"
pass
class BillingAddress(Model):
fields = ["first_name", "last_name", "email", "company", "phone", "line1", "line2", "line3", "city", "state_code", "state", "country", "zip", "validation_status"]
repr_field = "zip"
pass

fields = ["id", "po_number", "customer_id", "subscription_id", "recurring", "status", "vat_number", \
Expand Down
1 change: 1 addition & 0 deletions chargebee/models/payment_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
class PaymentSource(Model):
class Card(Model):
fields = ["first_name", "last_name", "iin", "last4", "brand", "funding_type", "expiry_month", "expiry_year", "billing_addr1", "billing_addr2", "billing_city", "billing_state_code", "billing_state", "billing_country", "billing_zip", "masked_number"]
repr_field = "last4"
pass
class BankAccount(Model):
fields = ["name_on_account", "bank_name", "mandate_id", "account_type"]
Expand Down
1 change: 1 addition & 0 deletions chargebee/models/subscription.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class Coupon(Model):
pass
class ShippingAddress(Model):
fields = ["first_name", "last_name", "email", "company", "phone", "line1", "line2", "line3", "city", "state_code", "state", "country", "zip", "validation_status"]
repr_field = "zip"
pass
class ReferralInfo(Model):
fields = ["referral_code", "coupon_code", "referrer_id", "external_reference_id", "reward_status", "referral_system", "account_id", "campaign_id", "external_campaign_id", "friend_offer_type", "referrer_reward_type", "notify_referral_system", "destination_url", "post_purchase_widget_enabled"]
Expand Down
1 change: 1 addition & 0 deletions chargebee/models/subscription_estimate.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
class SubscriptionEstimate(Model):
class ShippingAddress(Model):
fields = ["first_name", "last_name", "email", "company", "phone", "line1", "line2", "line3", "city", "state_code", "state", "country", "zip", "validation_status"]
repr_field = "zip"
pass

fields = ["id", "currency_code", "status", "next_billing_at", "shipping_address"]
Expand Down
4 changes: 3 additions & 1 deletion chargebee/result.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ def invoices(self):
def _get_list(self, type, cls, sub_types={}, dependant_types={}, dependant_sub_types={}):
if not type in self._response:
return None

set_val = []
for obj in self._response[type]:
if isinstance(obj, dict):
Expand All @@ -206,6 +206,8 @@ def _get(self, type, cls, sub_types=None, dependant_types=None):
def __str__(self):
return json.dumps(self._response, indent=4)

def __repr__(self):
return "<chargebee.Result: {}>".format(";".join(self._response.keys()))

class Content(Result):
pass

0 comments on commit d4d6b08

Please sign in to comment.