<commit_before>import logging

from flask import Blueprint, request

from aleph.core import db
from aleph.model import Mapping
from aleph.search import QueryParser, DatabaseQueryResult
from aleph.views.serializers import MappingSerializer
from aleph.views.util import get_db_collection, get_index_entity, parse_request  # noqa
from aleph.views.context import enable_cache
from aleph.views.forms import MappingSchema
from aleph.views.util import require, obj_or_404, get_session_id
from aleph.queues import queue_task, OP_BULKLOAD
from aleph.logic.collections import refresh_collection
from aleph.logic.mapping import load_query, get_mapping_query
from aleph.index.entities import delete_entities_by_mapping_id

blueprint = Blueprint('mappings_api', __name__)
log = logging.getLogger(__name__)


@blueprint.route('/api/2/collections/<int:collection_id>/mappings', methods=['GET'])  # noqa
def index_by_collection(collection_id):
    collection = get_db_collection(collection_id)
    parser = QueryParser(request.args, request.authz)
    q = Mapping.by_collection(collection.id)
    if 'table' in parser.filters:
        q = Mapping.by_table(parser.filters['table'].pop(), q)
    result = DatabaseQueryResult(request, q, parser=parser)
    return MappingSerializer.jsonify_result(result)


@blueprint.route('/api/2/collections/<int:collection_id>/mappings', methods=['POST', 'PUT'])  # noqa
def create(collection_id):
    collection = get_db_collection(collection_id, action=request.authz.WRITE)
    data = parse_request(MappingSchema)
    entity_id = data.get('table_id')
    query = load_query()
    enable_cache()
    entity = get_index_entity(entity_id, request.authz.READ)
    mapping = Mapping.create(query, entity.get('id'), collection, request.authz.id)  # noqa
    return MappingSerializer.jsonify(mapping)


@blueprint.route('/api/2/collections/<int:collection_id>/mappings/<int:mapping_id>', methods=['GET'])  # noqa
def view(collection_id, mapping_id):
    require(request.authz.logged_in)
    get_db_collection(collection_id, action=request.authz.WRITE)
    mapping = obj_or_404(Mapping.by_id(mapping_id))
    return MappingSerializer.jsonify(mapping)


@blueprint.route('/api/2/collections/<int:collection_id>/mappings/<int:mapping_id>', methods=['POST', 'PUT'])  # noqa
def update(collection_id, mapping_id):
    require(request.authz.logged_in)
    collection = get_db_collection(collection_id, action=request.authz.WRITE)
    mapping = obj_or_404(Mapping.by_id(mapping_id))
    data = parse_request(MappingSchema)
    entity_id = data.get('table_id')
    query = load_query()
    enable_cache()
    entity = get_index_entity(entity_id, request.authz.READ)
    mapping.update(query=query, table_id=entity.get('id'))
    if request.args.get('flush') is True:
        delete_entities_by_mapping_id(mapping.id)
        collection.touch()
        db.session.commit()
        refresh_collection(collection.id)
    return MappingSerializer.jsonify(mapping)


@blueprint.route('/api/2/collections/<int:collection_id>/mappings/<int:mapping_id>', methods=['DELETE'])  # noqa
def delete(collection_id, mapping_id):
    require(request.authz.logged_in)
    collection = get_db_collection(collection_id, action=request.authz.WRITE)
    mapping = obj_or_404(Mapping.by_id(mapping_id))
    mapping.delete()
    if request.args.get('flush') == 'true':
        delete_entities_by_mapping_id(mapping.id)
        collection.touch()
        db.session.commit()
        refresh_collection(collection.id)
    return ('', 204)

<commit_msg>mapping update api needs a fix too<commit_after>import logging

from flask import Blueprint, request

from aleph.core import db
from aleph.model import Mapping
from aleph.search import QueryParser, DatabaseQueryResult
from aleph.views.serializers import MappingSerializer
from aleph.views.util import get_db_collection, get_index_entity, parse_request  # noqa
from aleph.views.context import enable_cache
from aleph.views.forms import MappingSchema
from aleph.views.util import require, obj_or_404, get_session_id
from aleph.queues import queue_task, OP_BULKLOAD
from aleph.logic.collections import refresh_collection
from aleph.logic.mapping import load_query, get_mapping_query
from aleph.index.entities import delete_entities_by_mapping_id

blueprint = Blueprint('mappings_api', __name__)
log = logging.getLogger(__name__)


@blueprint.route('/api/2/collections/<int:collection_id>/mappings', methods=['GET'])  # noqa
def index_by_collection(collection_id):
    collection = get_db_collection(collection_id)
    parser = QueryParser(request.args, request.authz)
    q = Mapping.by_collection(collection.id)
    if 'table' in parser.filters:
        q = Mapping.by_table(parser.filters['table'].pop(), q)
    result = DatabaseQueryResult(request, q, parser=parser)
    return MappingSerializer.jsonify_result(result)


@blueprint.route('/api/2/collections/<int:collection_id>/mappings', methods=['POST', 'PUT'])  # noqa
def create(collection_id):
    collection = get_db_collection(collection_id, action=request.authz.WRITE)
    data = parse_request(MappingSchema)
    entity_id = data.get('table_id')
    query = load_query()
    enable_cache()
    entity = get_index_entity(entity_id, request.authz.READ)
    mapping = Mapping.create(query, entity.get('id'), collection, request.authz.id)  # noqa
    return MappingSerializer.jsonify(mapping)


@blueprint.route('/api/2/collections/<int:collection_id>/mappings/<int:mapping_id>', methods=['GET'])  # noqa
def view(collection_id, mapping_id):
    require(request.authz.logged_in)
    get_db_collection(collection_id, action=request.authz.WRITE)
    mapping = obj_or_404(Mapping.by_id(mapping_id))
    return MappingSerializer.jsonify(mapping)


@blueprint.route('/api/2/collections/<int:collection_id>/mappings/<int:mapping_id>', methods=['POST', 'PUT'])  # noqa
def update(collection_id, mapping_id):
    require(request.authz.logged_in)
    collection = get_db_collection(collection_id, action=request.authz.WRITE)
    mapping = obj_or_404(Mapping.by_id(mapping_id))
    data = parse_request(MappingSchema)
    entity_id = data.get('table_id')
    query = load_query()
    enable_cache()
    entity = get_index_entity(entity_id, request.authz.READ)
    mapping.update(query=query, table_id=entity.get('id'))
    if request.args.get('flush') == 'true':
        delete_entities_by_mapping_id(mapping.id)
        collection.touch()
        db.session.commit()
        refresh_collection(collection.id)
    return MappingSerializer.jsonify(mapping)


@blueprint.route('/api/2/collections/<int:collection_id>/mappings/<int:mapping_id>', methods=['DELETE'])  # noqa
def delete(collection_id, mapping_id):
    require(request.authz.logged_in)
    collection = get_db_collection(collection_id, action=request.authz.WRITE)
    mapping = obj_or_404(Mapping.by_id(mapping_id))
    mapping.delete()
    if request.args.get('flush') == 'true':
        delete_entities_by_mapping_id(mapping.id)
        collection.touch()
        db.session.commit()
        refresh_collection(collection.id)
    return ('', 204)


{'fraction_non_alphanumeric': 0.11542260476929589, 'fraction_numerical': 0.005644137152532806, 'mean_word_length': 5.244933920704846, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, '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': '7361570', 'n_tokens_mistral': 2270, 'n_tokens_neox': 2244, 'n_words': 385}