Skip to content

Commit bac37af

Browse files
committed
Add useful __repr__() methods to Models & Result
Uses the Model.fields[0] by default, but can be overridden via `Model.repr_field`
1 parent 043d855 commit bac37af

File tree

7 files changed

+19
-8
lines changed

7 files changed

+19
-8
lines changed

chargebee/model.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,25 @@
22

33

44
class Model(object):
5-
6-
fields = []
5+
fields = [] # field list
6+
repr_field = None # field to use for repr(), default is fields[0]
77

88
def __init__(self, values, sub_types=None, dependant_types=None):
99
if sub_types is None:
1010
sub_types = {}
1111
if dependant_types is None:
1212
dependant_types = {}
13-
13+
1414
self.values = values
1515
self.sub_types = sub_types
1616
self.dependant_types = dependant_types
1717
for field in self.fields:
1818
setattr(self, field, None)
1919

20+
def __repr__(self):
21+
repr_field = self.repr_field or self.fields[0]
22+
return "<chargebee.{}: {}={}>".format(self.__class__.__name__, repr_field, getattr(self, repr_field))
23+
2024
def __str__(self):
2125
return json.dumps(self.values, indent=4)
2226

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

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

4751
@classmethod
4852
def construct(cls, values, sub_types=None, dependant_types=None):
4953
obj = cls(values, sub_types, dependant_types)
5054
obj.load(values)
5155
return obj
52-
56+
5357
def init_dependant(self, obj, type, sub_types={}):
5458
if obj.get(type) != None:
5559
if isinstance(obj, dict) and type in self.dependant_types:
@@ -63,4 +67,3 @@ def init_dependant_list(self, obj, type, sub_types={}):
6367
set_val = [self.dependant_types[type].construct(dt, sub_types) for dt in obj[type]]
6468
setattr(self, type, set_val)
6569

66-

chargebee/models/customer.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
class Customer(Model):
77
class BillingAddress(Model):
88
fields = ["first_name", "last_name", "email", "company", "phone", "line1", "line2", "line3", "city", "state_code", "state", "country", "zip", "validation_status"]
9+
repr_field = "zip"
910
pass
1011
class ReferralUrl(Model):
1112
fields = ["external_customer_id", "referral_sharing_url", "created_at", "updated_at", "referral_campaign_id", "referral_account_id", "referral_external_campaign_id", "referral_system"]

chargebee/models/invoice.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,11 @@ class Note(Model):
4242
pass
4343
class ShippingAddress(Model):
4444
fields = ["first_name", "last_name", "email", "company", "phone", "line1", "line2", "line3", "city", "state_code", "state", "country", "zip", "validation_status"]
45+
repr_field = "zip"
4546
pass
4647
class BillingAddress(Model):
4748
fields = ["first_name", "last_name", "email", "company", "phone", "line1", "line2", "line3", "city", "state_code", "state", "country", "zip", "validation_status"]
49+
repr_field = "zip"
4850
pass
4951

5052
fields = ["id", "po_number", "customer_id", "subscription_id", "recurring", "status", "vat_number", \

chargebee/models/payment_source.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
class PaymentSource(Model):
77
class Card(Model):
88
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"]
9+
repr_field = "last4"
910
pass
1011
class BankAccount(Model):
1112
fields = ["last4", "name_on_account", "bank_name", "mandate_id", "account_type", "echeck_type", "account_holder_type"]

chargebee/models/subscription.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class Coupon(Model):
1818
pass
1919
class ShippingAddress(Model):
2020
fields = ["first_name", "last_name", "email", "company", "phone", "line1", "line2", "line3", "city", "state_code", "state", "country", "zip", "validation_status"]
21+
repr_field = "zip"
2122
pass
2223
class ReferralInfo(Model):
2324
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"]

chargebee/models/subscription_estimate.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
class SubscriptionEstimate(Model):
77
class ShippingAddress(Model):
88
fields = ["first_name", "last_name", "email", "company", "phone", "line1", "line2", "line3", "city", "state_code", "state", "country", "zip", "validation_status"]
9+
repr_field = "zip"
910
pass
1011

1112
fields = ["id", "currency_code", "status", "next_billing_at", "pause_date", "resume_date", \

chargebee/result.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ def invoices(self):
214214
def _get_list(self, type, cls, sub_types={}, dependant_types={}, dependant_sub_types={}):
215215
if not type in self._response:
216216
return None
217-
217+
218218
set_val = []
219219
for obj in self._response[type]:
220220
if isinstance(obj, dict):
@@ -238,6 +238,8 @@ def _get(self, type, cls, sub_types=None, dependant_types=None):
238238
def __str__(self):
239239
return json.dumps(self._response, indent=4)
240240

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

242244
class Content(Result):
243245
pass

0 commit comments

Comments
 (0)