Fixing clearing watchers issue

remotes/origin/logger
Alejandro Alonso 2015-09-23 09:59:06 +02:00
parent b5b14c8e46
commit d5fbe6bafe
2 changed files with 40 additions and 2 deletions

View File

@ -190,13 +190,14 @@ class WatchedResourceModelSerializer(serializers.ModelSerializer):
#If that's the case we need to remove it before calling the super method #If that's the case we need to remove it before calling the super method
watcher_field = self.fields.pop("watchers", None) watcher_field = self.fields.pop("watchers", None)
self.validate_watchers(attrs, "watchers") self.validate_watchers(attrs, "watchers")
new_watcher_ids = set(attrs.pop("watchers", [])) new_watcher_ids = attrs.pop("watchers", None)
obj = super(WatchedResourceModelSerializer, self).restore_object(attrs, instance) obj = super(WatchedResourceModelSerializer, self).restore_object(attrs, instance)
#A partial update can exclude the watchers field or if the new instance can still not be saved #A partial update can exclude the watchers field or if the new instance can still not be saved
if instance is None or len(new_watcher_ids) == 0: if instance is None or new_watcher_ids is None:
return obj return obj
new_watcher_ids = set(new_watcher_ids)
old_watcher_ids = set(obj.get_watchers().values_list("id", flat=True)) old_watcher_ids = set(obj.get_watchers().values_list("id", flat=True))
adding_watcher_ids = list(new_watcher_ids.difference(old_watcher_ids)) adding_watcher_ids = list(new_watcher_ids.difference(old_watcher_ids))
removing_watcher_ids = list(old_watcher_ids.difference(new_watcher_ids)) removing_watcher_ids = list(old_watcher_ids.difference(new_watcher_ids))

View File

@ -477,3 +477,40 @@ def test_update_userstory_respecting_watchers(client):
assert response.status_code == 200 assert response.status_code == 200
assert response.data["subject"] == "Updating test" assert response.data["subject"] == "Updating test"
assert response.data["watchers"] == [watching_user.id] assert response.data["watchers"] == [watching_user.id]
def test_update_userstory_update_watchers(client):
watching_user = f.create_user()
project = f.ProjectFactory.create()
us = f.UserStoryFactory.create(project=project, status__project=project, milestone__project=project)
f.MembershipFactory.create(project=us.project, user=us.owner, is_owner=True)
f.MembershipFactory.create(project=us.project, user=watching_user)
client.login(user=us.owner)
url = reverse("userstories-detail", kwargs={"pk": us.pk})
data = {"watchers": [watching_user.id], "version":1}
response = client.json.patch(url, json.dumps(data))
assert response.status_code == 200
assert response.data["watchers"] == [watching_user.id]
watcher_ids = list(us.get_watchers().values_list("id", flat=True))
assert watcher_ids == [watching_user.id]
def test_update_userstory_remove_watchers(client):
watching_user = f.create_user()
project = f.ProjectFactory.create()
us = f.UserStoryFactory.create(project=project, status__project=project, milestone__project=project)
us.add_watcher(watching_user)
f.MembershipFactory.create(project=us.project, user=us.owner, is_owner=True)
f.MembershipFactory.create(project=us.project, user=watching_user)
client.login(user=us.owner)
url = reverse("userstories-detail", kwargs={"pk": us.pk})
data = {"watchers": [], "version":1}
response = client.json.patch(url, json.dumps(data))
assert response.status_code == 200
assert response.data["watchers"] == []
watcher_ids = list(us.get_watchers().values_list("id", flat=True))
assert watcher_ids == []