示例#1
0
def check_role_alerts(authz):
    alerts = Alert.by_role(authz.role).all()
    if not len(alerts):
        return
    log.info('Alerting %r, %d alerts...', authz.role, len(alerts))
    for alert in alerts:
        args = {'q': alert.query_text}
        entity = get_entity(alert.entity_id) if alert.entity_id else None
        state = SearchQueryParser(args, authz)
        query = AlertDocumentsQuery(state,
                                    entity=entity,
                                    since=alert.notified_at)
        results = query.search().get('hits')
        if results['total'] == 0:
            continue
        log.info('Found %d new results for: %r', results['total'], alert.label)
        alert.update()
        try:
            subject = '%s (%s new results)' % (alert.label, results['total'])
            html = render_template('email/alert.html',
                                   alert=alert,
                                   role=authz.role,
                                   total=results.get('total'),
                                   results=format_results(alert, results),
                                   app_title=app_title,
                                   ui_url=app_ui_url)
            notify_role(authz.role, subject, html)
        except Exception as ex:
            log.exception(ex)
    db.session.commit()
示例#2
0
文件: alerts.py 项目: wcyn/aleph
def check_role_alerts(authz):
    alerts = Alert.by_role(authz.role).all()
    if not len(alerts):
        return
    log.info('Alerting %r, %d alerts...', authz.role, len(alerts))
    for alert in alerts:
        args = {
            'q': alert.query_text,
            'filter:entities.id': alert.entity_id,
            'limit': 50
        }
        state = QueryState(args, authz)
        results = documents_query(state, since=alert.notified_at)
        if results['total'] == 0:
            continue
        log.info('Found %d new results for: %r', results['total'], alert.label)
        alert.update()
        try:
            subject = '%s (%s new results)' % (alert.label, results['total'])
            html = render_template('email/alert.html',
                                   alert=alert,
                                   role=authz.role,
                                   total=results.get('total'),
                                   results=format_results(alert, results),
                                   app_title=app_title,
                                   app_url=app_url)
            notify_role(authz.role, subject, html)
        except Exception as ex:
            log.exception(ex)
    db.session.commit()
示例#3
0
def check_role_alerts(role):
    alerts = Alert.by_role(role).all()
    if not len(alerts):
        return
    log.info('Alerting %r, %d alerts...', role, len(alerts))
    for alert in alerts:
        results = alert_query(alert)
        if results['total'] == 0:
            continue
        log.info('Found: %d new results for: %r', results['total'],
                 alert.label)
        alert.update()
        try:
            subject = '%s (%s new results)' % (alert.label, results['total'])
            html = render_template('alert.html',
                                   alert=alert,
                                   results=results,
                                   role=role,
                                   qs=make_document_query(alert),
                                   app_title=get_config('APP_TITLE'),
                                   app_url=get_app_url())
            notify_role(role, subject, html)
        except Exception as ex:
            log.exception(ex)
    db.session.commit()
示例#4
0
def create_code():
    data = parse_request(RoleCodeCreateSchema)
    signature = Role.SIGNATURE.dumps(data['email'])
    url = '{}activate/{}'.format(settings.APP_UI_URL, signature)
    role = Role(email=data['email'], name='Visitor')
    log.info("Confirmation URL [%r]: %s", role, url)
    notify_role(role,
                gettext('Registration'),
                'email/registration_code.html',
                url=url)
    return jsonify({
        'status': 'ok',
        'message': gettext('To proceed, please check your email.')
    })
示例#5
0
文件: roles_api.py 项目: wcyn/aleph
def invite_email():
    data = request_data()
    email = data.get('email')

    if not email:
        abort(400)

    signature = Role.SIGNATURE_SERIALIZER.dumps(email, salt=email)
    url = '{}signup/{}'.format(app_url, signature)
    role = Role(email=email, name='Visitor')

    notify_role(role=role,
                subject='Registration',
                html=render_template('email/registration_invitation.html',
                                     url=url,
                                     role=role))

    return jsonify({'status': 'To proceed, please check your email.'}), 201
示例#6
0
def generate_role_digest(role):
    """Generate notification digest emails for the given user."""
    # TODO: get and use the role's locale preference.
    since = datetime.utcnow() - timedelta(hours=25)
    q = Notification.by_role(role, since=since)
    total_count = q.count()
    if total_count == 0:
        return
    notifications = []
    for notification in q.limit(25):
        notifications.append(render_notification(notification))

    subject = '%s notifications' % total_count
    notify_role(role, subject,
                'email/notifications.html',
                total_count=total_count,
                notifications=notifications,
                manage_url=ui_url('notifications'))
示例#7
0
def update_permission(role, collection, read, write):
    """Update a roles permission to access a given collection."""
    pre = Permission.by_collection_role(collection.id, role)
    post = Permission.grant_collection(collection.id, role, read, write)
    db.session.commit()

    try:
        url = '%scollections/%s' % (app_url, collection.id)
        html = render_template('email/permission.html',
                               role=role,
                               url=url,
                               collection=collection,
                               pre=pre,
                               post=post,
                               app_url=app_url,
                               app_title=app_title)
        notify_role(role, collection.label, html)
    except Exception as ex:
        log.exception(ex)
    return post
示例#8
0
def check_role_alerts(role):
    alerts = Alert.by_role(role).all()
    if not len(alerts):
        return
    log.info('Alerting %r, %d alerts...', role, len(alerts))
    for alert in alerts:
        results = alert_query(alert)
        if results['total'] == 0:
            continue
        log.info('Found: %d new results for: %r', results['total'],
                 alert.label)
        alert.update()
        try:
            subject = '%s (%s new results)' % (alert.label, results['total'])
            html = render_template('alert.html', alert=alert, results=results,
                                   role=role, qs=make_document_query(alert),
                                   app_title=get_app_title(),
                                   app_url=get_app_url())
            notify_role(role, subject, html)
        except Exception as ex:
            log.exception(ex)
    db.session.commit()