Skip to content

Commit 6f278d1

Browse files
committed
Adding changed_fields support. (intercom#40)
* adding identity_vars to User and Company * adding submittable_attribute method to Resource * changing to_dict support
1 parent cd3b580 commit 6f278d1

File tree

5 files changed

+26
-7
lines changed

5 files changed

+26
-7
lines changed

intercom/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ def endpoint(cls, value):
187187

188188
class ResourceEncoder(JSONEncoder):
189189
def default(self, o):
190-
if hasattr(o, 'from_api'):
190+
if hasattr(o, 'attributes'):
191191
# handle API resources
192192
return o.attributes
193193
return super(ResourceEncoder, self).default(o)

intercom/api_operations/save.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ def from_dict(self, pdict):
1717
@property
1818
def to_dict(self):
1919
a_dict = {}
20-
for name in self.attributes.keys():
20+
for name in self.__dict__.keys():
21+
if name == "changed_attributes":
22+
continue
2123
a_dict[name] = self.__dict__[name] # direct access
2224
return a_dict
2325

@@ -33,12 +35,13 @@ def from_response(self, response):
3335

3436
def save(self):
3537
collection = utils.resource_class_to_collection_name(self.__class__)
36-
params = self.__dict__
38+
params = self.attributes
3739
if self.id_present and not self.posted_updates:
3840
# update
3941
response = Intercom.put('/%s/%s' % (collection, self.id), **params)
4042
else:
4143
# create
44+
params.update(self.identity_hash)
4245
response = Intercom.post('/%s' % (collection), **params)
4346
if response:
4447
return self.from_response(response)
@@ -53,6 +56,8 @@ def posted_updates(self):
5356

5457
@property
5558
def identity_hash(self):
56-
identity_vars = getattr(self, 'identity_vars', None)
57-
if identity_vars:
58-
return {}
59+
identity_vars = getattr(self, 'identity_vars', [])
60+
parts = {}
61+
for var in identity_vars:
62+
parts[var] = getattr(self, var, None)
63+
return parts

intercom/company.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@
66

77
class Company(Resource, Count, Find, Load):
88
update_verb = 'post'
9+
identity_vars = ['id', 'company_id']

intercom/traits/api_resource.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,15 @@ def to_datetime_value(value):
3333
class Resource(object):
3434

3535
def __init__(_self, **params):
36+
_self.changed_attributes = []
3637
_self.from_dict(params)
3738
self = _self
3839

3940
if hasattr(_self, 'flat_store_attributes'):
4041
for attr in self.flat_store_attributes:
4142
if not hasattr(self, attr):
4243
setattr(self, attr, FlatStore())
44+
_self.changed_attributes = []
4345

4446
def _flat_store_attribute(self, attribute):
4547
if hasattr(self, 'flat_store_attributes'):
@@ -54,6 +56,7 @@ def from_api(cls, response):
5456

5557
def from_response(self, response):
5658
self.from_dict(response)
59+
self.changed_attributes = []
5760
return self
5861

5962
def from_dict(self, dict):
@@ -64,7 +67,14 @@ def from_dict(self, dict):
6467

6568
@property
6669
def attributes(self):
67-
return self.__dict__
70+
res = {}
71+
for name, value in self.__dict__.items():
72+
if self.submittable_attribute(name, value):
73+
res[name] = value
74+
return res
75+
76+
def submittable_attribute(self, name, value):
77+
return name in self.changed_attributes or isinstance(value, FlatStore)
6878

6979
def __getattribute__(self, attribute):
7080
value = super(Resource, self).__getattribute__(attribute)
@@ -82,4 +92,6 @@ def __setattr__(self, attribute, value):
8292
value_to_set = time.mktime(value.timetuple())
8393
else:
8494
value_to_set = value
95+
if attribute != 'changed_attributes':
96+
self.__dict__['changed_attributes'].append(attribute)
8597
super(Resource, self).__setattr__(attribute, value_to_set)

intercom/user.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class User(Resource, Find, FindAll, All, Count, Load, Save, Delete,
1313
IncrementableAttributes):
1414

1515
update_verb = 'post'
16+
identity_vars = ['email', 'user_id']
1617

1718
@property
1819
def flat_store_attributes(self):

0 commit comments

Comments
 (0)