AtmoAssistant/app/utils.py

50 lines
1.1 KiB
Python
Raw Normal View History

2025-01-20 20:09:02 -05:00
from datetime import datetime, timedelta, timezone
import json
from urllib import parse
import logging
import re
import typing as t
import models
logger = logging.getLogger("gunicorn.error")
def str_none(x):
if x is None:
return ""
else:
return str(x)
def string_validator(input_str: str):
# Decode the input string
decoded_str = parse.unquote(input_str)
# Sanitize the string
sanitized = re.sub(r"[\s]", "", decoded_str)
sanitized = re.sub(r'[<>"\'%;]', "", sanitized)
# Check length of the string
if len(sanitized) < 1:
return None
return sanitized
def validate_data_presence(data: t.Dict[str, t.Any], keys: list[str]) -> bool:
"""
Validate that all given keys are present in the data.
Args:
data (Dict[str, Any]): The JSON data to be validated.
keys (list[str]): A list of keys to look for in the data.
Returns:
bool: If any key is missing, returns False. Otherwise, returns True.
"""
for key in keys:
if key not in data:
return False
return True