Skip to content

Commit

Permalink
Merge branch 'master' into Allow-the-configuration-of-authentication-…
Browse files Browse the repository at this point in the history
…mechanisms-#350
  • Loading branch information
MRichards99 committed Mar 29, 2022
2 parents 1c30f2f + c6aa15e commit 8761800
Show file tree
Hide file tree
Showing 10 changed files with 134 additions and 76 deletions.
22 changes: 18 additions & 4 deletions .github/workflows/ci-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ jobs:

# Prep for running the playbook
- name: Create hosts file
run: echo -e "[icatdb-minimal-hosts]\nlocalhost ansible_connection=local" > icat-ansible/hosts
run: echo -e "[icatdb_minimal_hosts]\nlocalhost ansible_connection=local" > icat-ansible/hosts
- name: Prepare vault pass
run: echo -e "icattravispw" > icat-ansible/vault_pass.txt
- name: Move vault to directory it'll get detected by Ansible
Expand All @@ -54,10 +54,17 @@ jobs:
- name: Change hostname to localhost
run: sudo hostname -b localhost

# Remove existing MySQL installation so it doesn't interfere with GitHub Actions
- name: Remove existing mysql
run: |
sudo apparmor_parser -R /etc/apparmor.d/usr.sbin.mysqld
sudo apt-get remove --purge "mysql*"
sudo rm -rf /var/lib/mysql* /etc/mysql
# Create local instance of ICAT
- name: Run ICAT Ansible Playbook
run: |
ansible-playbook icat-ansible/icatdb-minimal-hosts.yml -i icat-ansible/hosts --vault-password-file icat-ansible/vault_pass.txt -vv
ansible-playbook icat-ansible/icatdb_minimal_hosts.yml -i icat-ansible/hosts --vault-password-file icat-ansible/vault_pass.txt -vv
# rootUserNames needs editing as anon/anon is used in search API and required to pass endpoint tests
- name: Add anon user to rootUserNames
Expand Down Expand Up @@ -202,7 +209,7 @@ jobs:

# Prep for running the playbook
- name: Create hosts file
run: echo -e "[icatdb-minimal-hosts]\nlocalhost ansible_connection=local" > icat-ansible/hosts
run: echo -e "[icatdb_minimal_hosts]\nlocalhost ansible_connection=local" > icat-ansible/hosts
- name: Prepare vault pass
run: echo -e "icattravispw" > icat-ansible/vault_pass.txt
- name: Move vault to directory it'll get detected by Ansible
Expand All @@ -215,10 +222,17 @@ jobs:
- name: Change hostname to localhost
run: sudo hostname -b localhost

# Remove existing MySQL installation so it doesn't interfere with GitHub Actions
- name: Remove existing mysql
run: |
sudo apparmor_parser -R /etc/apparmor.d/usr.sbin.mysqld
sudo apt-get remove --purge "mysql*"
sudo rm -rf /var/lib/mysql* /etc/mysql
# Create local instance of ICAT
- name: Run ICAT Ansible Playbook
run: |
ansible-playbook icat-ansible/icatdb-minimal-hosts.yml -i icat-ansible/hosts --vault-password-file icat-ansible/vault_pass.txt -vv
ansible-playbook icat-ansible/icatdb_minimal_hosts.yml -i icat-ansible/hosts --vault-password-file icat-ansible/vault_pass.txt -vv
- name: Checkout DataGateway API
uses: actions/checkout@v2
Expand Down
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

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

## v4.3.0 (2022-03-18)
### Feature
* Allow multiple datetime formats to be used when filtering in search API #338 ([`f20ad24`](https://github.com/ral-facilities/datagateway-api/commit/f20ad24886f85bb3ba70abfd29352b1a7e5c58ff))

### Fix
* Output datetime data in the same format as SciCat #338 ([`4596db9`](https://github.com/ral-facilities/datagateway-api/commit/4596db98808f35488aca86667aa26811d777b58e))

## v4.2.0 (2022-02-28)
### Feature
* Create openapi endpoint for Search API #281 ([`412458c`](https://github.com/ral-facilities/datagateway-api/commit/412458cc4cc73230db0115bdbfdfe6ac815d42c1))
Expand Down
14 changes: 14 additions & 0 deletions datagateway_api/src/search_api/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
PythonICATSkipFilter,
PythonICATWhereFilter,
)
from datagateway_api.src.search_api.models import PaNOSCAttribute
from datagateway_api.src.search_api.panosc_mappings import mappings
from datagateway_api.src.search_api.query import SearchAPIQuery

Expand All @@ -19,6 +20,19 @@ class SearchAPIWhereFilter(PythonICATWhereFilter):
def __init__(self, field, value, operation, search_api_query=None):
self.search_api_query = search_api_query
super().__init__(field, value, operation)

