import six

from .query_builder import FILTER_FIELDS, TEXT_FIELDS
from .conversions import strip_and_lowercase


def process_values_for_matching(request_json, key):
    values = request_json[key]

    if isinstance(values, list):
        return [strip_and_lowercase(value) for value in values]
    elif isinstance(values, six.string_types):
        return strip_and_lowercase(values)

    return values


def convert_request_json_into_index_json(request_json):
    filter_fields = [field for field in request_json if field in FILTER_FIELDS]

    for field in filter_fields:
        request_json["filter_" + field] = \
            process_values_for_matching(request_json, field)
        if field not in TEXT_FIELDS:
            del request_json[field]

    return request_json

import six

from .query_builder import FILTER_FIELDS, TEXT_FIELDS
from .conversions import strip_and_lowercase

FILTER_FIELDS_SET = set(FILTER_FIELDS)
TEXT_FIELDS_SET = set(TEXT_FIELDS)


def process_values_for_matching(values):
    if isinstance(values, list):
        return [strip_and_lowercase(value) for value in values]
    elif isinstance(values, six.string_types):
        return strip_and_lowercase(values)

    return values


def convert_request_json_into_index_json(request_json):
    index_json = {}

    for field in request_json:
        if field in FILTER_FIELDS_SET:
            index_json["filter_" + field] = process_values_for_matching(
                request_json[field]
            )
        if field in TEXT_FIELDS_SET:
            index_json[field] = request_json[field]

    return index_json

Drop any unknown request fields when converting into index document
Drop any unknown request fields when converting into index document

Previously, convert_request_json_into_index_json relied on the request
being sent through the dmutils.apiclient, which dropped any fields that
aren't supposed to be indexed. This means that dmutils contains a copy
of the filter and text fields lists.

Instead, new converting functions only keeps text and filter fields from
the request, so it can accept any service document for indexing.

  import six
  
  from .query_builder import FILTER_FIELDS, TEXT_FIELDS
  from .conversions import strip_and_lowercase
  
+ FILTER_FIELDS_SET = set(FILTER_FIELDS)
+ TEXT_FIELDS_SET = set(TEXT_FIELDS)
  
- def process_values_for_matching(request_json, key):
-     values = request_json[key]
  
+ def process_values_for_matching(values):
      if isinstance(values, list):
          return [strip_and_lowercase(value) for value in values]
      elif isinstance(values, six.string_types):
          return strip_and_lowercase(values)
  
      return values
  
  
  def convert_request_json_into_index_json(request_json):
-     filter_fields = [field for field in request_json if field in FILTER_FIELDS]
+     index_json = {}
  
+     for field in request_json:
-     for field in filter_fields:
-         request_json["filter_" + field] = \
-             process_values_for_matching(request_json, field)
-         if field not in TEXT_FIELDS:
+         if field in FILTER_FIELDS_SET:
+             index_json["filter_" + field] = process_values_for_matching(
-             del request_json[field]
+                 request_json[field]
+             )
+         if field in TEXT_FIELDS_SET:
+             index_json[field] = request_json[field]
  
-     return request_json
+     return index_json
  
Drop any unknown request fields when converting into index document
## Code Before:
import six

from .query_builder import FILTER_FIELDS, TEXT_FIELDS
from .conversions import strip_and_lowercase


def process_values_for_matching(request_json, key):
    values = request_json[key]

    if isinstance(values, list):
        return [strip_and_lowercase(value) for value in values]
    elif isinstance(values, six.string_types):
        return strip_and_lowercase(values)

    return values


def convert_request_json_into_index_json(request_json):
    filter_fields = [field for field in request_json if field in FILTER_FIELDS]

    for field in filter_fields:
        request_json["filter_" + field] = \
            process_values_for_matching(request_json, field)
        if field not in TEXT_FIELDS:
            del request_json[field]

    return request_json

## Instruction:
Drop any unknown request fields when converting into index document
## Code After:
import six

from .query_builder import FILTER_FIELDS, TEXT_FIELDS
from .conversions import strip_and_lowercase

FILTER_FIELDS_SET = set(FILTER_FIELDS)
TEXT_FIELDS_SET = set(TEXT_FIELDS)


def process_values_for_matching(values):
    if isinstance(values, list):
        return [strip_and_lowercase(value) for value in values]
    elif isinstance(values, six.string_types):
        return strip_and_lowercase(values)

    return values


def convert_request_json_into_index_json(request_json):
    index_json = {}

    for field in request_json:
        if field in FILTER_FIELDS_SET:
            index_json["filter_" + field] = process_values_for_matching(
                request_json[field]
            )
        if field in TEXT_FIELDS_SET:
            index_json[field] = request_json[field]

    return index_json
