Skip to content

Commit

Permalink
Handle assignment notification based on user preferences [SDBELGA-818] (
Browse files Browse the repository at this point in the history
superdesk#2018)

* Handle assignment notification based on user preferences [SDBELGA-818]

* address comment

* refactore code

* check assigned user

* Refactor: Define user_id constant

* fix pytest

* add user default prefrence

* fix behave tests
  • Loading branch information
devketanpro authored Jul 10, 2024
1 parent 3eb114c commit 4a8dbf9
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 11 deletions.
4 changes: 4 additions & 0 deletions server/features/assignments.feature
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ Feature: Assignments
"""
[{"name": "Sports", "content_expiry": 60, "members": [{"user": "#CONTEXT_USER_ID#"}]}]
"""
And "users"
"""
[{"_id": "507f191e810c19729de87034", "name":"testfoo", "email":"[email protected]", "username":"johnfoo"}]
"""

@auth
Scenario: Empty planning list
Expand Down
4 changes: 4 additions & 0 deletions server/features/planning.feature
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,10 @@ Feature: Planning
"""
[{"_id": "desk_123", "name": "Politic Desk"}]
"""
Given "users"
"""
[{"_id": "507f191e810c19729de871eb", "name":"testfoo", "email":"[email protected]", "username":"johnfoo"}]
"""
Given "vocabularies"
"""
[{
Expand Down
11 changes: 11 additions & 0 deletions server/planning/assignments/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,14 @@ def init_app(app):
label=lazy_gettext("Default sort preferences for Assignment lists"),
category=lazy_gettext("Assignments"),
)

superdesk.register_default_user_preference(
"assignment:notification",
{
"type": "bool",
"enabled": True,
"default": True,
},
label=lazy_gettext("Send Assignment notifications via email"),
category=lazy_gettext("notifications"),
)
7 changes: 7 additions & 0 deletions server/planning/assignments/assignments.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,13 @@ def send_assignment_notification(self, updates, original=None, force=False):
return

assigned_to = updates.get("assigned_to", {})

# No assignment notification sent, if user is not enabled assignment notification
if assigned_to.get("user") and not superdesk.get_resource_service(
"preferences"
).assignment_notification_is_enabled(user_id=assigned_to.get("user")):
return

assignment_id = updates.get("_id") or assigned_to.get("assignment_id", "Unknown")
if not original:
original = {}
Expand Down
24 changes: 23 additions & 1 deletion server/planning/assignments/assignments_link_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@


assignment_id = "5b20652a1d41c812e24aa49e"
USER_ID = ObjectId("5d385f31fe985ec67a0ca583")


class AssignmentLinkTestCase(TestCase):
Expand Down Expand Up @@ -81,12 +82,33 @@ def test_updates_creates_new_record(self):
"coverage_item": "cov1",
"assigned_to": {
"state": "assigned",
"user": "test",
"user": USER_ID,
"desk": "test",
},
}
],
)
self.app.data.insert(
"users",
[
{
"_id": USER_ID,
"username": "admin",
"password": "blabla",
"email": "[email protected]",
"user_type": "administrator",
"is_active": True,
"needs_activation": False,
"is_author": True,
"is_enabled": True,
"display_name": "John Smith",
"sign_off": "ADM",
"first_name": "John",
"last_name": "Smith",
"role": ObjectId("5d542206c04280bc6d6157f9"),
}
],
)

get_resource_service("assignments_link").post(
[{"assignment_id": assignment_id, "item_id": "item1", "reassign": True}]
Expand Down
39 changes: 32 additions & 7 deletions server/planning/assignments/assignments_unlink_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,34 @@


class AssignmentUnlinkTestCase(TestCase):
USER_ID = ObjectId("5d385f31fe985ec67a0ca583")

def setUp(self):
super().setUp()
with self.app.app_context():
users = [
{
"_id": self.USER_ID,
"username": "admin",
"password": "blabla",
"email": "[email protected]",
"user_type": "administrator",
"is_active": True,
"needs_activation": False,
"is_author": True,
"is_enabled": True,
"display_name": "John Smith",
"sign_off": "ADM",
"first_name": "John",
"last_name": "Smith",
"role": ObjectId("5d542206c04280bc6d6157f9"),
}
]
self.app.data.insert("users", users)

def test_delivery_record(self):
with self.app.app_context():
flask.g.user = {"_id": ObjectId()}
flask.g.user = {"_id": self.USER_ID}
self.app.data.insert(
"vocabularies",
[
Expand Down Expand Up @@ -105,8 +130,8 @@ def test_delivery_record(self):
def test_unlinks_all_content_updates(self):
with self.app.app_context():
self.app.config.update({"PLANNING_LINK_UPDATES_TO_COVERAGES": True})
flask.g.user = {"_id": ObjectId()}
user_id = ObjectId()
flask.g.user = {"_id": self.USER_ID}
user_id = self.USER_ID
desk_id = ObjectId()

# Make sure users a members of the desks
Expand Down Expand Up @@ -218,8 +243,8 @@ def test_unlinks_all_content_updates(self):
def test_unlinks_properly_on_unlinking_any_update_in_chain(self):
with self.app.app_context():
self.app.config.update({"PLANNING_LINK_UPDATES_TO_COVERAGES": True})
flask.g.user = {"_id": ObjectId()}
user_id = ObjectId()
flask.g.user = {"_id": self.USER_ID}
user_id = self.USER_ID
desk_id = ObjectId()

# Make sure users a members of the desks
Expand Down Expand Up @@ -336,8 +361,8 @@ def test_unlinks_properly_on_unlinking_any_update_in_chain(self):
def test_unlinks_archived_content(self):
with self.app.app_context():
self.app.config.update({"PLANNING_LINK_UPDATES_TO_COVERAGES": True})
flask.g.user = {"_id": ObjectId()}
user_id = ObjectId()
flask.g.user = {"_id": self.USER_ID}
user_id = self.USER_ID
desk_id = ObjectId()
self.app.data.insert(
"vocabularies",
Expand Down
30 changes: 27 additions & 3 deletions server/planning/planning/planning_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
from planning.tests import TestCase
from superdesk import get_resource_service
from superdesk.errors import SuperdeskApiError
from bson import ObjectId

USER_ID = ObjectId("5d385f31fe985ec67a0ca583")


class DuplicateCoverageTestCase(TestCase):
Expand All @@ -28,7 +31,7 @@ def setUp(self):
},
"news_coverage_status": {"qcode": "ncostat:int"},
"assigned_to": {
"user": "59f7f0881d41c88cab3f2a99",
"user": USER_ID,
"desk": "desk1",
"state": "in_progress",
},
Expand All @@ -37,6 +40,27 @@ def setUp(self):
}
],
)
self.app.data.insert(
"users",
[
{
"_id": USER_ID,
"username": "admin",
"password": "blabla",
"email": "[email protected]",
"user_type": "administrator",
"is_active": True,
"needs_activation": False,
"is_author": True,
"is_enabled": True,
"display_name": "John Smith",
"sign_off": "ADM",
"first_name": "John",
"last_name": "Smith",
"role": ObjectId("5d542206c04280bc6d6157f9"),
}
],
)

def test_duplicate(self):
with self.app.app_context():
Expand All @@ -49,7 +73,7 @@ def test_duplicate(self):
"scheduled": datetime(2029, 10, 13, 15, 00, tzinfo=pytz.UTC),
},
"assigned_to": {
"user": "562435231d41c835d7b5fb55",
"user": USER_ID,
"desk": "desk2",
"state": "in_progress",
},
Expand All @@ -63,7 +87,7 @@ def test_duplicate(self):

self.assertEqual(new_coverage["planning"]["slugline"], "new slugline")
self.assertEqual(new_coverage["planning"]["scheduled"], datetime(2029, 10, 13, 15, 00, tzinfo=pytz.UTC))
self.assertEqual(new_coverage["assigned_to"]["user"], "562435231d41c835d7b5fb55")
self.assertEqual(new_coverage["assigned_to"]["user"], USER_ID)
self.assertEqual(new_coverage["assigned_to"]["desk"], "desk2")
self.assertEqual(new_coverage["assigned_to"]["state"], "in_progress")
self.assertEqual(new_coverage["news_coverage_status"], {"qcode": "ncostat:onreq"})
Expand Down

0 comments on commit 4a8dbf9

Please sign in to comment.