Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 62d2852

Browse files
authored
Merge pull request ej2#68 from Tesorio/to-dict
ToDictMixin
2 parents 906bc44 + e276a45 commit 62d2852

File tree

3 files changed

+97
-3
lines changed

3 files changed

+97
-3
lines changed

quickbooks/mixins.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import simplejson as json
2+
import six
23
from .utils import build_where_clause, build_choose_clause
34
from .client import QuickBooks
45
from .exceptions import QuickbooksException
@@ -50,6 +51,42 @@ def from_json(cls, json_data):
5051
return obj
5152

5253

54+
# Based on http://stackoverflow.com/a/1118038
55+
def to_dict(obj, classkey=None):
56+
"""
57+
Recursively converts Python object into a dictionary
58+
"""
59+
if isinstance(obj, dict):
60+
data = {}
61+
for (k, v) in obj.items():
62+
data[k] = to_dict(v, classkey)
63+
return data
64+
elif hasattr(obj, "_ast"):
65+
return to_dict(obj._ast())
66+
elif hasattr(obj, "__iter__") and not isinstance(obj, str):
67+
return [to_dict(v, classkey) for v in obj]
68+
elif hasattr(obj, "__dict__"):
69+
if six.PY2:
70+
data = dict([(key, to_dict(value, classkey))
71+
for key, value in obj.__dict__.iteritems()
72+
if not callable(value) and not key.startswith('_')])
73+
else:
74+
data = dict([(key, to_dict(value, classkey))
75+
for key, value in obj.__dict__.items()
76+
if not callable(value) and not key.startswith('_')])
77+
78+
if classkey is not None and hasattr(obj, "__class__"):
79+
data[classkey] = obj.__class__.__name__
80+
return data
81+
else:
82+
return obj
83+
84+
85+
class ToDictMixin(object):
86+
def to_dict(self):
87+
return to_dict(self)
88+
89+
5390
class ReadMixin(object):
5491
qbo_object_name = ""
5592

quickbooks/objects/base.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
from six import python_2_unicode_compatible
2-
from ..mixins import ToJsonMixin, FromJsonMixin, ReadMixin, ListMixin, UpdateMixin
2+
from ..mixins import ToJsonMixin, FromJsonMixin, ReadMixin, ListMixin, UpdateMixin, ToDictMixin
33

44

5-
class QuickbooksBaseObject(ToJsonMixin, FromJsonMixin):
5+
class QuickbooksBaseObject(ToJsonMixin, FromJsonMixin, ToDictMixin):
66
class_dict = {}
77
list_dict = {}
88
detail_dict = {}
@@ -66,7 +66,7 @@ def __str__(self):
6666

6767

6868
@python_2_unicode_compatible
69-
class PhoneNumber(ToJsonMixin, FromJsonMixin):
69+
class PhoneNumber(ToJsonMixin, FromJsonMixin, ToDictMixin):
7070
def __init__(self):
7171
self.FreeFormNumber = ""
7272

tests/unit/test_mixins.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,63 @@ def test_from_json_missing_detail_object(self):
6262
self.assertEquals(new_obj.TotalAmt, 100)
6363

6464

65+
class ToDictMixinTest(unittest.TestCase):
66+
def test_to_dict(self):
67+
json_data = {
68+
'DocNumber': '123',
69+
'TotalAmt': 100,
70+
'Line': [
71+
{
72+
"Id": "0",
73+
"Description": "Test",
74+
"Amount": 25.54,
75+
"DetailType": "JournalEntryLineDetail",
76+
"JournalEntryLineDetail": {
77+
"PostingType": "Debit",
78+
}
79+
},
80+
],
81+
}
82+
83+
entry = JournalEntry.from_json(json_data)
84+
expected = {
85+
'DocNumber': '123',
86+
'SyncToken': 0,
87+
'domain': 'QBO',
88+
'TxnDate': '',
89+
'TotalAmt': 100,
90+
'ExchangeRate': 1,
91+
'CurrencyRef': None,
92+
'PrivateNote': '',
93+
'sparse': False,
94+
'Line': [{
95+
'LinkedTxn': [],
96+
'Description': 'Test',
97+
'JournalEntryLineDetail': {
98+
'TaxAmount': 0,
99+
'Entity': None,
100+
'DepartmentRef': None,
101+
'TaxCodeRef': None,
102+
'BillableStatus': '',
103+
'TaxApplicableOn': 'Sales',
104+
'PostingType': 'Debit',
105+
'AccountRef': None,
106+
'ClassRef': None,
107+
},
108+
'DetailType': 'JournalEntryLineDetail',
109+
'LineNum': 0,
110+
'Amount': 25.54,
111+
'CustomField': [],
112+
'Id': '0',
113+
}],
114+
'Adjustment': False,
115+
'Id': None,
116+
'TxnTaxDetail': None,
117+
}
118+
119+
self.assertEquals(expected, entry.to_dict())
120+
121+
65122
class ListMixinTest(unittest.TestCase):
66123
def setUp(self):
67124
self.qb_client = client.QuickBooks(

0 commit comments

Comments
 (0)