<commit_before>
import bottle
from bottle import request, response
import sqlalchemy as sa

from bauble import app, API_ROOT
from bauble.middleware import build_counts, basic_auth, filter_param, resolve_relation
from bauble.model import Plant


column_names = [col.name for col in sa.inspect(Plant).columns]

def resolve_plant(next):
    def _wrapped(*args, **kwargs):
        request.plant = request.session.query(Plant).get(request.args['plant_id'])
        if not request.plant:
            bottle.abort(404, "Plant not found")
        return next(*args, **kwargs)
    return _wrapped


@app.get(API_ROOT + "/plant")
@basic_auth
@filter_param(Plant, column_names)
def index_plant():
    # TODO: we're not doing any sanitization or validation...see preggy or validate.py

    plants = request.filter if request.filter else request.session.query(Plant)
    return [plant.json() for plant in plants]


@app.get(API_ROOT + "/plant/<plant_id:int>")
@basic_auth
@resolve_plant
def get_plant(plant_id):
    return request.plant.json()


@app.route(API_ROOT + "/plant/<plant_id:int>", method='PATCH')
@basic_auth
@resolve_plant
def patch_plant(plant_id):

    if not request.json:
        bottle.abort(400, 'The request doesn\'t contain a request body')

    # create a copy of the request data with only the columns
    data = {col: request.json[col] for col in request.json.keys() if col in column_names}
    for key, value in data.items():
        setattr(request.plant, key, data[key])
    request.session.commit()
    return request.plant.json()


@app.post(API_ROOT + "/plant")
@basic_auth
@resolve_relation('accession_id', 'accession')
@resolve_relation('location_id', 'location')
def post_plant():<commit_msg>fix embedding on /plant requests<commit_after>
import bottle
from bottle import request, response
import sqlalchemy as sa

from bauble import app, API_ROOT
from bauble.middleware import build_counts, basic_auth, filter_param, resolve_relation
from bauble.model import Plant, get_relation


column_names = [col.name for col in sa.inspect(Plant).columns]

def resolve_plant(next):
    def _wrapped(*args, **kwargs):
        request.plant = request.session.query(Plant).get(request.args['plant_id'])
        if not request.plant:
            bottle.abort(404, "Plant not found")
        return next(*args, **kwargs)
    return _wrapped


def build_embedded(embed, plant):
    # if embed == 'synonyms':
    #     data = genus.synonyms
    # else:
    #     data = get_relation(Genus, genus.id, embed, session=request.session)
    data = get_relation(Plant, plant.id, embed, session=request.session)

    if isinstance(data, list):
        return (embed, [obj.json() for obj in data])
    else:
        return (embed, data.json() if data else '{}')


@app.get(API_ROOT + "/plant")
@basic_auth
@filter_param(Plant, column_names)
def index_plant():
    # TODO: we're not doing any sanitization or validation...see preggy or validate.py

    plants = request.filter if request.filter else request.session.query(Plant)
    return [plant.json() for plant in plants]


@app.get(API_ROOT + "/plant/<plant_id:int>")
@basic_auth
@resolve_plant
def get_plant(plant_id):

    json_data = request.plant.json()

    if 'embed' in request.params:
        embed_list = request.params.embed if isinstance(request.params.embed, list) \
            else [request.params.embed]
        embedded = map(lambda embed: build_embedded(embed, request.plant), embed_list)
        json_data.update(embedded)

    return json_data


@app.route(API_ROOT + "/plant/<plant_id:int>", method='PATCH')
@basic_auth
@resolve_plant
def patch_plant(plant_id):

    if not request.json:
        bottle.abort(400, 'The request doesn\'t contain a request body')

    # create a copy of the request data with only the columns
    data = {col: request.json[col] for col in request.json.keys() if col in column_names}
    for key, value in data.items():
        setattr(request.plant, key, data[key])
    request.session.commit()
    return request.plant.json()


@app.post(API_ROOT + "/plant")
@basic_auth
@resolve_relation('accession_id', 'accession')
@resolve_relation('location_id', 'location')
def post_plant():
{'fraction_non_alphanumeric': 0.11844938980617373, 'fraction_numerical': 0.002871500358937545, 'mean_word_length': 4.005988023952096, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '1320230', 'n_tokens_mistral': 1333, 'n_tokens_neox': 1304, 'n_words': 362}