Example #1
0
    def __call__(self):
        related_action = self.element.related_action
        notification_subject = self.element.notification_subject
        notification_action = self.element.notification_action

        obj = self.event.object
        path = "/".join(obj.getPhysicalPath())

        tags = get_tags(obj)

        try:
            url = obj.absolute_url()
        except Exception:
            url = "N/A"

        try:
            content_title = obj.Title()
        except Exception:
            content_title = "N/A"

        try:
            actor = ContentHistoryView(
                obj,
                self.context.REQUEST).fullHistory()[0]['actor']['username']
        except Exception:
            try:
                actor = obj.Creator()
            except Exception:
                actor = "N/A"

        rabbit_config = get_rabbit_config()
        rabbit = RabbitMQConnector(**rabbit_config)
        try:
            rabbit.open_connection()
            rabbit.declare_queue(RABBIT_QUEUE)

            json_notification = {
                'notification_subject': notification_subject,
                'notification_action': notification_action,
                'content_url': url,
                'content_title': content_title,
                'actor': actor,
                'path': path,
                'tags': tags,
                'events': related_action,
            }
            message = json.dumps(json_notification)

            rabbit.send_message(RABBIT_QUEUE, message)
        except Exception:
            LOGGER.error(
                "RabbitMQ connection problem. "
                "Check client configuration: /@@rabbitmq-client-controlpanel."
                " See example in documentation.")
    def __call__(self):
        event = self.event
        queue_name = self.element.queue_name

        obj = event.object

        interpolator = IStringInterpolator(obj)

        body = interpolator(self.element.body.strip())

        if body:
            #send message to the RabbitMQ service
            rabbit = RabbitMQConnector(**rabbit_config)
            rabbit.open_connection()
            try:
                rabbit.declare_queue(queue_name)
                rabbit.send_message(queue_name, body)
            except Exception, err:
                logger.error('Sending \'%s\' in \'%s\' FAILED with error: %s',
                             body, queue_name, err)
            else:
                logger.info('Sending \'%s\' in \'%s\' OK', body, queue_name)
            rabbit.close_connection()
def notifications_center_operations(site):
    """ All the operations of Notifications Center happen here
        Callable by both: browser view and script
    """
    def operations(message, site):
        def get_object(site, path):
            """ Return the object at given path
                or its first existing parent
            """
            obj = get_object_having_path(path)
            if obj is None:
                try:
                    path = "/".join(path.split("/")[:-1])
                    return get_object(site, path)
                except Exception:
                    return None
            else:
                if obj == site:
                    return None
                return obj
            return None

        msg = json.loads(message)
        """ This object is the object related to notification we want to send.
            Sometimes it is missing (example: on deleted items) so a parent
            will be used for that case.

            But the content_url is not changed, basically we just make sure
            the notification is sent.
        """
        obj = get_object(site, msg['path'])

        if obj is not None:
            catalog = get_catalog()
            users = catalog.search_users_by_preferences(
                tags=msg['tags'], events=[msg.get('events', '')], mode="or")
            for user_id in users:
                msg['user_id'] = user_id
                evt = SendEEANotificationEvent(obj, json.dumps(msg))
                notify(evt)
                close(evt)  # make sure it will work for multiple notify(
        else:
            LOGGER.error("Object with path {0} not found.".format(msg['path']))

        return True

    # Consume messages from queue
    rabbit_config = get_rabbit_config()
    rabbit = RabbitMQConnector(**rabbit_config)

    ok_configuration = True
    try:
        LOGGER.info('START consuming from \'%s\'', RABBIT_QUEUE)
        rabbit.open_connection()
        rabbit.declare_queue(RABBIT_QUEUE)
    except AttributeError:
        LOGGER.error("Wrong configuration for RabbitMQ client.")
        ok_configuration = False

    if ok_configuration:
        while True:
            method, properties, body = rabbit.get_message(RABBIT_QUEUE)
            if method is None and properties is None and body is None:
                LOGGER.info('Queue is empty \'%s\'.', RABBIT_QUEUE)
                break

            operations(body, site)
            rabbit.get_channel().basic_ack(delivery_tag=method.delivery_tag)

        rabbit.close_connection()
        LOGGER.info('DONE consuming from \'%s\'', RABBIT_QUEUE)