Beispiel #1
0
    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
        watcher_field = self.fields.pop("watchers", None)
        self.validate_watchers(attrs, "watchers")
        new_watcher_ids = set(attrs.pop("watchers", []))
        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
        if instance is None or len(new_watcher_ids) == 0:
            return obj

        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))

        User = apps.get_model("users", "User")
        adding_users = User.objects.filter(id__in=adding_watcher_ids)
        removing_users = User.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
Beispiel #2
0
    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
Beispiel #3
0
    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
        watcher_field = self.fields.pop("watchers", None)
        instance = super(WatchedResourceModelSerializer,
                         self).restore_object(attrs, instance)
        if instance is not None and self.validate_watchers(attrs, "watchers"):
            new_watcher_ids = set(attrs.get("watchers", []))
            old_watcher_ids = set(
                services.get_watchers(instance).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))

            User = apps.get_model("users", "User")
            adding_users = User.objects.filter(id__in=adding_watcher_ids)
            removing_users = User.objects.filter(id__in=removing_watcher_ids)
            for user in adding_users:
                services.add_watcher(instance, user)

            for user in removing_users:
                services.remove_watcher(instance, user)

            instance.watchers = services.get_watchers(instance)

        return instance
Beispiel #4
0
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 test_wikipage_watchers_retrieve(client, data):
    add_watcher(data.public_wiki_page, data.project_owner)
    public_url = reverse(
        "wiki-watchers-detail", kwargs={"resource_id": data.public_wiki_page.pk, "pk": data.project_owner.pk}
    )
    add_watcher(data.private_wiki_page1, data.project_owner)
    private_url1 = reverse(
        "wiki-watchers-detail", kwargs={"resource_id": data.private_wiki_page1.pk, "pk": data.project_owner.pk}
    )
    add_watcher(data.private_wiki_page2, data.project_owner)
    private_url2 = reverse(
        "wiki-watchers-detail", kwargs={"resource_id": data.private_wiki_page2.pk, "pk": data.project_owner.pk}
    )
    add_watcher(data.blocked_wiki_page, data.project_owner)
    blocked_url = reverse(
        "wiki-watchers-detail", kwargs={"resource_id": data.blocked_wiki_page.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 = apps.get_model("users", "User")
        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()]
Beispiel #7
0
    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 = apps.get_model("users", "User")
        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()]
Beispiel #8
0
    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
        watcher_field = self.fields.pop("watchers", None)
        instance = super(WatchedResourceModelSerializer, self).restore_object(attrs, instance)
        if instance is not None and self.validate_watchers(attrs, "watchers"):
            new_watcher_ids = set(attrs.get("watchers", []))
            old_watcher_ids = set(services.get_watchers(instance).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))

            User = apps.get_model("users", "User")
            adding_users = User.objects.filter(id__in=adding_watcher_ids)
            removing_users = User.objects.filter(id__in=removing_watcher_ids)
            for user in adding_users:
                services.add_watcher(instance, user)

            for user in removing_users:
                services.remove_watcher(instance, user)

            instance.watchers = services.get_watchers(instance)

        return instance
Beispiel #9
0
 def watch(self, request, pk=None):
     obj = self.get_object()
     self.check_permissions(request, "watch", obj)
     services.add_watcher(obj, request.user)
     return response.Ok()
Beispiel #10
0
 def add_watcher(self, user):
     services.add_watcher(self, user)
Beispiel #11
0
 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()
Beispiel #12
0
 def add_watcher(self, user):
     services.add_watcher(self, user)