Skip to content

Commit

Permalink
Merge branch 'master' into feature/search-api-include-filter-#261
Browse files Browse the repository at this point in the history
  • Loading branch information
MRichards99 committed Jan 31, 2022
2 parents 5da48f8 + 58e5f20 commit e0b2d95
Show file tree
Hide file tree
Showing 9 changed files with 1,082 additions and 119 deletions.
18 changes: 18 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,24 @@

<!--next-version-placeholder-->

## v3.5.0 (2022-01-31)
### Feature
* Implement basic version of `SearchAPIIncludeFilter` #261 ([`f2f53c9`](https://github.com/ral-facilities/datagateway-api/commit/f2f53c92229d052ae697787eb80a35dcd2ea3b45))

### Fix
* Fix list type field checking in Python 3.6 #265 ([`691a59e`](https://github.com/ral-facilities/datagateway-api/commit/691a59ea3f850475572c3a877fb739e5216c6fe7))

### Documentation
* Add new comments and fix existing #265 ([`3f1b1cf`](https://github.com/ral-facilities/datagateway-api/commit/3f1b1cffdd1e57ab4eb1227b13e0906424adefd0))

## v3.4.0 (2022-01-31)
### Feature
* Implement `regexp` operator #297 ([`bf3fe0e`](https://github.com/ral-facilities/datagateway-api/commit/bf3fe0ef2ac582d55dbd881edf6a81a93625ce91))
* Implement `neq` operator #297 ([`9094bbb`](https://github.com/ral-facilities/datagateway-api/commit/9094bbb894ead20a53fadfd0e24b264af29548b9))
* Implement `nin` operator #297 ([`00dbba5`](https://github.com/ral-facilities/datagateway-api/commit/00dbba525d5cd86cb5577f3b1621a7042cdd2fa0))
* Implement `inq` operator #297 ([`fc1cf19`](https://github.com/ral-facilities/datagateway-api/commit/fc1cf194454a4da60652b1f68df278c4624ddc11))
* Implement `between` operator #297 ([`4736888`](https://github.com/ral-facilities/datagateway-api/commit/4736888bf76cda0dbc00f997443ed565f0f5e760))

## v3.3.0 (2022-01-31)
### Feature
* Add function to get PaNOSC to ICAT mapping for where filter #260 ([`34b1d81`](https://github.com/ral-facilities/datagateway-api/commit/34b1d819482aa3efdb4f8da321125d3e40d76617))
Expand Down
4 changes: 2 additions & 2 deletions datagateway_api/search_api_mapping.json.example
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@
"Parameter": {
"base_icat_entity": ["InvestigationParameter", "DatasetParameter"],
"id": "id",
"name": "name",
"name": "type.name",
"value": ["numericValue", "stringValue", "dateTimeValue"],
"unit": "type.units",
"dataset": {"Dataset": "investigation.investigationInstruments.instrument.datasetInstruments.dataset"},
"dataset": {"Dataset": "dataset"},
"document": {"Document": "investigation"}
},
"Person": {
Expand Down
11 changes: 8 additions & 3 deletions datagateway_api/src/common/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,16 @@ def __init__(self, field, value, operation):
self.value = value
self.operation = operation

if self.operation == "in":
if self.operation in ["in", "nin", "inq", "between"]:
if not isinstance(self.value, list):
raise BadRequestError(
"When using the 'in' operation for a WHERE filter, the values must"
" be in a list format e.g. [1, 2, 3]",
f"When using the {self.operation} operation for a WHERE filter, the"
f" values must be in a list format e.g. [1, 2]",
)
if self.operation == "between" and len(self.value) != 2:
raise BadRequestError(
"When using the 'between' operation for a WHERE filter, the list"
"must contain two values e.g. [1, 2]",
)


Expand Down
26 changes: 22 additions & 4 deletions datagateway_api/src/datagateway_api/icat/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def create_filter(self):
log.info("Creating condition for ICAT where filter")
if self.operation == "eq":
where_filter = self.create_condition(self.field, "=", self.value)
elif self.operation == "ne":
elif self.operation in ["ne", "neq"]:
where_filter = self.create_condition(self.field, "!=", self.value)
elif self.operation == "like":
where_filter = self.create_condition(self.field, "like", f"%{self.value}%")
Expand All @@ -75,7 +75,7 @@ def create_filter(self):
where_filter = self.create_condition(self.field, ">", self.value)
elif self.operation == "gte":
where_filter = self.create_condition(self.field, ">=", self.value)
elif self.operation == "in":
elif self.operation in ["in", "inq"]:
# Convert self.value into a string with brackets equivalent to tuple format.
# Cannot convert straight to tuple as single element tuples contain a
# trailing comma which Python ICAT/JPQL doesn't accept
Expand All @@ -88,6 +88,25 @@ def create_filter(self):
self.value = "(NULL)"

where_filter = self.create_condition(self.field, "in", self.value)
elif self.operation == "nin":
# Convert self.value into a string with brackets equivalent to tuple format.
# Cannot convert straight to tuple as single element tuples contain a
# trailing comma which Python ICAT/JPQL doesn't accept
self.value = str(self.value).replace("[", "(").replace("]", ")")

# DataGateway Search can send requests with blank lists. Adding NULL to the
# filter prevents the API from returning a 500. An empty list will be
# returned instead, equivalent to the DB backend
if self.value == "()":
self.value = "(NULL)"

where_filter = self.create_condition(self.field, "not in", self.value)
elif self.operation == "between":
where_filter = self.create_condition(
self.field, "between", f"'{self.value[0]}' and '{self.value[1]}'",
)
elif self.operation == "regexp":
where_filter = self.create_condition(self.field, "regexp", self.value)
else:
raise FilterError(f"Bad operation given to where filter: {self.operation}")

Expand Down Expand Up @@ -116,8 +135,7 @@ def create_condition(attribute_name, operator, value):
# distinct filter is used in a request
jpql_value = (
f"{value}"
if operator == "in"
or operator == "!="
if operator in ("in", "not in", "!=", "between")
or str(value).startswith("UPPER")
or "o." in str(value)
else f"'{value}'"
Expand Down
Loading

0 comments on commit e0b2d95

Please sign in to comment.