OCR Template mapping done Now lets work on user and permission modules

This commit is contained in:
2026-07-14 16:49:54 +05:30
parent c725014788
commit b812d3cb82
4 changed files with 143 additions and 4 deletions

View File

@@ -1,5 +1,7 @@
import re
from typing import Optional, Union
from typing import Optional
from datetime import datetime
from dateutil import parser
def sanitize_amount(text: str) -> Optional[float]:
"""
@@ -31,3 +33,30 @@ def sanitize_amount(text: str) -> Optional[float]:
return float(cleaned_text)
except ValueError:
return None
def sanitize_date(text: str, field_type: str = 'DATE') -> Optional[str]:
"""
Sanitizes a string representing a date or datetime and converts it to ISO 8601 format.
Args:
text (str): The raw extracted text from the document.
field_type (str): 'DATE' or 'DATETIME'. Determines the output format.
Returns:
Optional[str]: The sanitized date string in ISO format, or None if it could not be parsed.
"""
if not text:
return None
try:
# dateutil.parser is very robust at handling formats like "Dec 11, 2020", "11/12/2020", etc.
# fuzzy=True allows it to ignore extra words/characters around the date
parsed_date = parser.parse(text, fuzzy=True)
if field_type == 'DATETIME':
return parsed_date.strftime('%Y-%m-%dT%H:%M:%S')
else:
return parsed_date.strftime('%Y-%m-%d')
except (ValueError, TypeError, OverflowError):
return None