diff --git a/insights/sources/agents/clients.py b/insights/sources/agents/clients.py index 83c4417..7604f5c 100644 --- a/insights/sources/agents/clients.py +++ b/insights/sources/agents/clients.py @@ -15,9 +15,9 @@ def __init__(self, project) -> None: def list(self, query_filters: dict): if query_filters.get("created_on__gte", None): - query_filters["start_date"] = query_filters.pop("created_on__gte")[0] + query_filters["start_date"] = query_filters.pop("created_on__gte") if query_filters.get("created_on__lte", None): - query_filters["end_date"] = query_filters.pop("created_on__lte")[0] + query_filters["end_date"] = query_filters.pop("created_on__lte") response = requests.get( url=self.url, headers=self.headers, params=query_filters diff --git a/insights/widgets/usecases/get_source_data.py b/insights/widgets/usecases/get_source_data.py index 3f26c90..5b1f3dd 100644 --- a/insights/widgets/usecases/get_source_data.py +++ b/insights/widgets/usecases/get_source_data.py @@ -1,8 +1,22 @@ +from datetime import datetime, time + +import pytz + from insights.projects.parsers import parse_dict_to_json from insights.shared.viewsets import get_source from insights.widgets.models import Widget +def apply_timezone_to_filters(default_filters, project_timezone_str): + project_timezone = pytz.timezone(project_timezone_str) + for key in default_filters.keys(): + if key.endswith("__gte") or key.endswith("__lte"): + date_str = default_filters[key][0] + date_obj = datetime.strptime(date_str, "%Y-%m-%d") + date_obj_with_tz = project_timezone.localize(date_obj) + default_filters[key] = date_obj_with_tz.isoformat() + + def get_source_data_from_widget( widget: Widget, is_report: bool = False, filters: dict = {}, user_email: str = "" ): @@ -19,13 +33,17 @@ def get_source_data_from_widget( default_filters, operation, op_field, limit = widget.source_config( sub_widget=filters.pop("slug", [None])[0] - ) # implement a dynamic handler for each widget + ) + + default_filters.update(filters) + + project_timezone = widget.project.timezone + apply_timezone_to_filters(default_filters, project_timezone) - filters.update(default_filters) if operation == "list": - tags = filters.pop("tags", [None])[0] + tags = default_filters.pop("tags", [None])[0] if tags: - filters["tags"] = tags.split(",") + default_filters["tags"] = tags.split(",") if op_field: query_kwargs["op_field"] = op_field @@ -33,7 +51,7 @@ def get_source_data_from_widget( query_kwargs["limit"] = limit serialized_source = SourceQuery.execute( - filters=filters, + filters=default_filters, operation=operation, parser=parse_dict_to_json, project=widget.project,