Skip to content

Commit ca6c568

Browse files
committed
Refactor model construction from results.
* move sub type & dependent type declarations to the model classes * dependent_sub_types moves to the sub type classes themselves * simplify construct/init_* methods
1 parent 8e3b64b commit ca6c568

File tree

14 files changed

+258
-87
lines changed

14 files changed

+258
-87
lines changed

chargebee/model.py

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,11 @@
44
class Model(object):
55
fields = [] # field list
66
repr_field = None # field to use for repr(), default is fields[0]
7+
sub_types = {} # mapping {attr: type}
8+
dependant_types = {} # mapping {attr: type}. If type is a 1-tuple, indicates it's a list.
79

8-
def __init__(self, values, sub_types=None, dependant_types=None):
9-
if sub_types is None:
10-
sub_types = {}
11-
if dependant_types is None:
12-
dependant_types = {}
13-
10+
def __init__(self, values):
1411
self.values = values
15-
self.sub_types = sub_types
16-
self.dependant_types = dependant_types
1712
for field in self.fields:
1813
setattr(self, field, None)
1914

@@ -49,21 +44,15 @@ def __getattr__(self, name):
4944
raise AttributeError("Attribute %s not found " % name)
5045

5146
@classmethod
52-
def construct(cls, values, sub_types=None, dependant_types=None):
53-
obj = cls(values, sub_types, dependant_types)
47+
def construct(cls, values):
48+
obj = cls(values)
5449
obj.load(values)
50+
for k, dependent_type in cls.dependant_types.items():
51+
if values.get(k) is not None:
52+
if isinstance(dependent_type, tuple):
53+
# dependent type being a 1-tuple indicates a list
54+
set_val = [dependent_type[0].construct(v) for v in values[k]]
55+
else:
56+
set_val = dependent_type.construct(values[k])
57+
setattr(obj, k, set_val)
5558
return obj
56-
57-
def init_dependant(self, obj, type, sub_types={}):
58-
if obj.get(type) != None:
59-
if isinstance(obj, dict) and type in self.dependant_types:
60-
dependant_obj = self.dependant_types[type].construct(obj[type], sub_types)
61-
setattr(self, type, dependant_obj)
62-
63-
def init_dependant_list(self, obj, type, sub_types={}):
64-
if obj.get(type) != None:
65-
if isinstance(obj[type],(list, tuple)) and type in self.dependant_types:
66-
if(self.dependant_types != None):
67-
set_val = [self.dependant_types[type].construct(dt, sub_types) for dt in obj[type]]
68-
setattr(self, type, set_val)
69-

chargebee/models/credit_note.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,16 @@ class Allocation(Model):
3232
"sub_total", "round_off_amount", "line_items", "discounts", "line_item_discounts", "taxes", \
3333
"line_item_taxes", "linked_refunds", "allocations", "deleted"]
3434

35+
sub_types = {
36+
'line_items': LineItem,
37+
'discounts': Discount,
38+
'line_item_discounts': LineItemDiscount,
39+
'taxes': Tax,
40+
'line_item_taxes': LineItemTax,
41+
'linked_refunds': LinkedRefund,
42+
'allocations': Allocation,
43+
}
44+
3545

3646
@staticmethod
3747
def create(params, env=None, headers=None):

chargebee/models/customer.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,14 @@ class Balance(Model):
3030
"unbilled_charges", "refundable_credits", "excess_payments", "balances", "meta_data", "deleted", \
3131
"registered_for_gst"]
3232

33+
sub_types = {
34+
'billing_address': BillingAddress,
35+
'referral_urls': ReferralUrl,
36+
'contacts': Contact,
37+
'payment_method': PaymentMethod,
38+
'balances': Balance,
39+
}
40+
3341

3442
@staticmethod
3543
def create(params=None, env=None, headers=None):

chargebee/models/estimate.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,26 @@
33
from chargebee import request
44
from chargebee import APIError
55

6+
from chargebee.models.credit_note_estimate import CreditNoteEstimate
7+
from chargebee.models.invoice_estimate import InvoiceEstimate
8+
from chargebee.models.subscription_estimate import SubscriptionEstimate
9+
from chargebee.models.unbilled_charge import UnbilledCharge
10+
11+
612
class Estimate(Model):
713

814
fields = ["created_at", "subscription_estimate", "invoice_estimate", "invoice_estimates", \
915
"next_invoice_estimate", "credit_note_estimates", "unbilled_charge_estimates"]
1016

