Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions stream/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,37 @@ def get_activities(self, ids=None, foreign_id_times=None):

return self.get('activities/', auth_token, params=query_params)

def activity_partial_update(self, id=None, foreign_id=None, time=None, set={}, unset=[]):
'''
Partial update activity, via foreign ID or Foreign ID + timestamp

id: the activity ID
foreign_id: the activity foreign ID
time: the activity time
set: object containing the set operations
unset: list of unset operations
'''

auth_token = self.create_jwt_token('activities', '*', feed_id='*')

if id is None and (foreign_id is None or time is None):
raise TypeError('The id or foreign_id+time parameters must be provided and not be None')
if id is not None and (foreign_id is not None or time is not None):
raise TypeError('Only one of the id or the foreign_id+time parameters can be provided')

data = {
'set': set,
'unset': unset,
}

if id:
data['id'] = id
else:
data['foreign_id'] = foreign_id
data['time'] = time

return self.post('activity/', auth_token, data=data)

def create_redirect_url(self, target_url, user_id, events):
'''
Creates a redirect url for tracking the given events in the context
Expand Down
68 changes: 66 additions & 2 deletions stream/tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -866,7 +866,7 @@ def test_uniqueness_foreign_id(self):
"verb": "tweet",
"object": 1,
"foreign_id": "tweet:11",
"time": now,
"time": utcnow,
}
self.user1.add_activity(activity_data)

Expand All @@ -875,7 +875,7 @@ def test_uniqueness_foreign_id(self):
"verb": "tweet",
"object": 3,
"foreign_id": "tweet:11",
"time": now,
"time": utcnow,
}
self.user1.add_activity(activity_data)

Expand Down Expand Up @@ -1125,3 +1125,67 @@ def test_get_activities_full(self):
response = self.c.get_activities(foreign_id_times=[(fid, dt)])
self.assertEqual(len(response["results"]), 1)
self.assertEqual(activity["foreign_id"], response["results"][0]["foreign_id"])

def test_activity_partial_update(self):
now = datetime.datetime.utcnow()
feed = self.c.feed('user', uuid4())
feed.add_activity({
"actor": "barry",
"object": "09",
"verb": "tweet",
"time": now,
"foreign_id": 'fid:123',
'product': {
'name': 'shoes',
'price': 9.99,
'color': 'blue'
}
})
activity = feed.get()['results'][0]

set = {
'product.name': 'boots',
'product.price': 7.99,
'popularity': 1000,
'foo': {
'bar': {
'baz': 'qux',
}
}
}
unset = [ 'product.color' ]

# partial update by ID
self.c.activity_partial_update(id=activity['id'], set=set, unset=unset)
updated = feed.get()['results'][0]
expected = activity
expected['product'] = {
'name': 'boots',
'price': 7.99
}
expected['popularity'] = 1000
expected['foo'] = {
'bar': {
'baz': 'qux'
}
}
self.assertEqual(updated, expected)

# partial update by foreign ID + time
set = {
'foo.bar.baz': 42,
'popularity': 9000
}
unset = [ 'product.price' ]
self.c.activity_partial_update(foreign_id=activity['foreign_id'], time=activity['time'], set=set, unset=unset)
updated = feed.get()['results'][0]
expected['product'] = {
'name': 'boots'
}
expected['foo'] = {
'bar': {
'baz': 42
}
}
expected['popularity'] = 9000
self.assertEqual(updated, expected)