From d4d6b08f5cffa8e8696455a6200e4ee15e44ea02 Mon Sep 17 00:00:00 2001 From: Robert Coup Date: Wed, 7 Feb 2018 16:05:48 +0000 Subject: [PATCH] Add useful __repr__() methods to Models & Result Uses the Model.fields[0] by default, but can be overridden via `Model.repr_field` --- chargebee/model.py | 15 +++++++++------ chargebee/models/customer.py | 1 + chargebee/models/invoice.py | 2 ++ chargebee/models/payment_source.py | 1 + chargebee/models/subscription.py | 1 + chargebee/models/subscription_estimate.py | 1 + chargebee/result.py | 4 +++- 7 files changed, 18 insertions(+), 7 deletions(-) diff --git a/chargebee/model.py b/chargebee/model.py index a75c18d..b0acebc 100644 --- a/chargebee/model.py +++ b/chargebee/model.py @@ -2,7 +2,7 @@ class Model(object): - + fields = [] def __init__(self, values, sub_types=None, dependant_types=None): @@ -10,13 +10,17 @@ def __init__(self, values, sub_types=None, dependant_types=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 "".format(self.__class__.__name__, repr_field, getattr(self, repr_field)) + def __str__(self): return json.dumps(self.values, indent=4) @@ -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: @@ -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) - \ No newline at end of file diff --git a/chargebee/models/customer.py b/chargebee/models/customer.py index f4c0c6b..cc64001 100644 --- a/chargebee/models/customer.py +++ b/chargebee/models/customer.py @@ -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"] diff --git a/chargebee/models/invoice.py b/chargebee/models/invoice.py index abf735a..3315f33 100644 --- a/chargebee/models/invoice.py +++ b/chargebee/models/invoice.py @@ -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", \ diff --git a/chargebee/models/payment_source.py b/chargebee/models/payment_source.py index d4436f2..b4062b3 100644 --- a/chargebee/models/payment_source.py +++ b/chargebee/models/payment_source.py @@ -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"] diff --git a/chargebee/models/subscription.py b/chargebee/models/subscription.py index 9c69647..6b01e05 100644 --- a/chargebee/models/subscription.py +++ b/chargebee/models/subscription.py @@ -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"] diff --git a/chargebee/models/subscription_estimate.py b/chargebee/models/subscription_estimate.py index 43965cb..7c1d4b2 100644 --- a/chargebee/models/subscription_estimate.py +++ b/chargebee/models/subscription_estimate.py @@ -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"] diff --git a/chargebee/result.py b/chargebee/result.py index a3d6315..2c066cc 100644 --- a/chargebee/result.py +++ b/chargebee/result.py @@ -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): @@ -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 "".format(";".join(self._response.keys())) class Content(Result): pass