"""Date range validation functions.""" from oto import response as oto_response from analytics.consts import error from analytics.utils import date as analytics_date def validate_date_range(from_date, to_date, days_threshold, date_format='%Y-%m-%d'): """Validate date range to be not greater than threshold provided. Args: from_date (str): start of the date range. to_date (str): end of the date ranege. days_threshold (int): threshold number of days in the date range to validate date_format (str) optional: string representation of date format Returns: Response: the response object. """ if days_threshold <= 0: return oto_response.create_error_response( code=error.VALIDATION_ERROR_CODE, message=error.INVALID_DAYS_THRESHOLD_MESSAGE) from_date = analytics_date.get(from_date, date_format) to_date = analytics_date.get(to_date, date_format) if not all((from_date, to_date)): return oto_response.create_error_response( code=error.VALIDATION_ERROR_CODE, message=error.INVALID_DATE_RANGE_FORMAT_MESSAGE) delta = to_date - from_date if not 0 < delta.days <= days_threshold: return oto_response.create_error_response( code=error.VALIDATION_ERROR_CODE, message=error.INVALID_DATE_RANGE_VALUES_MESSAGE) return oto_response.Response()