示例#1
0
    def test_get_subscribers(self, item, item_name, namespace, tag_name):
        users = get_subscribers(**item.meta)
        assert users == set()

        name1 = u'baz'
        password = u'password'
        email1 = u'*****@*****.**'
        name2 = u"bar"
        email2 = u"*****@*****.**"
        name3 = u"barbaz"
        email3 = u"*****@*****.**"
        user.create_user(username=name1,
                         password=password,
                         email=email1,
                         validate=False,
                         locale=u'en')
        user1 = user.User(name=name1, password=password)
        user.create_user(username=name2,
                         password=password,
                         email=email2,
                         validate=False)
        user2 = user.User(name=name2, password=password)
        user.create_user(username=name3,
                         password=password,
                         email=email3,
                         verify_email=True,
                         locale=u"en")
        user3 = user.User(name=name3, password=password, email=email3)
        subscribers = get_subscribers(**item.meta)
        assert subscribers == set()

        namere = r'.*'
        nameprefix = u"fo"
        subscription_lists = [
            ["{0}:{1}".format(ITEMID, item.meta[ITEMID])],
            ["{0}:{1}:{2}".format(TAGS, namespace, tag_name)],
            ["{0}:{1}:{2}".format(NAME, namespace, item_name)],
            ["{0}:{1}:{2}".format(NAMERE, namespace, namere)],
            ["{0}:{1}:{2}".format(NAMEPREFIX, namespace, nameprefix)],
        ]
        users = [user1, user2, user3]
        expected_names = {user1.name0, user2.name0}
        for subscriptions in subscription_lists:
            for user_ in users:
                user_.profile._meta[SUBSCRIPTIONS] = subscriptions
                user_.save(force=True)
            subscribers = get_subscribers(**item.meta)
            subscribers_names = {subscriber.name for subscriber in subscribers}
            assert subscribers_names == expected_names

        meta = {
            CONTENTTYPE: u'text/plain;charset=utf-8',
            ACL: u"{0}: All:read,write".format(user1.name0)
        }
        item._save(meta, comment=u"")
        item = Item.create(item_name)
        subscribers = get_subscribers(**item.meta)
        assert {subscriber.name for subscriber in subscribers} == {user2.name0}
示例#2
0
def send_notifications(app, item_name, **kwargs):
    """ Send mail notifications to subscribers on item change

    :param app: local proxy app
    :param item_name: name of the changed item
    :param kwargs: key/value pairs that contain extra information about the item
                   required in order to create a notification
    """
    action = kwargs.get('action')
    revs = get_item_last_revisions(app, item_name) if action not in [
        DESTROY_REV, DESTROY_ALL, ] else []
    notification = Notification(app, item_name, revs, **kwargs)
    content_diff = notification.get_content_diff()
    meta_diff = notification.get_meta_diff()

    u = flaskg.user
    meta = kwargs.get('meta') if action in [DESTROY_REV, DESTROY_ALL, ] else revs[0].meta._meta
    subscribers = {subscriber for subscriber in get_subscribers(**meta) if
                   subscriber.itemid != u.itemid}
    subscribers_locale = {subscriber.locale for subscriber in subscribers}
    for locale in subscribers_locale:
        with force_locale(locale):
            txt_msg, html_msg = notification.render_templates(content_diff, meta_diff)
            subject = L_('[%(moin_name)s] Update of "%(item_name)s" by %(user_name)s',
                         moin_name=app.cfg.interwikiname, item_name=item_name, user_name=u.name0)
            subscribers_emails = [subscriber.email for subscriber in subscribers
                                  if subscriber.locale == locale]
            sendmail(subject, txt_msg, to=subscribers_emails, html=html_msg)
