Exemplo n.º 1
0
    def _send_newsletter(self, notif_type, when_start, when_end):
        """
        We'll look in the ``Products.Naaya.NySite.getActionLogger`` for object
        creation/modification log entries. Then we'll send notifications for
        the period between `when_start` and `when_end` using the
        `notif_type` template.

        """
        notif_logger.info('Notifications newsletter on site %r, type %r, '
                          'from %s to %s',
                          ofs_path(self.getSite()), notif_type,
                          when_start, when_end)
        objects_by_email = {}
        langs_by_email = {}
        subscriptions_by_email = {}
        anonymous_users = {}
        for log_type, ob in utils.get_modified_objects(self.getSite(),
                                                       when_start, when_end):
            notif_logger.info('.. modified object: %r', ofs_path(ob))
            for subscription in utils.fetch_subscriptions(ob, inherit=True):
                if subscription.notif_type != notif_type:
                    continue
                if not subscription.check_permission(ob):
                    continue
                email = subscription.get_email(ob)
                if email is None:
                    continue
                content_types = getattr(subscription, 'content_types', [])
                if content_types and ob.meta_type not in content_types:
                    continue
                notif_logger.info('.. .. sending newsletter to %r', email)
                objects_by_email.setdefault(email, []).append({
                    'ob': ob,
                    'type': log_type,
                })
                langs_by_email[email] = subscription.lang

                subscriptions_by_email[email] = subscription
                anonymous_users[email] = isinstance(subscription,
                                                    AnonymousSubscription)

        messages_by_email = {}
        for email in objects_by_email:
            messages_by_email[email] = {
                'objs': objects_by_email[email],
                '_lang': langs_by_email[email],
                'subscription': subscriptions_by_email[email],
                'here': self,
                'anonymous': anonymous_users[email]
            }

        template = self._get_template(notif_type)
        self._send_notifications(messages_by_email, template)
Exemplo n.º 2
0
    def _send_newsletter(self, notif_type, when_start, when_end):
        """
        We'll look in the ``Products.Naaya.NySite.getActionLogger`` for object
        creation/modification log entries. Then we'll send notifications for
        the period between `when_start` and `when_end` using the
        `notif_type` template.

        """
        notif_logger.info(
            'Notifications newsletter on site %r, type %r, '
            'from %s to %s', ofs_path(self.getSite()), notif_type, when_start,
            when_end)
        objects_by_email = {}
        langs_by_email = {}
        subscriptions_by_email = {}
        anonymous_users = {}
        for log_type, ob in utils.get_modified_objects(self.getSite(),
                                                       when_start, when_end):
            notif_logger.info('.. modified object: %r', ofs_path(ob))
            for subscription in utils.fetch_subscriptions(ob, inherit=True):
                if subscription.notif_type != notif_type:
                    continue
                if not subscription.check_permission(ob):
                    continue
                email = subscription.get_email(ob)
                if email is None:
                    continue
                content_types = getattr(subscription, 'content_types', [])
                if content_types and ob.meta_type not in content_types:
                    continue
                notif_logger.info('.. .. sending newsletter to %r', email)
                objects_by_email.setdefault(email, []).append({
                    'ob': ob,
                    'type': log_type,
                })
                langs_by_email[email] = subscription.lang

                subscriptions_by_email[email] = subscription
                anonymous_users[email] = isinstance(subscription,
                                                    AnonymousSubscription)

        messages_by_email = {}
        for email in objects_by_email:
            messages_by_email[email] = {
                'objs': objects_by_email[email],
                '_lang': langs_by_email[email],
                'subscription': subscriptions_by_email[email],
                'here': self,
                'anonymous': anonymous_users[email]
            }

        template = self._get_template(notif_type)
        self._send_notifications(messages_by_email, template)
    def test_get_modified_objects(self):
        action_logger = self.portal.getActionLogger()
        items = dict(action_logger.items())
        for entry_id, log_entry in items.items():  # clean action logger
            del action_logger[entry_id]

        # Create a few log entries
        action_logger.create(type=constants.LOG_TYPES['approved'], path='info')
        action_logger.create(type=constants.LOG_TYPES['created'], path='fol1')
        action_logger.create(type=constants.LOG_TYPES['modified'], path='fol2')
        # Same as above. `get_modified_objects` should return 3 object
        action_logger.create(type=constants.LOG_TYPES['modified'], path='fol2')

        modified_obj_list = list(
            get_modified_objects(self.portal,
                                 (datetime.utcnow() - timedelta(minutes=1)),
                                 (datetime.utcnow() + timedelta(minutes=1))))

        self.assertEqual(len(modified_obj_list), 3)
    def test_get_modified_objects(self):
        action_logger = self.portal.getActionLogger()
        items = dict(action_logger.items())
        for entry_id, log_entry in items.items():  # clean action logger
            del action_logger[entry_id]

        # Create a few log entries
        action_logger.create(type=constants.LOG_TYPES['approved'],
                             path='info')
        action_logger.create(type=constants.LOG_TYPES['created'],
                             path='fol1')
        action_logger.create(type=constants.LOG_TYPES['modified'],
                             path='fol2')
        # Same as above. `get_modified_objects` should return 3 object
        action_logger.create(type=constants.LOG_TYPES['modified'],
                             path='fol2')

        modified_obj_list = list(get_modified_objects(self.portal,
                                 (datetime.utcnow() - timedelta(minutes=1)),
                                 (datetime.utcnow() + timedelta(minutes=1))))

        self.assertEqual(len(modified_obj_list), 3)