17+
dependant_types = {
18+
'subscription_estimate': SubscriptionEstimate,
19+
'invoice_estimate': InvoiceEstimate,
20+
'next_invoice_estimate': InvoiceEstimate,
21+
'invoice_estimates': (InvoiceEstimate,),
22+
'credit_note_estimates': (CreditNoteEstimate,),
23+
'unbilled_charge_estimates': (UnbilledCharge,),
24+
}
25+
1126

1227
@staticmethod
1328
def create_subscription(params, env=None, headers=None):

chargebee/models/event.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ class Webhook(Model):
1212
fields = ["id", "occurred_at", "source", "user", "webhook_status", "webhook_failure_reason", \
1313
"webhooks", "event_type", "api_version"]
1414

15+
sub_types = {
16+
'webhooks': Webhook,
17+
}
18+
19+
1520
@property
1621
def content(self):
1722
from chargebee import Content
@@ -23,12 +28,12 @@ def deserialize(json_data):
2328
webhook_data = json.loads(json_data)
2429
except (TypeError, ValueError) as ex:
2530
raise Exception("The passed json_data is not JSON formatted . " + ex.message)
26-
31+
2732
api_version = webhook_data.get('api_version', None)
2833
env_version = Environment.API_VERSION
29-
if api_version != None and api_version.upper() != env_version.upper():
34+
if api_version != None and api_version.upper() != env_version.upper():
3035
raise Exception("API version [" + api_version.upper() + "] in response does not match "
31-
+ "with client library API version [" + env_version.upper() + "]")
36+
+ "with client library API version [" + env_version.upper() + "]")
3237
return Event.construct(webhook_data)
3338

3439
@staticmethod

chargebee/models/invoice.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,22 @@ class BillingAddress(Model):
5454
"taxes", "line_item_taxes", "linked_payments", "applied_credits", "adjustment_credit_notes", \
5555
"issued_credit_notes", "linked_orders", "notes", "shipping_address", "billing_address", "deleted"]
5656

57+
sub_types = {
58+
'line_items': LineItem,
59+
'discounts': Discount,
60+
'line_item_discounts': LineItemDiscount,
61+
'taxes': Tax,
62+
'line_item_taxes': LineItemTax,
63+
'linked_payments': LinkedPayment,
64+
'applied_credits': AppliedCredit,
65+
'adjustment_credit_notes': AdjustmentCreditNote,
66+
'issued_credit_notes': IssuedCreditNote,
67+
'linked_orders': LinkedOrder,
68+
'notes': Note,
69+
'shipping_address': ShippingAddress,
70+
'billing_address': BillingAddress,
71+
}
72+
5773

5874
@staticmethod
5975
def create(params, env=None, headers=None):

chargebee/models/invoice_estimate.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,10 @@ class LineItemDiscount(Model):
2323
fields = ["recurring", "price_type", "currency_code", "sub_total", "total", "credits_applied", \
2424
"amount_paid", "amount_due", "line_items", "discounts", "taxes", "line_item_taxes", "line_item_discounts"]
2525

26+
sub_types = {
27+
'line_items': LineItem,
28+
'discounts': Discount,
29+
'taxes': Tax,
30+
'line_item_taxes': LineItemTax,
31+
'line_item_discounts': LineItemDiscount,
32+
}

chargebee/models/payment_source.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ class Paypal(Model):
2121
fields = ["id", "customer_id", "type", "reference_id", "status", "gateway", "gateway_account_id", \
2222
"ip_address", "issuing_country", "card", "bank_account", "amazon_payment", "paypal"]
2323

24+
sub_types = {
25+
'card': Card,
26+
'bank_account': BankAccount,
27+
'amazon_payment': AmazonPayment,
28+
'paypal': Paypal,
29+
}
30+
2431

2532
@staticmethod
2633
def create_using_temp_token(params, env=None, headers=None):

chargebee/models/portal_session.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ class LinkedCustomer(Model):
1111
fields = ["id", "token", "access_url", "redirect_url", "status", "created_at", "expires_at", \
1212
"customer_id", "login_at", "logout_at", "login_ipaddress", "logout_ipaddress", "linked_customers"]
1313

14+
sub_types = {
15+
'linked_customers': LinkedCustomer
16+
}
17+
1418

1519
@staticmethod
1620
def create(params, env=None, headers=None):

chargebee/models/subscription.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ class ReferralInfo(Model):
2727
"base_currency_code", "addons", "coupon", "coupons", "shipping_address", "referral_info", "invoice_notes", \
2828
"meta_data", "deleted"]
2929

30+
sub_types = {
31+
'addons': Addon,
32+
'coupons': Coupon,
33+
'shipping_address': ShippingAddress,
34+
'referral_info': ReferralInfo,
35+
}
36+
3037

3138
@staticmethod
3239
def create(params, env=None, headers=None):

0 commit comments

Comments
 (0)