<commit_before>import json

from flask import url_for, make_response

from .flaskapp import app, resource
from .util import *

@app.route('/<username>/layers', methods=['GET', 'POST'])
def layers(username):
    if request.method == 'GET':
        return get_layers(username)
    elif request.method == 'POST':
        return post_layers(username)

    # should never be reached
    abort(500) # pragma: no coverage

@resource
def get_layers(username):
    user = get_user_or_404(username)
    resources = []
    for resource in user.layers:
        resources.append({
            'name': resource.name,
            'url': url_for_layer(resource),
            'urn': urn_for_layer(resource),
        })
    return {
            'owner': { 'username': user.username, 'url': url_for_user(user) },
            'resources': resources,
    }

def post_layers(username):
    user = get_user_or_404(username)

    # Create a new layer
    l = model.Layer(user)
    if request.json is not None:
        update_layer(l, request.json)
    l.save()

    # Return it
    response = make_response(json.dumps({ 'url': url_for_layer(l), 'urn': urn_for_layer(l) }), 201)
    response.headers['Location'] = url_for_layer(l)
    response.headers['Content-Type'] = 'application/json'
    return response

@app.route('/<username>/layers/<layer_id>', methods=['GET', 'PUT'])
def layer(username, layer_id):
    if request.method == 'GET':
        return get_layer(username, layer_id)
    elif request.method == 'PUT':
        return put_layer(username, layer_id)

    # should never be reached
    abort(500) # pragma: no coverage

@resource
def get_layer(username, layer_id):
    user, layer = get_user_and_layer_or_404(username, layer_id)
    return {
            'name': layer.name,
            'urn': urn_for_layer(layer),
            'owner': { 'username': user.username, 'url': url_for_user(user) },
            'source': { 'bucket': urn_for_bucket(layer.bucket) if layer.bucket is not None else None, 'source': layer.bucket_layer_name },
    }

def put_layer(username, layer_id):
    user, l = get_user_and_layer_or_404(username, layer_id)
    if request.json is not None:
        update_layer(l, request.json)
    l.save()

    # Return it
    response = make_response(json.dumps({ 'url': url_for_layer(l), 'urn': urn_for_layer(l) }), 201)
    response.headers['Location'] = url_for_layer(l)
    response.headers['Content-Type'] = 'application/json'
    return response
<commit_msg>REST layer: include bucket link in REST output<commit_after>import json

from flask import url_for, make_response

from .flaskapp import app, resource
from .util import *

@app.route('/<username>/layers', methods=['GET', 'POST'])
def layers(username):
    if request.method == 'GET':
        return get_layers(username)
    elif request.method == 'POST':
        return post_layers(username)

    # should never be reached
    abort(500) # pragma: no coverage

@resource
def get_layers(username):
    user = get_user_or_404(username)
    resources = []
    for resource in user.layers:
        resources.append({
            'name': resource.name,
            'url': url_for_layer(resource),
            'urn': urn_for_layer(resource),
        })
    return {
            'owner': { 'username': user.username, 'url': url_for_user(user) },
            'resources': resources,
    }

def post_layers(username):
    user = get_user_or_404(username)

    # Create a new layer
    l = model.Layer(user)
    if request.json is not None:
        update_layer(l, request.json)
    l.save()

    # Return it
    response = make_response(json.dumps({ 'url': url_for_layer(l), 'urn': urn_for_layer(l) }), 201)
    response.headers['Location'] = url_for_layer(l)
    response.headers['Content-Type'] = 'application/json'
    return response

@app.route('/<username>/layers/<layer_id>', methods=['GET', 'PUT'])
def layer(username, layer_id):
    if request.method == 'GET':
        return get_layer(username, layer_id)
    elif request.method == 'PUT':
        return put_layer(username, layer_id)

    # should never be reached
    abort(500) # pragma: no coverage

@resource
def get_layer(username, layer_id):
    user, layer = get_user_and_layer_or_404(username, layer_id)

    bucket = None
    b = layer.bucket
    if b is not None:
        bucket = { 'urn': urn_for_bucket(b), 'url': url_for_bucket(b) }

    return {
            'name': layer.name,
            'urn': urn_for_layer(layer),
            'owner': { 'username': user.username, 'url': url_for_user(user) },
            'source': { 'bucket': bucket, 'source': layer.bucket_layer_name },
    }

def put_layer(username, layer_id):
    user, l = get_user_and_layer_or_404(username, layer_id)
    if request.json is not None:
        update_layer(l, request.json)
    l.save()

    # Return it
    response = make_response(json.dumps({ 'url': url_for_layer(l), 'urn': urn_for_layer(l) }), 201)
    response.headers['Location'] = url_for_layer(l)
    response.headers['Content-Type'] = 'application/json'
    return response

{'fraction_non_alphanumeric': 0.13347877826259422, 'fraction_numerical': 0.009520031733439112, 'mean_word_length': 3.199000832639467, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7032298', 'n_tokens_mistral': 1647, 'n_tokens_neox': 1604, 'n_words': 378}