Пример #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)
        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
Пример #2
0
    def save_watchers(self):
        new_watcher_emails = set(self._watchers)
        old_watcher_emails = set(notifications_services.get_watchers(self.object).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 = notifications_services.get_watchers(self.object)
Пример #3
0
def _push_to_timelines(project,
                       user,
                       obj,
                       event_type,
                       created_datetime,
                       extra_data={}):
    if project is not None:
        # Actions related with a project

        ## Project timeline
        _push_to_timeline(project,
                          obj,
                          event_type,
                          created_datetime,
                          namespace=build_project_namespace(project),
                          extra_data=extra_data)

        ## User profile timelines
        ## - Me
        related_people = User.objects.filter(id=user.id)

        ## - Owner
        if hasattr(obj, "owner_id") and obj.owner_id:
            related_people |= User.objects.filter(id=obj.owner_id)

        ## - Assigned to
        if hasattr(obj, "assigned_to_id") and obj.assigned_to_id:
            related_people |= User.objects.filter(id=obj.assigned_to_id)

        ## - Watchers
        watchers = notifications_services.get_watchers(obj)
        if watchers:
            related_people |= watchers

        ## - Exclude inactive and system users and remove duplicate
        related_people = related_people.exclude(is_active=False)
        related_people = related_people.exclude(is_system=True)
        related_people = related_people.distinct()

        _push_to_timeline(related_people,
                          obj,
                          event_type,
                          created_datetime,
                          namespace=build_user_namespace(user),
                          extra_data=extra_data)
    else:
        # Actions not related with a project
        ## - Me
        _push_to_timeline(user,
                          obj,
                          event_type,
                          created_datetime,
                          namespace=build_user_namespace(user),
                          extra_data=extra_data)
Пример #4
0
    def retrieve(self, request, *args, **kwargs):
        pk = kwargs.get("pk", None)
        resource_id = kwargs.get("resource_id", None)
        resource = get_object_or_404(self.resource_model, pk=resource_id)

        self.check_permissions(request, 'retrieve', resource)

        try:
            self.object = services.get_watchers(resource).get(pk=pk)
        except ObjectDoesNotExist:  # or User.DoesNotExist
            return response.NotFound()

        serializer = self.get_serializer(self.object)
        return response.Ok(serializer.data)
Пример #5
0
    def retrieve(self, request, *args, **kwargs):
        pk = kwargs.get("pk", None)
        resource_id = kwargs.get("resource_id", None)
        resource = get_object_or_404(self.resource_model, pk=resource_id)

        self.check_permissions(request, 'retrieve', resource)

        try:
            self.object = services.get_watchers(resource).get(pk=pk)
        except ObjectDoesNotExist: # or User.DoesNotExist
            return response.NotFound()

        serializer = self.get_serializer(self.object)
        return response.Ok(serializer.data)
Пример #6
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
Пример #7
0
    def get_watchers(self) -> frozenset:
        """
        Default implementation method for obtain a list of
        watchers for current instance.

        NOTE: the default implementation returns frozen
        set of all watchers if "watchers" attribute exists
        in a model.

        WARNING: it returns a full evaluated set and in
        future, for project with 1000k watchers it can be
        very inefficient way for obtain watchers but at
        this momment is the simplest way.
        """
        return frozenset(services.get_watchers(self))
Пример #8
0
    def get_watchers(self) -> frozenset:
        """
        Default implementation method for obtain a list of
        watchers for current instance.

        NOTE: the default implementation returns frozen
        set of all watchers if "watchers" attribute exists
        in a model.

        WARNING: it returns a full evaluated set and in
        future, for project with 1000k watchers it can be
        very inefficient way for obtain watchers but at
        this momment is the simplest way.
        """
        return frozenset(services.get_watchers(self))
Пример #9
0
def _push_to_timelines(project, user, obj, event_type, created_datetime, extra_data={}):
    if project is not None:
        # Actions related with a project

        ## Project timeline
        _push_to_timeline(project, obj, event_type, created_datetime,
            namespace=build_project_namespace(project),
            extra_data=extra_data)

        ## User profile timelines
        ## - Me
        related_people = User.objects.filter(id=user.id)

        ## - Owner
        if hasattr(obj, "owner_id") and obj.owner_id:
            related_people |= User.objects.filter(id=obj.owner_id)

        ## - Assigned to
        if hasattr(obj, "assigned_to_id") and obj.assigned_to_id:
            related_people |= User.objects.filter(id=obj.assigned_to_id)

        ## - Watchers
        watchers = notifications_services.get_watchers(obj)
        if watchers:
            related_people |= watchers

        ## - Exclude inactive and system users and remove duplicate
        related_people = related_people.exclude(is_active=False)
        related_people = related_people.exclude(is_system=True)
        related_people = related_people.distinct()

        _push_to_timeline(related_people, obj, event_type, created_datetime,
            namespace=build_user_namespace(user),
            extra_data=extra_data)
    else:
        # Actions not related with a project
        ## - Me
        _push_to_timeline(user, obj, event_type, created_datetime,
            namespace=build_user_namespace(user),
            extra_data=extra_data)
Пример #10
0
 def get_watchers(self) -> object:
     """
     Default implementation method for obtain a list of
     watchers for current instance.
     """
     return services.get_watchers(self)
Пример #11
0
 def get_watchers(self) -> object:
     """
     Default implementation method for obtain a list of
     watchers for current instance.
     """
     return services.get_watchers(self)
Пример #12
0
 def get_queryset(self):
     resource = self.resource_model.objects.get(
         pk=self.kwargs.get("resource_id"))
     return services.get_watchers(resource)
Пример #13
0
    def to_native(self, obj):
        #watchers is wasn't attached via the get_queryset of the viewset we need to manually add it
        if not hasattr(obj, "watchers"):
            obj.watchers = [user.id for user in services.get_watchers(obj)]

        return super(WatchedResourceModelSerializer, self).to_native(obj)
Пример #14
0
 def to_native(self, obj):
     ret = super(WatcheableObjectModelSerializer, self).to_native(obj)
     ret["watchers"] = [user.email for user in notifications_services.get_watchers(obj)]
     return ret
Пример #15
0
 def get_queryset(self):
     resource = self.resource_model.objects.get(pk=self.kwargs.get("resource_id"))
     return services.get_watchers(resource)
Пример #16
0
    def to_native(self, obj):
        #watchers is wasn't attached via the get_queryset of the viewset we need to manually add it
        if obj is not None and not hasattr(obj, "watchers"):
            obj.watchers = [user.id for user in services.get_watchers(obj)]

        return super(WatchedResourceModelSerializer, self).to_native(obj)