示例#3
0
def send_notifications(app, fqname, **kwargs):
    """ Send mail notifications to subscribers on item change

    :param app: local proxy app
    :param fqname: fqname of the changed item
    :param kwargs: key/value pairs that contain extra information about the item
                   required in order to create a notification
    """
    action = kwargs.get('action')
    revs = get_item_last_revisions(app, fqname) if action not in [
        DESTROY_REV, DESTROY_ALL, ] else []
    notification = Notification(app, fqname, revs, **kwargs)
    try:
        content_diff = notification.get_content_diff()
    except Exception:
        # current user has likely corrupted an item or fixed a corrupted item
        # if current item is corrupt, another exception will occur in a downstream script
        content_diff = [u'- ' + _('An error has occurred, the current or prior revision of this item may be corrupt.')]
    meta_diff = notification.get_meta_diff()

    u = flaskg.user
    meta = kwargs.get('meta') if action in [DESTROY_REV, DESTROY_ALL, ] else revs[0].meta._meta
    subscribers = {subscriber for subscriber in get_subscribers(**meta) if
                   subscriber.itemid != u.itemid}
    subscribers_locale = {subscriber.locale for subscriber in subscribers}
    for locale in subscribers_locale:
        with force_locale(locale):
            txt_msg, html_msg = notification.render_templates(content_diff, meta_diff)
            subject = L_('[%(moin_name)s] Update of "%(fqname)s" by %(user_name)s',
                         moin_name=app.cfg.interwikiname, fqname=unicode(fqname), user_name=u.name0)
            subscribers_emails = [subscriber.email for subscriber in subscribers
                                  if subscriber.locale == locale]
            sendmail(subject, txt_msg, to=subscribers_emails, html=html_msg)
示例#4
0
    def test_get_subscribers(self, item, item_name, namespace, tag_name):
        users = get_subscribers(**item.meta)
        assert users == set()

        name1 = u'baz'
        password = u'password'
        email1 = u'*****@*****.**'
        name2 = u"bar"
        email2 = u"*****@*****.**"
        name3 = u"barbaz"
        email3 = u"*****@*****.**"
        user.create_user(username=name1, password=password, email=email1, validate=False, locale=u'en')
        user1 = user.User(name=name1, password=password)
        user.create_user(username=name2, password=password, email=email2, validate=False)
        user2 = user.User(name=name2, password=password)
        user.create_user(username=name3, password=password, email=email3, locale=u"en")
        user3 = user.User(name=name3, password=password, email1=email3)
        subscribers = get_subscribers(**item.meta)
        assert subscribers == set()

        namere = r'.*'
        nameprefix = u"fo"
        subscription_lists = [
            ["{0}:{1}".format(ITEMID, item.meta[ITEMID])],
            ["{0}:{1}:{2}".format(TAGS, namespace, tag_name)],
            ["{0}:{1}:{2}".format(NAME, namespace, item_name)],
            ["{0}:{1}:{2}".format(NAMERE, namespace, namere)],
            ["{0}:{1}:{2}".format(NAMEPREFIX, namespace, nameprefix)],
        ]
        users = [user1, user2, user3]
        expected_names = {user1.name0, user2.name0}
        for subscriptions in subscription_lists:
            for user_ in users:
                user_.profile._meta[SUBSCRIPTIONS] = subscriptions
                user_.save(force=True)
            subscribers = get_subscribers(**item.meta)
            subscribers_names = {subscriber.name for subscriber in subscribers}
            assert subscribers_names == expected_names

        meta = {CONTENTTYPE: u'text/plain;charset=utf-8',
                ACL: u"{0}: All:read,write".format(user1.name0)}
        item._save(meta, comment=u"")
        item = Item.create(item_name)
        subscribers = get_subscribers(**item.meta)
        assert {subscriber.name for subscriber in subscribers} == {user2.name0}
