def restore_object(self, attrs, instance=None): # watchers is not a field from the model but can be attached in the get_queryset of the viewset. # If that's the case we need to remove it before calling the super method self.fields.pop("watchers", None) self.validate_watchers(attrs, "watchers") new_watcher_ids = attrs.pop("watchers", None) obj = super(EditableWatchedResourceSerializer, self).restore_object(attrs, instance) # A partial update can exclude the watchers field or if the new instance can still not be saved if instance is None or new_watcher_ids is None: return obj new_watcher_ids = set(new_watcher_ids) old_watcher_ids = set(obj.get_watchers().values_list("id", flat=True)) adding_watcher_ids = list(new_watcher_ids.difference(old_watcher_ids)) removing_watcher_ids = list( old_watcher_ids.difference(new_watcher_ids)) adding_users = get_user_model().objects.filter( id__in=adding_watcher_ids) removing_users = get_user_model().objects.filter( id__in=removing_watcher_ids) for user in adding_users: services.add_watcher(obj, user) for user in removing_users: services.remove_watcher(obj, user) obj.watchers = obj.get_watchers() return obj
def test_userstory_watchers_retrieve(client, data): add_watcher(data.public_user_story, data.project_owner) public_url = reverse('userstory-watchers-detail', kwargs={"resource_id": data.public_user_story.pk, "pk": data.project_owner.pk}) add_watcher(data.private_user_story1, data.project_owner) private_url1 = reverse('userstory-watchers-detail', kwargs={"resource_id": data.private_user_story1.pk, "pk": data.project_owner.pk}) add_watcher(data.private_user_story2, data.project_owner) private_url2 = reverse('userstory-watchers-detail', kwargs={"resource_id": data.private_user_story2.pk, "pk": data.project_owner.pk}) add_watcher(data.blocked_user_story, data.project_owner) blocked_url = reverse('userstory-watchers-detail', kwargs={"resource_id": data.blocked_user_story.pk, "pk": data.project_owner.pk}) users = [ None, data.registered_user, data.project_member_without_perms, data.project_member_with_perms, data.project_owner ] results = helper_test_http_method(client, 'get', public_url, None, users) assert results == [200, 200, 200, 200, 200] results = helper_test_http_method(client, 'get', private_url1, None, users) assert results == [200, 200, 200, 200, 200] results = helper_test_http_method(client, 'get', private_url2, None, users) assert results == [401, 403, 403, 200, 200] results = helper_test_http_method(client, 'get', blocked_url, None, users) assert results == [401, 403, 403, 200, 200]
def save_watchers(self): new_watcher_emails = set(self._watchers) old_watcher_emails = set(self.object.get_watchers().values_list( "email", flat=True)) adding_watcher_emails = list( new_watcher_emails.difference(old_watcher_emails)) removing_watcher_emails = list( old_watcher_emails.difference(new_watcher_emails)) User = get_user_model() adding_users = User.objects.filter(email__in=adding_watcher_emails) removing_users = User.objects.filter(email__in=removing_watcher_emails) for user in adding_users: notifications_services.add_watcher(self.object, user) for user in removing_users: notifications_services.remove_watcher(self.object, user) self.object.watchers = [ user.email for user in self.object.get_watchers() ]
def watch(self, request, pk=None): obj = self.get_object() self.check_permissions(request, "watch", obj) self.pre_conditions_on_save(obj) services.add_watcher(obj, request.user) return response.Ok()
def add_watcher(self, user): services.add_watcher(self, user)