<commit_before>from flask import request
from werkzeug.exceptions import abort

import app
from app.main import main
from app.main.services.process_request_json import convert_request_json_into_index_json, check_json_from_request
from app.main.services.search_service import index, delete_by_id
from app.main.services.response_formatters import api_response


@main.route('/<string:index_name>/<string:doc_type>/<string:document_id>',
            methods=['PUT'])
def index_document(index_name, doc_type, document_id):
    payload = check_json_from_request(request)
    json_payload = payload.get('document') or payload.get('service')  # fallback to 'service' for backward-compat.
    if json_payload is None:
        abort(400, "Invalid JSON must have 'document' key.")

    mapping = app.mapping.get_mapping(index_name, doc_type)
    index_json = convert_request_json_into_index_json(mapping, json_payload)
    result, status_code = index(index_name, doc_type, index_json, document_id)

    return api_response(result, status_code)


@main.route('/<string:index_name>/<string:doc_type>/<string:service_id>',
            methods=['DELETE'])
def delete_service(index_name, doc_type, service_id):
    result, status_code = delete_by_id(index_name, doc_type, service_id)

    return api_response(result, status_code)
<commit_msg>Check doc type on update methods to ensure operation on relevant documents<commit_after>from flask import request
from werkzeug.exceptions import abort

import app
from app.main import main
from app.main.services.process_request_json import convert_request_json_into_index_json, check_json_from_request
from app.main.services.search_service import index, delete_by_id
from app.main.services.response_formatters import api_response


DOC_TYPES = ['services', 'briefs']


@main.route('/<string:index_name>/<string:doc_type>/<string:document_id>',
            methods=['PUT'])
def index_document(index_name, doc_type, document_id):
    if doc_type not in DOC_TYPES:
        abort(400, "doc_type must be one of: {DOC_TYPES}")

    payload = check_json_from_request(request)
    json_payload = payload.get('document') or payload.get('service')  # fallback to 'service' for backward-compat.
    if json_payload is None:
        abort(400, "Invalid JSON must have 'document' key.")

    mapping = app.mapping.get_mapping(index_name, doc_type)
    index_json = convert_request_json_into_index_json(mapping, json_payload)
    result, status_code = index(index_name, doc_type, index_json, document_id)

    return api_response(result, status_code)


@main.route('/<string:index_name>/<string:doc_type>/<string:service_id>',
            methods=['DELETE'])
def delete_service(index_name, doc_type, service_id):
    if doc_type not in DOC_TYPES:
        abort(400, "doc_type must be one of: {DOC_TYPES}")

    result, status_code = delete_by_id(index_name, doc_type, service_id)

    return api_response(result, status_code)

{'fraction_non_alphanumeric': 0.09795918367346938, 'fraction_numerical': 0.004081632653061225, 'mean_word_length': 5.421397379912664, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, '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': '5182432', 'n_tokens_mistral': 924, 'n_tokens_neox': 912, 'n_words': 208}