示例#5
0
    def test_perf_get_subscribers(self):
        pytest.skip("usually we do no performance tests")
        password = u"password"
        subscriptions = [
            "{0}:{1}".format(ITEMID, self.item.meta[ITEMID]),
            "{0}:{1}:{2}".format(NAME, self.namespace, self.item_name),
            "{0}:{1}:{2}".format(TAGS, self.namespace, self.tagname),
            "{0}:{1}:{2}".format(NAMEPREFIX, self.namespace, u"fo"),
            "{0}:{1}:{2}".format(NAMERE, self.namespace, r"\wo")
        ]
        users = set()
        expected_names = set()
        for i in xrange(10000):
            i = unicode(i)
            user.create_user(username=i,
                             password=password,
                             email="{0}@example.org".format(i),
                             validate=False,
                             locale=u'en')
            user_ = user.User(name=i, password=password)
            users.add(user_)
            expected_names.add(user_.name0)

        users_sliced = list(users)[:100]
        expected_names_sliced = {user_.name0 for user_ in users_sliced}
        tests = [(users_sliced, expected_names_sliced),
                 (users, expected_names)]

        import time
        for users_, expected_names_ in tests:
            print "\nTesting {0} subscribers from a total of {1} users".format(
                len(users_), len(users))
            for subscription in subscriptions:
                for user_ in users_:
                    user_.profile._meta[SUBSCRIPTIONS] = [subscription]
                    user_.save(force=True)
                t = time.time()
                subscribers = get_subscribers(**self.item.meta)
                elapsed_time = time.time() - t
                print "{0}: {1} s".format(
                    subscription.split(':', 1)[0], elapsed_time)
                subscribers_names = {
                    subscriber.name
                    for subscriber in subscribers
                }
                assert subscribers_names == expected_names_
示例#6
0
def send_notifications(app, fqname, **kwargs):
    """ Send mail notifications to subscribers on item change

    :param app: local proxy app
    :param fqname: fqname of the changed item
    :param kwargs: key/value pairs that contain extra information about the item
                   required in order to create a notification
    """
    action = kwargs.get('action')
    revs = get_item_last_revisions(app, fqname) if action not in [
        DESTROY_REV,
        DESTROY_ALL,
    ] else []
    notification = Notification(app, fqname, revs, **kwargs)
    content_diff = notification.get_content_diff()
    meta_diff = notification.get_meta_diff()

    u = flaskg.user
    meta = kwargs.get('meta') if action in [
        DESTROY_REV,
        DESTROY_ALL,
    ] else revs[0].meta._meta
    subscribers = {
        subscriber
        for subscriber in get_subscribers(**meta)
        if subscriber.itemid != u.itemid
    }
    subscribers_locale = {subscriber.locale for subscriber in subscribers}
    for locale in subscribers_locale:
        with force_locale(locale):
            txt_msg, html_msg = notification.render_templates(
                content_diff, meta_diff)
            subject = L_(
                '[%(moin_name)s] Update of "%(fqname)s" by %(user_name)s',
                moin_name=app.cfg.interwikiname,
                fqname=unicode(fqname),
                user_name=u.name0)
            subscribers_emails = [
                subscriber.email for subscriber in subscribers
                if subscriber.locale == locale
            ]
            sendmail(subject, txt_msg, to=subscribers_emails, html=html_msg)
    def test_perf_get_subscribers(self):
        pytest.skip("usually we do no performance tests")
        password = u"password"
        subscriptions = [
            "{0}:{1}".format(ITEMID, self.item.meta[ITEMID]),
            "{0}:{1}:{2}".format(NAME, self.namespace, self.item_name),
            "{0}:{1}:{2}".format(TAGS, self.namespace, self.tagname),
            "{0}:{1}:{2}".format(NAMEPREFIX, self.namespace, u"fo"),
            "{0}:{1}:{2}".format(NAMERE, self.namespace, r"\wo"),
        ]
        users = set()
        expected_names = set()
        for i in xrange(10000):
            i = unicode(i)
            user.create_user(
                username=i, password=password, email="{0}@example.org".format(i), validate=False, locale=u"en"
            )
            user_ = user.User(name=i, password=password)
            users.add(user_)
            expected_names.add(user_.name0)

        users_sliced = list(users)[:100]
        expected_names_sliced = {user_.name0 for user_ in users_sliced}
        tests = [(users_sliced, expected_names_sliced), (users, expected_names)]

        import time

        for users_, expected_names_ in tests:
            print "\nTesting {0} subscribers from a total of {1} users".format(len(users_), len(users))
            for subscription in subscriptions:
                for user_ in users_:
                    user_.profile._meta[SUBSCRIPTIONS] = [subscription]
                    user_.save(force=True)
                t = time.time()
                subscribers = get_subscribers(**self.item.meta)
                elapsed_time = time.time() - t
                print "{0}: {1} s".format(subscription.split(":", 1)[0], elapsed_time)
                subscribers_names = {subscriber.name for subscriber in subscribers}
                assert subscribers_names == expected_names_