# Detect various datetime formats and convert them into a format that ICAT can
# understand
if (
self.field in PaNOSCAttribute._datetime_field_names
and isinstance(self.value, str)
and DateHandler.is_str_a_date(self.value)
):
value_datetime = DateHandler.str_to_datetime_object(value)
str_datetime = DateHandler.datetime_object_to_str(value_datetime)
# +/- need to be removed so the format works when querying ICAT
self.value = str_datetime.replace("+", " ").replace("-", " ")

log.info("SearchAPIWhereFilter created: %s", repr(self))

def apply_filter(self, query):
Expand Down
28 changes: 24 additions & 4 deletions datagateway_api/src/search_api/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from typing import ClassVar, List, Optional, Union

from pydantic import BaseModel, Field, root_validator, ValidationError, validator
from pydantic.datetime_parse import parse_datetime
from pydantic.error_wrappers import ErrorWrapper

from datagateway_api.src.search_api.panosc_mappings import mappings
Expand Down Expand Up @@ -44,7 +45,26 @@ def _get_icat_field_value(icat_field_name, icat_data):
return icat_data


class SearchAPIDatetime(datetime):
"""
An alternative datetime class so datetimes can be outputted in a different format to
DataGateway API
"""

@classmethod
def __get_validators__(cls):
# Use default pydantic behaviour as well as behaviour defined in this class
yield parse_datetime
yield cls.use_search_api_format

@classmethod
def use_search_api_format(cls, v):
return v.strftime("%Y-%m-%dT%H:%M:%S.%f")[:-3] + "Z"


class PaNOSCAttribute(ABC, BaseModel):
_datetime_field_names = ["creationDate", "startDate", "endDate", "releaseDate"]

@classmethod
@abstractmethod
def from_icat(cls, icat_data, required_related_fields): # noqa: B902, N805
Expand Down Expand Up @@ -178,7 +198,7 @@ class Dataset(PaNOSCAttribute):
pid: str
title: str
is_public: bool = Field(alias="isPublic")
creation_date: datetime = Field(alias="creationDate")
creation_date: SearchAPIDatetime = Field(alias="creationDate")
size: Optional[int] = None

