Skip to content

Commit

Permalink
Merge branch 'master' into feature/distinct-filter-related-entities-#223
Browse files Browse the repository at this point in the history
  • Loading branch information
MRichards99 committed May 14, 2021
2 parents f261f7b + 5901e03 commit bfa8c84
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 109 deletions.
19 changes: 11 additions & 8 deletions datagateway_api/common/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,24 +46,27 @@ def _check_config_items_exist(self):
"""
A function to check that all config options exist before getting too far into
the setup of the API. This check takes the backend into account, meaning only
the config options for the backend used is required
Config options used for testing are not checked here as they should only be used
during tests, not in the typical running of the API
the config options for the backend used is required.
If a config option is missing, this will be picked up in `get_config_value()` by
exiting the application
Config options used for testing are not checked here as they should only be used
during tests, not in the typical running of the API.
Some options used when running the API (host, debug_mode etc.) aren't mandatory
when running the API in production (these options aren't used in the `wsgi.py`
entrypoint). As a result, they're not present in `config_keys`. However, they
are required when using `main.py` as an entrypoint. In any case of these
specific missing config options when using that entrypoint, they are checked at
API startup so any missing options will be caught quickly.
"""
# These keys are non-backend specific and therefore are mandatory for all uses
config_keys = [
APIConfigOptions.BACKEND,
APIConfigOptions.DEBUG_MODE,
APIConfigOptions.FLASK_RELOADER,
APIConfigOptions.GENERATE_SWAGGER,
APIConfigOptions.HOST,
APIConfigOptions.LOG_LEVEL,
APIConfigOptions.LOG_LOCATION,
APIConfigOptions.PORT,
]

if self.get_config_value(APIConfigOptions.BACKEND) == "python_icat":
Expand Down
4 changes: 3 additions & 1 deletion datagateway_api/common/constants.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from datetime import datetime

from dateutil.tz import tzlocal


class Constants:
PYTHON_ICAT_DISTNCT_CONDITION = "!= null"
TEST_MOD_CREATE_DATETIME = datetime(2000, 1, 1)
TEST_MOD_CREATE_DATETIME = datetime(2000, 1, 1, tzinfo=tzlocal())
6 changes: 5 additions & 1 deletion datagateway_api/common/database/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from decimal import Decimal
import enum

from dateutil.tz import tzlocal
from sqlalchemy import (
BigInteger,
Boolean,
Expand All @@ -20,6 +21,7 @@
from sqlalchemy.orm import relationship
from sqlalchemy.orm.collections import InstrumentedList

from datagateway_api.common.date_handler import DateHandler
from datagateway_api.common.exceptions import DatabaseError, FilterError

Base = declarative_base()
Expand Down Expand Up @@ -78,7 +80,9 @@ def _make_serializable(self, field):
:return: The converted field
"""
if isinstance(field, datetime):
return str(field)
# Add timezone info to match ICAT backend
field = field.replace(tzinfo=tzlocal())
return DateHandler.datetime_object_to_str(field)
elif isinstance(field, Decimal):
return float(field)
else:
Expand Down
3 changes: 2 additions & 1 deletion datagateway_api/common/icat/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import logging

from cachetools import cached
from dateutil.tz import tzlocal
from icat.entities import getTypeMap
from icat.exception import (
ICATInternalError,
Expand Down Expand Up @@ -114,7 +115,7 @@ def get_session_details_helper(client):
"""
session_time_remaining = client.getRemainingMinutes()
session_expiry_time = (
datetime.now() + timedelta(minutes=session_time_remaining)
datetime.now(tzlocal()) + timedelta(minutes=session_time_remaining)
).replace(microsecond=0)
username = client.getUserName()

Expand Down
181 changes: 91 additions & 90 deletions poetry.lock

Large diffs are not rendered by default.

11 changes: 6 additions & 5 deletions test/icat/conftest.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from datetime import datetime, timezone
from datetime import datetime
import uuid

from dateutil.tz import tzlocal
from flask import Flask
from icat.client import Client
from icat.exception import ICATNoObjectError
Expand Down Expand Up @@ -49,10 +50,10 @@ def create_investigation_test_data(client, num_entities=1):
f"Test data for the Python ICAT Backend on DataGateway API {i}"
)
investigation.startDate = datetime(
year=2020, month=1, day=4, hour=1, minute=1, second=1, tzinfo=timezone.utc,
year=2020, month=1, day=4, hour=1, minute=1, second=1, tzinfo=tzlocal(),
)
investigation.endDate = datetime(
year=2020, month=1, day=8, hour=1, minute=1, second=1, tzinfo=timezone.utc,
year=2020, month=1, day=8, hour=1, minute=1, second=1, tzinfo=tzlocal(),
)
# UUID visit ID means uniquesness constraint should always be met
investigation.visitId = str(uuid.uuid1())
Expand Down Expand Up @@ -112,10 +113,10 @@ def isis_specific_endpoint_data(icat_client):
facility_cycle = icat_client.new("facilityCycle")
facility_cycle.name = "Test cycle for DataGateway API testing"
facility_cycle.startDate = datetime(
year=2020, month=1, day=1, hour=1, minute=1, second=1, tzinfo=timezone.utc,
year=2020, month=1, day=1, hour=1, minute=1, second=1, tzinfo=tzlocal(),
)
facility_cycle.endDate = datetime(
year=2020, month=2, day=1, hour=1, minute=1, second=1, tzinfo=timezone.utc,
year=2020, month=2, day=1, hour=1, minute=1, second=1, tzinfo=tzlocal(),
)
facility_cycle.facility = icat_client.get("Facility", 1)
facility_cycle.create()
Expand Down
1 change: 1 addition & 0 deletions test/icat/test_query.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from datetime import datetime

from dateutil.tz import tzlocal
from icat.entity import Entity
import pytest

Expand Down
9 changes: 6 additions & 3 deletions test/icat/test_session_handling.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from datetime import datetime

from dateutil.tz import tzlocal
from icat.client import Client
import pytest

from datagateway_api.common.config import APIConfigOptions, config
from datagateway_api.common.date_handler import DateHandler
from datagateway_api.common.icat.filters import PythonICATWhereFilter


Expand All @@ -15,10 +17,11 @@ def test_get_valid_session_details(
"/sessions", headers=valid_icat_credentials_header,
)

session_expiry_datetime = datetime.strptime(
session_details.json["expireDateTime"], "%Y-%m-%d %H:%M:%S",
session_expiry_datetime = DateHandler.str_to_datetime_object(
session_details.json["expireDateTime"],
)
current_datetime = datetime.now()

current_datetime = datetime.now(tzlocal())
time_diff = abs(session_expiry_datetime - current_datetime)
time_diff_minutes = time_diff.seconds / 60

Expand Down

0 comments on commit bfa8c84

Please sign in to comment.