Skip to content

Commit

Permalink
Merge branch 'master' of github.com:GetStream/stream-python
Browse files Browse the repository at this point in the history
  • Loading branch information
JelteF committed May 24, 2019
2 parents 44f8062 + 5821227 commit eff2a6e
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 3 deletions.
32 changes: 29 additions & 3 deletions stream/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ def activity_partial_update(
self, id=None, foreign_id=None, time=None, set={}, unset=[]
):
"""
Partial update activity, via foreign ID or Foreign ID + timestamp
Partial update activity, via activity ID or Foreign ID + timestamp
id: the activity ID
foreign_id: the activity foreign ID
Expand All @@ -386,8 +386,6 @@ def activity_partial_update(
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"
Expand All @@ -405,6 +403,34 @@ def activity_partial_update(
data["foreign_id"] = foreign_id
data["time"] = time

return self.activities_partial_update(updates=[data])

def activities_partial_update(self, updates=[]):
"""
Partial update activity, via activity ID or Foreign ID + timestamp
:param updates: list of partial updates to perform.
eg.
[
{
"foreign_id": "post:1",
"time": datetime.datetime.utcnow(),
"set": {
"product.name": "boots",
"product.price": 7.99,
"popularity": 1000,
"foo": {"bar": {"baz": "qux"}},
},
"unset": ["product.color"]
}
]
"""

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

data = {"changes": updates}

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

def create_redirect_url(self, target_url, user_id, events):
Expand Down
77 changes: 77 additions & 0 deletions stream/tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1190,6 +1190,83 @@ def test_activity_partial_update(self):
expected["popularity"] = 9000
self.assertEqual(updated, expected)

def test_activities_partial_update(self):

feed = self.c.feed("user", uuid4())
feed.add_activities(
[
{
"actor": "barry",
"object": "09",
"verb": "tweet",
"time": datetime.datetime.utcnow(),
"foreign_id": "fid:123",
"product": {"name": "shoes", "price": 9.99, "color": "blue"},
},
{
"actor": "jerry",
"object": "10",
"verb": "tweet",
"time": datetime.datetime.utcnow(),
"foreign_id": "fid:456",
"product": {"name": "shoes", "price": 9.99, "color": "blue"},
},
{
"actor": "tommy",
"object": "09",
"verb": "tweet",
"time": datetime.datetime.utcnow(),
"foreign_id": "fid:789",
"product": {"name": "shoes", "price": 9.99, "color": "blue"},
},
]
)
activities = feed.get()["results"]

batch = [
{
"id": activities[0]["id"],
"set": {"product.color": "purple", "custom": {"some": "extra data"}},
"unset": ["product.price"],
},
{
"id": activities[2]["id"],
"set": {"product.price": 9001, "on_sale": True},
},
]

# partial update by ID
self.c.activities_partial_update(batch)
updated = feed.get()["results"]
expected = activities
expected[0]["product"] = {"name": "shoes", "color": "purple"}
expected[0]["custom"] = {"some": "extra data"}
expected[2]["product"] = {"name": "shoes", "price": 9001, "color": "blue"}
expected[2]["on_sale"] = True
self.assertEqual(updated, expected)

# partial update by foreign ID + time
batch = [
{
"foreign_id": activities[1]["foreign_id"],
"time": activities[1]["time"],
"set": {"product.color": "beeeeeeige", "custom": {"modified_by": "me"}},
"unset": ["product.name"],
},
{
"foreign_id": activities[2]["foreign_id"],
"time": activities[2]["time"],
"unset": ["on_sale"],
},
]
self.c.activities_partial_update(batch)
updated = feed.get()["results"]

expected[1]["product"] = {"price": 9.99, "color": "beeeeeeige"}
expected[1]["custom"] = {"modified_by": "me"}
del expected[2]["on_sale"]
self.assertEqual(updated, expected)

def test_create_reference(self):
ref = self.c.collections.create_reference("item", "42")
self.assertEqual(ref, "SO:item:42")
Expand Down

0 comments on commit eff2a6e

Please sign in to comment.