From 0a3e1524f8913020ed001e07ea58d27ada74aebc Mon Sep 17 00:00:00 2001 From: kaditya97 Date: Tue, 12 Dec 2023 21:53:31 +0545 Subject: [PATCH] fix: interval validation and datetime in utc timezone --- backend/api/projects/resources.py | 7 +++++-- backend/services/project_service.py | 4 ++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/backend/api/projects/resources.py b/backend/api/projects/resources.py index 8ecc02eb85..8f519c20cf 100644 --- a/backend/api/projects/resources.py +++ b/backend/api/projects/resources.py @@ -1185,7 +1185,7 @@ def get(self): default: Token sessionTokenHere== - name: interval in: path - description: Time interval to get active project + description: Time interval in hours to get active project required: false type: integer default: 24 @@ -1197,6 +1197,9 @@ def get(self): 500: description: Internal Server Error """ - interval = int(request.args.get("interval", 24)) + interval = request.args.get("interval", "24") + if not interval.isdigit(): + return {"Error": "Interval must be a number greater than 0"}, 400 + interval = int(interval) projects_dto = ProjectService.get_active_projects(interval) return projects_dto, 200 diff --git a/backend/services/project_service.py b/backend/services/project_service.py index e740eb9e0a..e8c5b3f318 100644 --- a/backend/services/project_service.py +++ b/backend/services/project_service.py @@ -2,7 +2,7 @@ from cachetools import TTLCache, cached from flask import current_app import geojson -from datetime import datetime, timedelta +from datetime import datetime, timedelta, timezone from backend.exceptions import NotFound from backend.models.dtos.mapping_dto import TaskDTOs @@ -620,7 +620,7 @@ def send_email_on_project_progress(project_id): @staticmethod def get_active_projects(interval): - action_date = datetime.now() - timedelta(hours=interval) + action_date = datetime.now(timezone.utc) - timedelta(hours=interval) result = ( TaskHistory.query.with_entities(TaskHistory.project_id) .distinct()