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 May 24, 2019
1 parent 043d855 commit bac37af
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 8 deletions.
17 changes: 10 additions & 7 deletions chargebee/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,25 @@


class Model(object):

fields = []
fields = [] # field list
repr_field = None # field to use for repr(), default is fields[0]

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 = self.repr_field or 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 @@ -42,9 +42,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 = ["last4", "name_on_account", "bank_name", "mandate_id", "account_type", "echeck_type", "account_holder_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 @@ -18,6 +18,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", "pause_date", "resume_date", \
Expand Down
4 changes: 3 additions & 1 deletion chargebee/result.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,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 @@ -238,6 +238,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 bac37af

Please sign in to comment.