Skip to content

Commit e3ce01b

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 d6d4a9d commit e3ce01b

22 files changed

+312
-102
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/addon.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ class Tier(Model):
1515
"shipping_frequency_period_unit", "resource_version", "updated_at", "invoice_notes", "taxable", \
1616
"tax_profile_id", "meta_data", "tiers"]
1717

18+
sub_types = {
19+
'tiers' : Tier,
20+
}
1821

1922
@staticmethod
2023
def create(params, env=None, headers=None):

chargebee/models/credit_note.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,17 @@ class Allocation(Model):
3636
"round_off_amount", "fractional_correction", "line_items", "discounts", "line_item_discounts", \
3737
"line_item_tiers", "taxes", "line_item_taxes", "linked_refunds", "allocations", "deleted"]
3838

39+
sub_types = {
40+
'line_items': LineItem,
41+
'discounts': Discount,
42+
'line_item_discounts': LineItemDiscount,
43+
'line_item_tiers' : LineItemTier,
44+
'taxes': Tax,
45+
'line_item_taxes': LineItemTax,
46+
'linked_refunds': LinkedRefund,
47+
'allocations': Allocation,
48+
}
49+
3950

4051
@staticmethod
4152
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
@@ -35,6 +35,14 @@ class Relationship(Model):
3535
"registered_for_gst", "business_customer_without_vat_number", "customer_type", "client_profile_id", \
3636
"relationship"]
3737

38+
sub_types = {
39+
'billing_address': BillingAddress,
40+
'referral_urls': ReferralUrl,
41+
'contacts': Contact,
42+
'payment_method': PaymentMethod,
43+
'balances': Balance,
44+
'relationship': Relationship,
45+
}
3846

3947
@staticmethod
4048
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/export.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ class Download(Model):
99
pass
1010

1111
fields = ["id", "operation_type", "mime_type", "status", "created_at", "download"]
12+
13+
sub_types = {
14+
'download': Download,
15+
}
16+
1217
def wait_for_export_completion(self):
1318
return wait_for_export_completion()
1419

chargebee/models/gift.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ class GiftTimeline(Model):
1717
fields = ["id", "status", "scheduled_at", "auto_claim", "no_expiry", "claim_expiry_date", \
1818
"resource_version", "updated_at", "gifter", "gift_receiver", "gift_timelines"]
1919

20+
sub_types = {
21+
'gifter': Gifter,
22+
'gift_receiver': GiftReceiver,
23+
'gift_timelines': GiftTimeline,
24+
}
2025

2126
@staticmethod
2227
def create(params, env=None, headers=None):

chargebee/models/invoice.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,24 @@ class BillingAddress(Model):
6262
"linked_payments", "dunning_attempts", "applied_credits", "adjustment_credit_notes", "issued_credit_notes", \
6363
"linked_orders", "notes", "shipping_address", "billing_address", "payment_owner", "deleted"]
6464

65+
sub_types = {
66+
'line_items': LineItem,
67+
'discounts': Discount,
68+
'line_item_discounts': LineItemDiscount,
69+
'taxes': Tax,
70+
'line_item_taxes': LineItemTax,
71+
'line_item_tiers': LineItemTier,
72+
'linked_payments': LinkedPayment,
73+
'dunning_attempts': DunningAttempt,
74+
'applied_credits': AppliedCredit,
75+
'adjustment_credit_notes': AdjustmentCreditNote,
76+
'issued_credit_notes': IssuedCreditNote,
77+
'linked_orders': LinkedOrder,
78+
'notes': Note,
79+
'shipping_address': ShippingAddress,
80+
'billing_address': BillingAddress,
81+
}
82+
6583

6684
@staticmethod
6785
def create(params, env=None, headers=None):

chargebee/models/invoice_estimate.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,11 @@ class LineItemDiscount(Model):
2727
"amount_paid", "amount_due", "line_items", "discounts", "taxes", "line_item_taxes", "line_item_tiers", \
2828
"line_item_discounts", "round_off_amount", "customer_id"]
2929

30+
sub_types = {
31+
'line_items': LineItem,
32+
'discounts': Discount,
33+
'taxes': Tax,
34+
'line_item_taxes': LineItemTax,
35+
'line_item_tiers' : LineItemTier,
36+
'line_item_discounts': LineItemDiscount,
37+
}

0 commit comments

Comments
 (0)