# Path: lektor/admin/utils.py
# def eventstream(f):
#     def new_func(*args, **kwargs):
#         def generate():
#             for event in chain(f(*args, **kwargs), (None,)):
#                 yield 'data: %s\n\n' % json.dumps(event)
#         return Response(generate(), mimetype='text/event-stream',
#                         direct_passthrough=True)
#     return update_wrapper(new_func, f)
# 
# Path: lektor/publisher.py
# def publish(self, target_url, credentials=None):
#     raise NotImplementedError()
# 
# class PublishError(Exception):
#     pass
# 
# Path: lektor/environment.py
# PRIMARY_ALT = '_primary'


import os
import posixpath
import click
from flask import Blueprint, jsonify, request, g, current_app
from lektor.utils import is_valid_id
from lektor.admin.utils import eventstream
from lektor.publisher import publish, PublishError
from lektor.environment import PRIMARY_ALT
    ts = g.admin_context.tree.edit(path, datamodel=values.get('model'),
                                   alt=alt)
    with ts:
        if ts.exists:
            exists = True
        else:
            ts.update(values.get('data') or {})

    return jsonify({
        'valid_id': True,
        'exists': exists,
        'path': path
    })


@bp.route('/deleterecord', methods=['POST'])
def delete_record():
    alt = request.values.get('alt') or PRIMARY_ALT
    delete_master = request.values.get('delete_master') == '1'
    if request.values['path'] != '/':
        ts = g.admin_context.tree.edit(request.values['path'], alt=alt)
        with ts:
            ts.delete(delete_master=delete_master)
    return jsonify(okay=True)


@bp.route('/rawrecord', methods=['PUT'])
def update_raw_record():
    values = request.get_json()
    data = values['data']

# Path: lektor/admin/utils.py
# def eventstream(f):
#     def new_func(*args, **kwargs):
#         def generate():
#             for event in chain(f(*args, **kwargs), (None,)):
#                 yield 'data: %s\n\n' % json.dumps(event)
#         return Response(generate(), mimetype='text/event-stream',
#                         direct_passthrough=True)
#     return update_wrapper(new_func, f)
# 
# Path: lektor/publisher.py
# def publish(self, target_url, credentials=None):
#     raise NotImplementedError()
# 
# class PublishError(Exception):
#     pass
# 
# Path: lektor/environment.py
# PRIMARY_ALT = '_primary'


# Path: lektor/admin/modules/api.py
import os
import posixpath
import click
from flask import Blueprint, jsonify, request, g, current_app
from lektor.utils import is_valid_id
from lektor.admin.utils import eventstream
from lektor.publisher import publish, PublishError
from lektor.environment import PRIMARY_ALT

    ts = g.admin_context.tree.edit(path, datamodel=values.get('model'),
                                   alt=alt)
    with ts:
        if ts.exists:
            exists = True
        else:
            ts.update(values.get('data') or {})

    return jsonify({
        'valid_id': True,
        'exists': exists,
        'path': path
    })


@bp.route('/deleterecord', methods=['POST'])
def delete_record():
    alt = request.values.get('alt') or PRIMARY_ALT
    delete_master = request.values.get('delete_master') == '1'
    if request.values['path'] != '/':
        ts = g.admin_context.tree.edit(request.values['path'], alt=alt)
        with ts:
            ts.delete(delete_master=delete_master)
    return jsonify(okay=True)


@bp.route('/rawrecord', methods=['PUT'])
def update_raw_record():
    values = request.get_json()
    data = values['data']

    alt = values.get('alt') or PRIMARY_ALT