Repository: ckan/ckan

Issue:
Update events not captured in activity stream when using the dataset grid view in the organization
**CKAN version**
master

**Describe the bug**
Delete events not captured in activity stream when using the dataset grid view

**Steps to reproduce**
1. Login with Admin
2. Go to Organization
3. Click on Manage
4. Go to Datasets Tab
5. Select the multiple Datasets 
6. Delete

<img width="1440" alt="Screenshot 2020-07-01 at 12 04 53 PM" src="https://user-images.githubusercontent.com/25545982/86211021-44f95900-bb93-11ea-9a87-d84c17b17ee0.png">

<img width="1438" alt="Screenshot 2020-07-01 at 1 18 43 PM" src="https://user-images.githubusercontent.com/25545982/86217767-93135a00-bb9d-11ea-9979-3d44fc968d68.png">


**Expected behavior**
There should be logging in activity stream when we use bulk delete of datasets


Update events not captured in activity stream when using the dataset grid view in the organization
**CKAN version**
master

**Describe the bug**
Delete events not captured in activity stream when using the dataset grid view

**Steps to reproduce**
1. Login with Admin
2. Go to Organization
3. Click on Manage
4. Go to Datasets Tab
5. Select the multiple Datasets 
6. Delete

<img width="1440" alt="Screenshot 2020-07-01 at 12 04 53 PM" src="https://user-images.githubusercontent.com/25545982/86211021-44f95900-bb93-11ea-9a87-d84c17b17ee0.png">

<img width="1438" alt="Screenshot 2020-07-01 at 1 18 43 PM" src="https://user-images.githubusercontent.com/25545982/86217767-93135a00-bb9d-11ea-9979-3d44fc968d68.png">


**Expected behavior**
There should be logging in activity stream when we use bulk delete of datasets




Discussion:

Resolving patch:
diff --git a/ckan/logic/action/update.py b/ckan/logic/action/update.py
--- a/ckan/logic/action/update.py
+++ b/ckan/logic/action/update.py
@@ -1202,6 +1202,17 @@ def _bulk_update_dataset(context, data_dict, update_dict):
         .filter(model.Package.owner_org == org_id) \
         .update(update_dict, synchronize_session=False)
 
+    # Handle Activity Stream for Bulk Operations
+    user = context['user']
+    user_obj = model.User.by_name(user)
+    if user_obj:
+        user_id = user_obj.id
+    else:
+        user_id = 'not logged in'
+    for dataset in datasets:
+        entity = model.Package.get(dataset)
+        activity = entity.activity_stream_item('changed', user_id)
+        model.Session.add(activity)
     model.Session.commit()
 
     # solr update here


Test patch:
diff --git a/ckan/tests/logic/action/test_update.py b/ckan/tests/logic/action/test_update.py
--- a/ckan/tests/logic/action/test_update.py
+++ b/ckan/tests/logic/action/test_update.py
@@ -1688,6 +1688,10 @@ def test_bulk_make_public(self):
         )
         for dataset in datasets:
             assert not (dataset.private)
+        activities = helpers.call_action(
+            "organization_activity_list", id=org["id"]
+        )
+        assert activities[0]['activity_type'] == 'changed package'
 
     def test_bulk_delete(self):
 
@@ -1719,6 +1723,11 @@ def test_bulk_delete(self):
         for dataset in datasets:
             assert dataset.state == "deleted"
 
+        activities = helpers.call_action(
+            "organization_activity_list", id=org["id"]
+        )
+        assert activities[0]['activity_type'] == 'deleted package'
+
 
 @pytest.mark.usefixtures("clean_db", "with_request_context")
 class TestDashboardMarkActivitiesOld(object):