Skip to content

Commit

Permalink
refactor: move text operator fields into the entity classes #260
Browse files Browse the repository at this point in the history
  • Loading branch information
MRichards99 committed Dec 21, 2021
1 parent 0944d39 commit 5b71f09
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 12 deletions.
22 changes: 21 additions & 1 deletion datagateway_api/src/search_api/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from abc import ABC, abstractclassmethod
from datetime import datetime
from typing import List, Optional, Union
from typing import ClassVar, List, Optional, Union

from pydantic import (
BaseModel,
Expand All @@ -22,6 +22,8 @@ def from_icat(self):
class Affiliation(PaNOSCAttribute):
"""Information about which facility a member is located at"""

_text_operator_fields: ClassVar[List[str]] = []

name: Optional[StrictStr]
id_: Optional[StrictStr] = Field(alias="id")
address: Optional[StrictStr]
Expand All @@ -41,6 +43,8 @@ class Dataset(PaNOSCAttribute):
and Technique
"""

_text_operator_fields: ClassVar[List[str]] = ["title"]

pid: StrictStr
title: StrictStr
is_public: StrictBool = Field(alias="isPublic")
Expand All @@ -64,6 +68,8 @@ class Document(PaNOSCAttribute):
Proposal which includes the dataset or published paper which references the dataset
"""

_text_operator_fields: ClassVar[List[str]] = ["title", "summary"]

pid: StrictStr
is_public: StrictBool = Field(alias="isPublic")
type_: StrictStr = Field(alias="type")
Expand All @@ -88,6 +94,8 @@ def from_icat(cls):
class File(PaNOSCAttribute):
"""Name of file and optionally location"""

_text_operator_fields: ClassVar[List[str]] = ["name"]

id_: StrictStr = Field(alias="id")
name: StrictStr
path: Optional[StrictStr]
Expand All @@ -103,6 +111,8 @@ def from_icat(cls):
class Instrument(PaNOSCAttribute):
"""Beam line where experiment took place"""

_text_operator_fields: ClassVar[List[str]] = ["name", "facility"]

pid: StrictStr
name: StrictStr
facility: StrictStr
Expand All @@ -117,6 +127,8 @@ def from_icat(cls):
class Member(PaNOSCAttribute):
"""Proposal team member or paper co-author"""

_text_operator_fields: ClassVar[List[str]] = []

id_: StrictStr = Field(alias="id")
role: Optional[StrictStr] = Field(alias="role")

Expand All @@ -135,6 +147,8 @@ class Parameter(PaNOSCAttribute):
Note: a parameter is either related to a dataset or a document, but not both.
"""

_text_operator_fields: ClassVar[List[str]] = []

id_: StrictStr = Field(alias="id")
name: StrictStr
value: Union[StrictFloat, StrictInt, StrictStr]
Expand Down Expand Up @@ -162,6 +176,8 @@ def from_icat(cls):
class Person(PaNOSCAttribute):
"""Human who carried out experiment"""

_text_operator_fields: ClassVar[List[str]] = []

id_: StrictStr = Field(alias="id")
full_name: StrictStr = Field(alias="fullName")
orcid: Optional[StrictStr]
Expand All @@ -179,6 +195,8 @@ def from_icat(cls):
class Sample(PaNOSCAttribute):
"""Extract of material used in the experiment"""

_text_operator_fields: ClassVar[List[str]] = ["name", "description"]

name: StrictStr
pid: StrictStr
description: Optional[StrictStr]
Expand All @@ -193,6 +211,8 @@ def from_icat(cls):
class Technique(PaNOSCAttribute):
"""Common name of scientific method used"""

_text_operator_fields: ClassVar[List[str]] = ["name"]

pid: StrictStr
name: StrictStr

Expand Down
27 changes: 16 additions & 11 deletions datagateway_api/src/search_api/query_filter_factory.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import importlib
import logging

from datagateway_api.src.common.base_query_filter_factory import QueryFilterFactory
Expand Down Expand Up @@ -123,20 +124,24 @@ def get_where_filter(where_filter_input, entity_name):
)
where_filters.append(nested)
elif list(where_filter_input.keys())[0] == "text":
# TODO - we might want to move this to the data
# definitions at a later point
text_operator_fields = {
"Dataset": ["title"],
"Document": ["title", "summary"],
"File": ["name"],
"Instrument": ["name", "facility"],
"Sample": ["name", "description"],
"Technique": ["name"],
}
try:
search_api_models = importlib.import_module(
"datagateway_api.src.search_api.models",
)
entity_class = getattr(search_api_models, entity_name)
except AttributeError as e:
raise SearchAPIError(
f"No text operator fields have been defined for {entity_name}"
f", {e.args}",
)

try:
or_conditional_filters = []
field_names = text_operator_fields[entity_name]
field_names = entity_class._text_operator_fields
if not field_names:
# No text operator fields present, raise KeyError to be caught in
# this try/except block
raise KeyError()
for field_name in field_names:
or_conditional_filters.append(
{field_name: {"like": where_filter_input["text"]}},
Expand Down

0 comments on commit 5b71f09

Please sign in to comment.