Skip to content

Commit

Permalink
Merge pull request #232 from ral-facilities/refactor/datehandler-db-b…
Browse files Browse the repository at this point in the history
…ackend-#225

Add Timezone Data to DB Backend
  • Loading branch information
MRichards99 authored May 11, 2021
2 parents f8b1136 + 9b2dc7d commit 827454b
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 13 deletions.
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
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
5 changes: 3 additions & 2 deletions test/icat/test_query.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from datetime import datetime, timezone
from datetime import datetime

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

Expand Down Expand Up @@ -364,7 +365,7 @@ def test_valid_get_distinct_attributes(self, icat_client):
hour=1,
minute=1,
second=1,
tzinfo=timezone.utc,
tzinfo=tzlocal(),
),
),
{"startDate": "2020-01-04 01:01:01+00:00"},
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 827454b

Please sign in to comment.