def push_count_update(user):
    """
    Pushes a count update to all of the clients machines
    """
    if PushService:
        count = UserNotification.objects.filter(user = user, read = False).count()
        PushService.send('notification.count', users = user, data = {'count' : count})
Exemple #2
0
def push_count_update(user):
    """
    Pushes a count update to all of the clients machines
    """
    if PushService:
        count = UserNotification.objects.filter(user=user, read=False).count()
        PushService.send('notification.count',
                         users=user,
                         data={'count': count})
Exemple #3
0
    def send(self):
        """
        Send and or save the actual notification
        """
        request = get_request()
        self.tags = '%s %s' % (self.tags, self.type)

        # if we still dont have any users, set it to the user in the request.
        # We do it here and not in model init to avoid redundant calls to get_request
        # when we might be saving the model directly
        if not len(self.users):
            self.users = [request.user]

        # if the store option is 1 or 2, we will store the notification
        if self.store == 1 or self.store == 2:

            # start by saving the actual notification
            notification = NotificationStore(message=self.message,
                                             target=self.get_target(),
                                             type=self.type,
                                             sender=self.sender,
                                             data=self.data).save()

            # loop through and create following links for users
            followers_to_save = []
            for user in self.users:

                # catch if is is an unauthenticated user, we wont save
                if user == request.user and not request.user.is_authenticated(
                ):
                    continue
                else:
                    followers_to_save.append(
                        UserNotification(user=user, notification=notification))

            # bulk save the user links
            UserNotification.objects.bulk_create(followers_to_save)

            # update the users notification counts
            for user in self.users:
                push_count_update(user)

        # here we check to see that the notification should actually be sent, which is any storage level except 2
        if self.store != 2:

            for user in self.users:

                # push our notification is push is set to true and we have a push service
                if self.push and PushService:

                    # here we have a user making a request that is not ajax and the push notification is supposed to go to them
                    # it will never make it there because of the page request, so just catch it and send it through normally
                    if not request.is_ajax() and user == request.user:
                        messages.add_message(request,
                                             messages.INFO,
                                             self,
                                             extra_tags=self.tags)
                    else:

                        # we will send through our arbitrary data with the message
                        push_data = {
                            'message': self.message,
                            'tags': self.tags
                        }
                        if self.data: push_data.update(self.data)

                        PushService.send(event='notification.new-notification',
                                         data=push_data,
                                         users=user)

                else:
                    if user == request.user:
                        messages.add_message(request,
                                             messages.INFO,
                                             self,
                                             extra_tags=self.tags)
    def send(self):
        """
        Send and or save the actual notification
        """ 
        request = get_request()
        self.tags = '%s %s' % (self.tags, self.type)

        # if we still dont have any users, set it to the user in the request.
        # We do it here and not in model init to avoid redundant calls to get_request
        # when we might be saving the model directly
        if not len(self.users):
            self.users = [request.user]

        # if the store option is 1 or 2, we will store the notification
        if self.store == 1 or self.store == 2:
            
            # start by saving the actual notification
            notification = NotificationStore(
                message = self.message,
                target = self.get_target(),
                type = self.type,
                sender = self.sender,
                data = self.data
                ).save()

            # loop through and create following links for users
            followers_to_save = []
            for user in self.users:

                # catch if is is an unauthenticated user, we wont save
                if user == request.user and not request.user.is_authenticated():
                    continue
                else:
                    followers_to_save.append(UserNotification(user = user, notification = notification))

            # bulk save the user links
            UserNotification.objects.bulk_create(followers_to_save)

            # update the users notification counts
            for user in self.users:
                push_count_update(user)


        # here we check to see that the notification should actually be sent, which is any storage level except 2
        if self.store != 2:

            for user in self.users:

                # push our notification is push is set to true and we have a push service
                if self.push and PushService:

                    # here we have a user making a request that is not ajax and the push notification is supposed to go to them
                    # it will never make it there because of the page request, so just catch it and send it through normally
                    if not request.is_ajax() and user == request.user:
                        messages.add_message(request, messages.INFO, self, extra_tags = self.tags)
                    else:
                        
                        # we will send through our arbitrary data with the message
                        push_data = {'message' : self.message, 'tags' : self.tags}
                        if self.data: push_data.update(self.data)

                        PushService.send(
                            event = 'notification.new-notification', 
                            data = push_data,
                            users = user
                            )

                else:
                    if user == request.user:
                        messages.add_message(request, messages.INFO, self, extra_tags = self.tags)