From 1021c802f84e298a8a61dc6563804ce8a6eade72 Mon Sep 17 00:00:00 2001 From: Viktor Bozhinov Date: Tue, 8 Feb 2022 09:42:14 +0000 Subject: [PATCH] determine public data based on config value #312 --- datagateway_api/src/search_api/models.py | 13 +++++++++---- .../src/search_api/query_filter_factory.py | 9 ++++++--- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/datagateway_api/src/search_api/models.py b/datagateway_api/src/search_api/models.py index f9c8ae9a..0ce3187a 100644 --- a/datagateway_api/src/search_api/models.py +++ b/datagateway_api/src/search_api/models.py @@ -7,6 +7,7 @@ from pydantic import BaseModel, Field, ValidationError, validator from pydantic.error_wrappers import ErrorWrapper +from datagateway_api.src.common.config import Config from datagateway_api.src.common.date_handler import DateHandler from datagateway_api.src.search_api.panosc_mappings import mappings @@ -197,8 +198,10 @@ def set_is_public(cls, value): # noqa: B902, N805 creation_date = DateHandler.str_to_datetime_object(value) current_datetime = datetime.now(timezone.utc) - three_years_ago = current_datetime - relativedelta(years=3) - return creation_date < three_years_ago + rd = relativedelta( + years=Config.config.search_api.num_of_years_determining_public_data, + ) + return creation_date < (current_datetime - rd) @classmethod def from_icat(cls, icat_data, required_related_fields): @@ -236,8 +239,10 @@ def set_is_public(cls, value): # noqa: B902, N805 creation_date = DateHandler.str_to_datetime_object(value) current_datetime = datetime.now(timezone.utc) - three_years_ago = current_datetime - relativedelta(years=3) - return creation_date < three_years_ago + rd = relativedelta( + years=Config.config.search_api.num_of_years_determining_public_data, + ) + return creation_date < (current_datetime - rd) @classmethod def from_icat(cls, icat_data, required_related_fields): diff --git a/datagateway_api/src/search_api/query_filter_factory.py b/datagateway_api/src/search_api/query_filter_factory.py index f0dd05d2..0086b7d1 100644 --- a/datagateway_api/src/search_api/query_filter_factory.py +++ b/datagateway_api/src/search_api/query_filter_factory.py @@ -4,6 +4,7 @@ from dateutil.relativedelta import relativedelta from datagateway_api.src.common.base_query_filter_factory import QueryFilterFactory +from datagateway_api.src.common.config import Config from datagateway_api.src.common.exceptions import FilterError, SearchAPIError from datagateway_api.src.search_api.filters import ( SearchAPIIncludeFilter, @@ -361,16 +362,18 @@ def convert_is_public_field_value_and_operation(value, operation): so that all Datasets older than 3 years (which ISIS considers public) are returned. """ - current_datetime = datetime.now(timezone.utc) - three_years_ago = current_datetime - relativedelta(years=3) value = not value if operation == "neq" else value if value is True: operation = "lt" else: operation = "gt" + current_datetime = datetime.now(timezone.utc) + rd = relativedelta( + years=Config.config.search_api.num_of_years_determining_public_data, + ) # The timezone part has a plus sign so replacing # with a blank space to avoid issues - value = str(three_years_ago).replace("+", " ") + value = str(current_datetime - rd).replace("+", " ") return value, operation