Skip to content

Commit

Permalink
DEV-1079 Preprocess field name extension (HumanSignal#1714)
Browse files Browse the repository at this point in the history
Co-authored-by: Konstantin Korotaev <[email protected]>
Co-authored-by: Max Tkachenko <[email protected]>
Co-authored-by: niklub <[email protected]>
  • Loading branch information
4 people authored Dec 24, 2021
1 parent a2532d4 commit 67420e8
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 22 deletions.
1 change: 1 addition & 0 deletions label_studio/core/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,7 @@
GET_STORAGE_LIST = 'io_storages.functions.get_storage_list'
STORAGE_ANNOTATION_SERIALIZER = 'io_storages.serializers.StorageAnnotationSerializer'
TASK_SERIALIZER_BULK = 'tasks.serializers.BaseTaskSerializerBulk'
PREPROCESS_FIELD_NAME = 'data_manager.functions.preprocess_field_name'


def project_delete(project):
Expand Down
13 changes: 13 additions & 0 deletions label_studio/data_manager/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,3 +292,16 @@ def custom_filter_expressions(*args, **kwargs):

def preprocess_filter(_filter, *_):
return _filter


def preprocess_field_name(raw_field_name, only_undefined_field=False):
field_name = raw_field_name.replace("filter:", "")
field_name = field_name.replace("tasks:", "")
ascending = False if field_name[0] == '-' else True # detect direction
field_name = field_name[1:] if field_name[0] == '-' else field_name # remove direction
if field_name.startswith("data."):
if only_undefined_field:
field_name = f'data__{settings.DATA_UNDEFINED_NAME}'
else:
field_name = field_name.replace("data.", "data__")
return field_name, ascending
27 changes: 5 additions & 22 deletions label_studio/data_manager/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,6 @@ class _Operator(BaseModel):
}


def preprocess_field_name(raw_field_name, only_undefined_field=False):
field_name = raw_field_name.replace("filter:tasks:", "")
if field_name.startswith("data."):
if only_undefined_field:
field_name = f'data__{settings.DATA_UNDEFINED_NAME}'
else:
field_name = field_name.replace("data.", "data__")

return field_name


def get_fields_for_filter_ordering(prepare_params):
result = []
if prepare_params is None:
Expand Down Expand Up @@ -132,16 +121,10 @@ def get_fields_for_evaluation(prepare_params, user):

def apply_ordering(queryset, ordering, only_undefined_field=False):
if ordering:
field_name = ordering[0].replace("tasks:", "")
ascending = False if field_name[0] == '-' else True # detect direction
field_name = field_name[1:] if field_name[0] == '-' else field_name # remove direction
field_name = field_name.replace('agreement', '_agreement')

if "data." in field_name:
field_name = field_name.replace(".", "__", 1)
if only_undefined_field:
field_name = re.sub('data__\w+', f'data__{settings.DATA_UNDEFINED_NAME}', field_name)
preprocess_field_name = load_func(settings.PREPROCESS_FIELD_NAME)
field_name, ascending = preprocess_field_name(ordering[0], only_undefined_field=only_undefined_field)

if field_name.startswith('data__'):
# annotate task with data field for float/int/bool ordering support
json_field = field_name.replace('data__', '')
queryset = queryset.annotate(ordering_field=KeyTextTransform(json_field, 'data'))
Expand Down Expand Up @@ -190,8 +173,8 @@ def apply_filters(queryset, filters, only_undefined_field=False):
continue

# django orm loop expression attached to column name
field_name = preprocess_field_name(_filter.filter, only_undefined_field)
field_name = field_name.replace('agreement', '_agreement')
preprocess_field_name = load_func(settings.PREPROCESS_FIELD_NAME)
field_name, _ = preprocess_field_name(_filter.filter, only_undefined_field)

# filter preprocessing, value type conversion, etc..
preprocess_filter = load_func(settings.DATA_MANAGER_PREPROCESS_FILTER)
Expand Down

0 comments on commit 67420e8

Please sign in to comment.