<commit_before>import pyramid_jsonapi.workflow as wf
import sqlalchemy

from collections.abc import Sequence

from sqlalchemy.orm.interfaces import (
    ONETOMANY,
    MANYTOMANY,
    MANYTOONE
)

from pyramid.httpexceptions import (
    HTTPBadRequest,
    HTTPForbidden,
    HTTPConflict,
    HTTPNotFound,
)

stages = (
    'alter_direct_results',
    'alter_related_query',
    'alter_related_results',
    'alter_results',
)


def workflow(view, stages, prev_data):
    try:
        data = view.request.json_body['data']
    except KeyError:
        raise HTTPBadRequest('data attribute required in POSTs.')

    if not isinstance(data, dict):
        raise HTTPBadRequest('data attribute must contain a single resource object.')

    # Check to see if we're allowing client ids
    if not view.api.settings.allow_client_ids and 'id' in data:
        raise HTTPForbidden('Client generated ids are not supported.')
    # Type should be correct or raise 409 Conflict
    datatype = data.get('type')
    if datatype != view.collection_name:
        raise HTTPConflict("Unsupported type '{}'".format(datatype))
    try:
        atts = data['attributes']
    except KeyError:
        atts = {}
    if 'id' in data:
        atts[view.model.__pyramid_jsonapi__['id_col_name']] = data['id']
    item = view.model(**atts)
    with view.dbsession.no_autoflush:
        for relname, reldict in data.get('relationships', {}).items():
            try:
                reldata = reldict['data']
            except KeyError:
                raise HTTPBadRequest(
                    'relationships within POST must have data member'
                )
            try:
                rel = view.relationships[relname]
            except KeyError:
                raise HTTPNotFound(
                    'No relationship {} in collection {}'.format(
                        relname,
                        view.collection_name
                    )
                )
            rel_type = view.api.view_classes[rel.tgt_class].collection_name
            if rel.direction is ONETOMANY or rel.direction is MANYTOMANY:
                # reldata should be a list/array
                if not isinstance(reldata, Sequence) or isinstance(reldata, str):
                    raise HTTPBadRequest(
                        'Relationship data should be an array for TOMANY relationships.'
                    )
                rel_items = []
                for rel_identifier in reldata:
                    if rel_identifier.get('type') != rel_type:
                        raise HTTPConflict(
                            'Relationship identifier has type {} and should be {}'.format(
                                rel_identifier.get('type'), rel_type
                            )
                        )
                    try:
                        rel_items.append(view.dbsession.query(rel.tgt_class).get(rel_identifier['id']))
                    except KeyError:
                        raise HTTPBadRequest(
                            'Relationship identifier must have an id member'
                        )
                setattr(item, relname, rel_items)
            else:
                print('Adding to_one related.')
                if (not isinstance(reldata, dict)) and (reldata is not None):
                    raise HTTPBadRequest(
                        'Relationship data should be a resource identifier object or null.'
                    )
                if reldata.get('type') != rel_type:
                    raise HTTPConflict(
                        'Relationship identifier has type {} and should be {}'.format(
                            reldata.get('type'), rel_type
                        )
                    )
                try:
                    setattr(
                        item,
                        relname,
                        view.dbsession.query(rel.tgt_class).get(reldata['id'])
                    )
                except KeyError:
                    raise HTTPBadRequest(
                        'No id member in relationship data.'
                    )
    try:
        view.dbsession.add(item)
        view.dbsession.flush()
    except sqlalchemy.exc.IntegrityError as exc:
        raise HTTPConflict(exc.args[0])
    view.request.response.status_code = 201
    view.request.response.headers['Location'] = view.request.route_url(
        view.api.endpoint_data.make_route_name(view.collection_name, suffix='item'),
        **{'id': view.id_col(item)}<commit_msg>remove extrenous print.<commit_after>import pyramid_jsonapi.workflow as wf
import sqlalchemy

from collections.abc import Sequence

from sqlalchemy.orm.interfaces import (
    ONETOMANY,
    MANYTOMANY,
    MANYTOONE
)

from pyramid.httpexceptions import (
    HTTPBadRequest,
    HTTPForbidden,
    HTTPConflict,
    HTTPNotFound,
)

stages = (
    'alter_direct_results',
    'alter_related_query',
    'alter_related_results',
    'alter_results',
)


def workflow(view, stages, prev_data):
    try:
        data = view.request.json_body['data']
    except KeyError:
        raise HTTPBadRequest('data attribute required in POSTs.')

    if not isinstance(data, dict):
        raise HTTPBadRequest('data attribute must contain a single resource object.')

    # Check to see if we're allowing client ids
    if not view.api.settings.allow_client_ids and 'id' in data:
        raise HTTPForbidden('Client generated ids are not supported.')
    # Type should be correct or raise 409 Conflict
    datatype = data.get('type')
    if datatype != view.collection_name:
        raise HTTPConflict("Unsupported type '{}'".format(datatype))
    try:
        atts = data['attributes']
    except KeyError:
        atts = {}
    if 'id' in data:
        atts[view.model.__pyramid_jsonapi__['id_col_name']] = data['id']
    item = view.model(**atts)
    with view.dbsession.no_autoflush:
        for relname, reldict in data.get('relationships', {}).items():
            try:
                reldata = reldict['data']
            except KeyError:
                raise HTTPBadRequest(
                    'relationships within POST must have data member'
                )
            try:
                rel = view.relationships[relname]
            except KeyError:
                raise HTTPNotFound(
                    'No relationship {} in collection {}'.format(
                        relname,
                        view.collection_name
                    )
                )
            rel_type = view.api.view_classes[rel.tgt_class].collection_name
            if rel.direction is ONETOMANY or rel.direction is MANYTOMANY:
                # reldata should be a list/array
                if not isinstance(reldata, Sequence) or isinstance(reldata, str):
                    raise HTTPBadRequest(
                        'Relationship data should be an array for TOMANY relationships.'
                    )
                rel_items = []
                for rel_identifier in reldata:
                    if rel_identifier.get('type') != rel_type:
                        raise HTTPConflict(
                            'Relationship identifier has type {} and should be {}'.format(
                                rel_identifier.get('type'), rel_type
                            )
                        )
                    try:
                        rel_items.append(view.dbsession.query(rel.tgt_class).get(rel_identifier['id']))
                    except KeyError:
                        raise HTTPBadRequest(
                            'Relationship identifier must have an id member'
                        )
                setattr(item, relname, rel_items)
            else:
                if (not isinstance(reldata, dict)) and (reldata is not None):
                    raise HTTPBadRequest(
                        'Relationship data should be a resource identifier object or null.'
                    )
                if reldata.get('type') != rel_type:
                    raise HTTPConflict(
                        'Relationship identifier has type {} and should be {}'.format(
                            reldata.get('type'), rel_type
                        )
                    )
                try:
                    setattr(
                        item,
                        relname,
                        view.dbsession.query(rel.tgt_class).get(reldata['id'])
                    )
                except KeyError:
                    raise HTTPBadRequest(
                        'No id member in relationship data.'
                    )
    try:
        view.dbsession.add(item)
        view.dbsession.flush()
    except sqlalchemy.exc.IntegrityError as exc:
        raise HTTPConflict(exc.args[0])
    view.request.response.status_code = 201
    view.request.response.headers['Location'] = view.request.route_url(
        view.api.endpoint_data.make_route_name(view.collection_name, suffix='item'),
        **{'id': view.id_col(item)}
{'fraction_non_alphanumeric': 0.0821127385707945, 'fraction_numerical': 0.0015534842432312472, 'mean_word_length': 1.5218242865137102, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '5556313', 'n_tokens_mistral': 2388, 'n_tokens_neox': 2225, 'n_words': 570}