コード例 #1
0
ファイル: storage.py プロジェクト: bwiklak/django-monitio
    def add(self, level, message, extra_tags='', subject='', user=None,
            from_user=None, expires=None, close_timeout=None,
            sse=False, email=False, url=None):
        """
        Adds or queues a message to the storage

        :param level: Level of the message
        :param message: Message text to be saved
        :param extra_tags: String with separated tags to add to message Ex: "secret classified"
        :param subject: Subject of the message 
        :param user: `auth.User` that receives the message
        :param from_user: `auth.User` that sends the message
        :param expires: Timestamp that indicates when the message expires
        :param close_timeout: Integer
        :param url: Optional string with URL leading to details of a given message

        .. note:: The message is only saved if it contains something and its level is over the recorded level (`MESSAGE_LEVEL`) `self.level`
        """
        to_user = user or get_user(self.request)
        if not to_user.is_authenticated():
            if Monit(level=level).is_persistent():
                raise NotImplementedError(
                    'Persistent message levels cannot be used for anonymous users.')
            else:
                return super(PersistentMessageStorage, self).add(level, message,
                                                                 extra_tags)
        if not message:
            return

        # Save the message only if its level is over the recorded level, see `MESSAGE_LEVEL` in Django docs
        level = int(level)
        if level < self.level:
            return

        # Add the message
        message = Monit(user=to_user, level=level, message=message,
                        extra_tags=extra_tags, subject=subject,
                        from_user=from_user, expires=expires,
                        close_timeout=close_timeout, url=url)

        # Messages need a primary key when being displayed so that they can be
        # closed/marked as read by the user. Hence, save it now instead of
        # adding it to queue:
        if STORE_WHEN_ADDING:
            message.save()

            if sse:
                # Sent delayed SSE notification
                defer(notify.via_sse, message.pk)

            if email:
                defer(notify.via_email, message.pk)

            return message
        else:
            self.added_new = True
            self._queued_messages.append(message)
コード例 #2
0
    def test__get_tags(self):
        m = Monit(extra_tags="foo", level=INFO)
        self.assertEquals(m._get_tags(), ['foo', 'info', 'unread'])

        m.read = True
        self.assertEquals(m._get_tags(), ['foo', 'info', 'read'])

        m.extra_tags = None
        self.assertEquals(m._get_tags(), ['info', 'read'])

        m.level = None
        self.assertEquals(m._get_tags(), ['read'])
コード例 #3
0
ファイル: storage.py プロジェクト: Foryou1989/django-monitio
    def add(self,
            level,
            message,
            extra_tags='',
            subject='',
            user=None,
            from_user=None,
            expires=None,
            close_timeout=None,
            sse=True,
            email=False):
        """
        Adds or queues a message to the storage

        :param level: Level of the message
        :param message: Message text to be saved
        :param extra_tags: String with separated tags to add to message Ex: "secret classified"
        :param subject: Subject of the message 
        :param user: `auth.User` that receives the message
        :param from_user: `auth.User` that sends the message
        :param expires: Timestamp that indicates when the message expires
        :param close_timeout: Integer

        .. note:: The message is only saved if it contains something and its level is over the recorded level (`MESSAGE_LEVEL`) `self.level`
        """
        to_user = user or get_user(self.request)
        if not to_user.is_authenticated():
            if Monit(level=level).is_persistent():
                raise NotImplementedError(
                    'Persistent message levels cannot be used for anonymous users.'
                )
            else:
                return super(PersistentMessageStorage,
                             self).add(level, message, extra_tags)
        if not message:
            return

        # Save the message only if its level is over the recorded level, see `MESSAGE_LEVEL` in Django docs
        level = int(level)
        if level < self.level:
            return

        # Add the message
        message = Monit(user=to_user,
                        level=level,
                        message=message,
                        extra_tags=extra_tags,
                        subject=subject,
                        from_user=from_user,
                        expires=expires,
                        close_timeout=close_timeout)

        # Messages need a primary key when being displayed so that they can be closed/marked as read by the user.
        # Hence, save it now instead of adding it to queue:
        if STORE_WHEN_ADDING:
            message.save()

            if sse:
                # Sent delayed SSE notification
                defer(notify.via_sse, message.pk)

            if email:
                defer(notify.via_email, message.pk)

            return message
        else:
            print "TAM"
            self.added_new = True
            self._queued_messages.append(message)
コード例 #4
0
    def test___unicode__(self):
        m = Monit(message='foo')
        self.assertEquals(unicode(m), u'foo')

        m = Monit(message='foo', subject='bar')
        self.assertEquals(unicode(m), u'bar: foo')
コード例 #5
0
 def test_save(self):
     m = Monit(subject='foo', message='bar', extra_tags='baz', level=INFO)
     m.save()
コード例 #6
0
 def test__prepare_message(self):
     m = Monit(subject='foo', message='bar', extra_tags='baz')
     m._prepare_message()