Beispiel #1
0
def send_notifications(app, fqname, action, data=None, meta=None, new_data=None, new_meta=None, **kwargs):
    """ Send mail notifications to subscribers on item change

    :param app: local proxy app
    :param fqname: fqname of the changed item
    :param action: type of modification - save, rename, destroy...
    :param data: the item's data, None if item is new
    :param meta: the item's meta data, None if item is new
    :param new_data: open file with new data, None if action is delete or destroy
    :param new_meta: new meta data, None if action is delete or destroy
    :param kwargs: optional comment
    """
    if new_meta is None:
        subscribers = {subscriber for subscriber in get_subscribers(**meta) if subscriber.itemid != flaskg.user.itemid}
    else:
        subscribers = {subscriber for subscriber in get_subscribers(**new_meta) if subscriber.itemid != flaskg.user.itemid}
    if not subscribers:
        return
    notification = Notification(app, fqname, action, data, meta, new_data, new_meta, **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 = ['- ' + _('An error has occurred, the current or prior revision of this item may be corrupt.')]
    meta_diff = notification.get_meta_diff()
    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 = _('[%(moin_name)s] Update of "%(fqname)s" by %(user_name)s',
                        moin_name=app.cfg.interwikiname, fqname=str(fqname), user_name=flaskg.user.name0)
            subscribers_emails = [subscriber.email for subscriber in subscribers
                                  if subscriber.locale == locale]
            sendmail(subject, txt_msg, to=subscribers_emails, html=html_msg)
Beispiel #2
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
        # or changed ACL and removed read access for himself
        # 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)
Beispiel #3
0
def test_force_locale():
    pytest.skip("This test needs to be run with --assert=reinterp or --assert=plain flag")
    app = Flask(__name__)
    b = babel.Babel(app)

    @b.localeselector
    def select_locale():
        return 'de_DE'

    with app.test_request_context():
        assert str(babel.get_locale()) == 'de_DE'
        with force_locale('en_US'):
            assert str(babel.get_locale()) == 'en_US'
        assert str(babel.get_locale()) == 'de_DE'