|
2 | 2 |
|
3 | 3 | import csv
|
4 | 4 | import pprint
|
| 5 | +from decimal import Decimal |
| 6 | +from datetime import datetime, date |
5 | 7 | try: # py3
|
6 | 8 | from io import StringIO
|
7 | 9 | except ImportError: # py2
|
@@ -33,6 +35,18 @@ def __init__(self, session=None, **attributes):
|
33 | 35 | :param attributes: attributes
|
34 | 36 | :return: model.{model}
|
35 | 37 | """
|
| 38 | + for k, v in attributes.items(): |
| 39 | + if k.endswith('_at'): |
| 40 | + try: |
| 41 | + attributes[k] = datetime.strptime(v, '%Y-%m-%dT%H:%M:%S') |
| 42 | + except (ValueError, TypeError): |
| 43 | + pass |
| 44 | + elif k == 'date': |
| 45 | + try: |
| 46 | + attributes[k] = datetime.strptime(v, '%Y-%m-%d') |
| 47 | + except (ValueError, TypeError): |
| 48 | + pass |
| 49 | + |
36 | 50 | super(AtomxModel, self).__setattr__('session', session)
|
37 | 51 | super(AtomxModel, self).__setattr__('_attributes', attributes)
|
38 | 52 | super(AtomxModel, self).__setattr__('_dirty', set()) # list of changed attributes
|
@@ -89,7 +103,19 @@ def _resource_name(self):
|
89 | 103 |
|
90 | 104 | @property
|
91 | 105 | def _dirty_json(self):
|
92 |
| - return {k: self._attributes[k] for k in self._dirty} |
| 106 | + dirty = {} |
| 107 | + for attr in self._dirty: |
| 108 | + val = self._attributes[attr] |
| 109 | + if isinstance(val, datetime) or isinstance(val, date): |
| 110 | + dirty[attr] = val.isoformat() |
| 111 | + elif isinstance(val, Decimal): |
| 112 | + dirty[attr] = float(val) |
| 113 | + elif isinstance(val, set): |
| 114 | + dirty[attr] = list(val) |
| 115 | + else: |
| 116 | + dirty[attr] = val |
| 117 | + |
| 118 | + return dirty |
93 | 119 |
|
94 | 120 | @property
|
95 | 121 | def json(self):
|
|
0 commit comments