documents: List["Document"] = []
Expand Down Expand Up @@ -218,9 +238,9 @@ class Document(PaNOSCAttribute):
title: str
summary: Optional[str] = None
doi: Optional[str] = None
start_date: Optional[datetime] = Field(None, alias="startDate")
end_date: Optional[datetime] = Field(None, alias="endDate")
release_date: Optional[datetime] = Field(None, alias="releaseDate")
start_date: Optional[SearchAPIDatetime] = Field(None, alias="startDate")
end_date: Optional[SearchAPIDatetime] = Field(None, alias="endDate")
release_date: Optional[SearchAPIDatetime] = Field(None, alias="releaseDate")
license_: Optional[str] = Field(None, alias="license")
keywords: Optional[List[str]] = []

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "datagateway-api"
version = "4.2.0"
version = "4.3.0"
description = "ICAT API to interface with the DataGateway"
license = "Apache-2.0"
readme = "README.md"
Expand Down
2 changes: 1 addition & 1 deletion test/search_api/endpoints/test_get_dataset_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ class TestSearchAPIGetDatasetFilesEndpoint:
"pid": "0-8401-1070-7",
"title": "DATASET 2",
"isPublic": True,
"creationDate": "2013-04-01T10:56:52+00:00",
"creationDate": "2013-04-01T10:56:52.000Z",
"size": None,
"documents": [],
"techniques": [],
Expand Down
44 changes: 22 additions & 22 deletions test/search_api/endpoints/test_get_entity_by_pid.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class TestSearchAPIGetByPIDEndpoint:
"pid": "0-8401-1070-7",
"title": "DATASET 2",
"isPublic": True,
"creationDate": "2013-04-01T10:56:52+00:00",
"creationDate": "2013-04-01T10:56:52.000Z",
"size": None,
"documents": [],
"techniques": [],
Expand All @@ -38,9 +38,9 @@ class TestSearchAPIGetByPIDEndpoint:
"summary": "Season identify professor happen third. Beat"
" professional blue clear style have. Light final summer.",
"doi": "0-449-78690-0",
"startDate": "2000-04-03T00:00:00+00:00",
"endDate": "2000-07-09T00:00:00+00:00",
"releaseDate": "2000-07-05T00:00:00+00:00",
"startDate": "2000-04-03T00:00:00.000Z",
"endDate": "2000-07-09T00:00:00.000Z",
"releaseDate": "2000-07-05T00:00:00.000Z",
"license": None,
"keywords": [
"read123",
Expand Down Expand Up @@ -148,7 +148,7 @@ class TestSearchAPIGetByPIDEndpoint:
"pid": "0-8401-1070-7",
"title": "DATASET 2",
"isPublic": True,
"creationDate": "2013-04-01T10:56:52+00:00",
"creationDate": "2013-04-01T10:56:52.000Z",
"size": None,
"documents": [
{
Expand All @@ -161,9 +161,9 @@ class TestSearchAPIGetByPIDEndpoint:
"\nPurpose study usually gas think. Machine world doctor"
" rise be college treat.",
"doi": "0-9729806-3-6",
"startDate": "2000-06-04T00:00:00+00:00",
"endDate": "2000-09-14T00:00:00+00:00",
"releaseDate": "2000-02-10T00:00:00+00:00",
"startDate": "2000-06-04T00:00:00.000Z",
"endDate": "2000-09-14T00:00:00.000Z",
"releaseDate": "2000-02-10T00:00:00.000Z",
"license": None,
"keywords": [
"later868",
Expand Down Expand Up @@ -262,9 +262,9 @@ class TestSearchAPIGetByPIDEndpoint:
"summary": "Season identify professor happen third. Beat"
" professional blue clear style have. Light final summer.",
"doi": "0-449-78690-0",
"startDate": "2000-04-03T00:00:00+00:00",
"endDate": "2000-07-09T00:00:00+00:00",
"releaseDate": "2000-07-05T00:00:00+00:00",
"startDate": "2000-04-03T00:00:00.000Z",
"endDate": "2000-07-09T00:00:00.000Z",
"releaseDate": "2000-07-05T00:00:00.000Z",
"license": None,
"keywords": [
"read123",
Expand Down Expand Up @@ -351,7 +351,7 @@ class TestSearchAPIGetByPIDEndpoint:
"pid": "0-449-78690-0",
"title": "DATASET 1",
"isPublic": True,
"creationDate": "2002-11-27T06:20:36+00:00",
"creationDate": "2002-11-27T06:20:36.000Z",
"size": None,
"documents": [],
"techniques": [],
Expand All @@ -364,7 +364,7 @@ class TestSearchAPIGetByPIDEndpoint:
"pid": "0-353-84629-5",
"title": "DATASET 241",
"isPublic": True,
"creationDate": "2006-11-21T17:10:42+00:00",
"creationDate": "2006-11-21T17:10:42.000Z",
"size": None,
"documents": [],
"techniques": [],
Expand Down Expand Up @@ -414,7 +414,7 @@ class TestSearchAPIGetByPIDEndpoint:
"pid": "0-8401-1070-7",
"title": "DATASET 2",
"isPublic": True,
"creationDate": "2013-04-01T10:56:52+00:00",
"creationDate": "2013-04-01T10:56:52.000Z",
"size": None,
"documents": [
{
Expand All @@ -427,9 +427,9 @@ class TestSearchAPIGetByPIDEndpoint:
"\nPurpose study usually gas think. Machine world doctor "
"rise be college treat.",
"doi": "0-9729806-3-6",
"startDate": "2000-06-04T00:00:00+00:00",
"endDate": "2000-09-14T00:00:00+00:00",
"releaseDate": "2000-02-10T00:00:00+00:00",
"startDate": "2000-06-04T00:00:00.000Z",
"endDate": "2000-09-14T00:00:00.000Z",
"releaseDate": "2000-02-10T00:00:00.000Z",
"license": None,
"keywords": [
"later868",
Expand Down Expand Up @@ -943,9 +943,9 @@ class TestSearchAPIGetByPIDEndpoint:
"summary": "Season identify professor happen third. Beat"
" professional blue clear style have. Light final summer.",
"doi": "0-449-78690-0",
"startDate": "2000-04-03T00:00:00+00:00",
"endDate": "2000-07-09T00:00:00+00:00",
"releaseDate": "2000-07-05T00:00:00+00:00",
"startDate": "2000-04-03T00:00:00.000Z",
"endDate": "2000-07-09T00:00:00.000Z",
"releaseDate": "2000-07-05T00:00:00.000Z",
"license": None,
"keywords": [
"read123",
Expand Down Expand Up @@ -1032,7 +1032,7 @@ class TestSearchAPIGetByPIDEndpoint:
"pid": "0-449-78690-0",
"title": "DATASET 1",
"isPublic": True,
"creationDate": "2002-11-27T06:20:36+00:00",
"creationDate": "2002-11-27T06:20:36.000Z",
"size": None,
"documents": [],
"techniques": [],
Expand All @@ -1045,7 +1045,7 @@ class TestSearchAPIGetByPIDEndpoint:
"pid": "0-353-84629-5",
"title": "DATASET 241",
"isPublic": True,
"creationDate": "2006-11-21T17:10:42+00:00",
"creationDate": "2006-11-21T17:10:42.000Z",
"size": None,
"documents": [],
"techniques": [],
Expand Down
Loading

0 comments on commit 8761800

Please sign in to comment.