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

Skip to content

Commit e0b8b89

Browse files
committed
Merge branch 'master' of github.com:GetStream/stream-python
2 parents a880300 + ad11e16 commit e0b8b89

File tree

3 files changed

+105
-2
lines changed

3 files changed

+105
-2
lines changed

CHANGELOG

+8
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22
Change history
33
================
44

5+
======
6+
2.10.0
7+
======
8+
:release-date: 2017-07-30
9+
:by: Tommaso Barbugli
10+
11+
Partial activity API endpoint
12+
513
======
614
2.9.3
715
======

stream/client.py

+31
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,37 @@ def get_activities(self, ids=None, foreign_id_times=None):
342342

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

345+
def activity_partial_update(self, id=None, foreign_id=None, time=None, set={}, unset=[]):
346+
'''
347+
Partial update activity, via foreign ID or Foreign ID + timestamp
348+
349+
id: the activity ID
350+
foreign_id: the activity foreign ID
351+
time: the activity time
352+
set: object containing the set operations
353+
unset: list of unset operations
354+
'''
355+
356+
auth_token = self.create_jwt_token('activities', '*', feed_id='*')
357+
358+
if id is None and (foreign_id is None or time is None):
359+
raise TypeError('The id or foreign_id+time parameters must be provided and not be None')
360+
if id is not None and (foreign_id is not None or time is not None):
361+
raise TypeError('Only one of the id or the foreign_id+time parameters can be provided')
362+
363+
data = {
364+
'set': set,
365+
'unset': unset,
366+
}
367+
368+
if id:
369+
data['id'] = id
370+
else:
371+
data['foreign_id'] = foreign_id
372+
data['time'] = time
373+
374+
return self.post('activity/', auth_token, data=data)
375+
345376
def create_redirect_url(self, target_url, user_id, events):
346377
'''
347378
Creates a redirect url for tracking the given events in the context

stream/tests/test_client.py

+66-2
Original file line numberDiff line numberDiff line change
@@ -866,7 +866,7 @@ def test_uniqueness_foreign_id(self):
866866
"verb": "tweet",
867867
"object": 1,
868868
"foreign_id": "tweet:11",
869-
"time": now,
869+
"time": utcnow,
870870
}
871871
self.user1.add_activity(activity_data)
872872

@@ -875,7 +875,7 @@ def test_uniqueness_foreign_id(self):
875875
"verb": "tweet",
876876
"object": 3,
877877
"foreign_id": "tweet:11",
878-
"time": now,
878+
"time": utcnow,
879879
}
880880
self.user1.add_activity(activity_data)
881881

@@ -1125,3 +1125,67 @@ def test_get_activities_full(self):
11251125
response = self.c.get_activities(foreign_id_times=[(fid, dt)])
11261126
self.assertEqual(len(response["results"]), 1)
11271127
self.assertEqual(activity["foreign_id"], response["results"][0]["foreign_id"])
1128+
1129+
def test_activity_partial_update(self):
1130+
now = datetime.datetime.utcnow()
1131+
feed = self.c.feed('user', uuid4())
1132+
feed.add_activity({
1133+
"actor": "barry",
1134+
"object": "09",
1135+
"verb": "tweet",
1136+
"time": now,
1137+
"foreign_id": 'fid:123',
1138+
'product': {
1139+
'name': 'shoes',
1140+
'price': 9.99,
1141+
'color': 'blue'
1142+
}
1143+
})
1144+
activity = feed.get()['results'][0]
1145+
1146+
set = {
1147+
'product.name': 'boots',
1148+
'product.price': 7.99,
1149+
'popularity': 1000,
1150+
'foo': {
1151+
'bar': {
1152+
'baz': 'qux',
1153+
}
1154+
}
1155+
}
1156+
unset = [ 'product.color' ]
1157+
1158+
# partial update by ID
1159+
self.c.activity_partial_update(id=activity['id'], set=set, unset=unset)
1160+
updated = feed.get()['results'][0]
1161+
expected = activity
1162+
expected['product'] = {
1163+
'name': 'boots',
1164+
'price': 7.99
1165+
}
1166+
expected['popularity'] = 1000
1167+
expected['foo'] = {
1168+
'bar': {
1169+
'baz': 'qux'
1170+
}
1171+
}
1172+
self.assertEqual(updated, expected)
1173+
1174+
# partial update by foreign ID + time
1175+
set = {
1176+
'foo.bar.baz': 42,
1177+
'popularity': 9000
1178+
}
1179+
unset = [ 'product.price' ]
1180+
self.c.activity_partial_update(foreign_id=activity['foreign_id'], time=activity['time'], set=set, unset=unset)
1181+
updated = feed.get()['results'][0]
1182+
expected['product'] = {
1183+
'name': 'boots'
1184+
}
1185+
expected['foo'] = {
1186+
'bar': {
1187+
'baz': 42
1188+
}
1189+
}
1190+
expected['popularity'] = 9000
1191+
self.assertEqual(updated, expected)

0 commit comments

Comments
 (0)