예제 #1
0
def export(_ids):
    user = get_user(required=True)
    _format = flask.request.args.get('format')
    if not _format:
        return jsonify({'message': 'No format specified.'}), 400

    layout_format = flask.request.args.get('secondary_format')
    formatter = app.download_formatters[_format]['formatter']
    monitoring_profile = get_entity_or_404(
        flask.request.args.get('monitoring_profile'), 'monitoring')
    monitoring_profile['format_type'] = _format
    monitoring_profile['alert_type'] = layout_format
    items = get_items_for_monitoring_report([_id for _id in _ids.split(',')],
                                            monitoring_profile)

    if len(items) > 0:
        try:
            _file = get_monitoring_file(monitoring_profile, items)
        except Exception as e:
            logger.exception(e)
            return jsonify({'message': 'Error exporting items to file'}), 400

        if _file:
            update_action_list(_ids.split(','), 'export', force_insert=True)
            get_resource_service('history').create_history_record(
                items, 'export', user, 'monitoring')

            return send_file(
                _file,
                mimetype=formatter.get_mimetype(None),
                attachment_filename=formatter.format_filename(None),
                as_attachment=True)

    return jsonify({'message': 'No files to export.'}), 400
예제 #2
0
def share():
    data = get_json_or_400()
    assert data.get('users')
    assert data.get('items')
    assert data.get('monitoring_profile')
    current_user = get_user(required=True)
    monitoring_profile = get_entity_or_404(data.get('monitoring_profile'),
                                           'monitoring')
    items = get_items_for_monitoring_report(data.get('items'),
                                            monitoring_profile)

    for user_id in data['users']:
        user = get_resource_service('users').find_one(req=None, _id=user_id)
        template_kwargs = {
            'recipient': user,
            'sender': current_user,
            'message': data.get('message'),
            'item_name': 'Monitoring Report',
        }
        formatter = app.download_formatters['monitoring_pdf']['formatter']
        monitoring_profile['format_type'] = 'monitoring_pdf'
        _file = get_monitoring_file(monitoring_profile, items)
        attachment = base64.b64encode(_file.read())

        send_email(
            [user['email']],
            gettext(
                'From %s: %s' % (app.config['SITE_NAME'],
                                 monitoring_profile.get(
                                     'subject', monitoring_profile['name']))),
            text_body=flask.render_template('share_items.txt',
                                            **template_kwargs),
            html_body=flask.render_template('share_items.html',
                                            **template_kwargs),
            attachments_info=[{
                'file':
                attachment,
                'file_name':
                formatter.format_filename(None),
                'content_type':
                'application/{}'.format(formatter.FILE_EXTENSION),
                'file_desc':
                'Monitoring Report'
            }],
        )

    update_action_list(data.get('items'), 'shares')
    get_resource_service('history').create_history_record(
        items, 'share', current_user, 'monitoring')
    return jsonify({'success': True}), 200