SHAFI commited on
Commit
be4faf2
·
1 Parent(s): e7f36c9

HOTFIX: Fix date_parser to handle Pydantic Article models

Browse files

TypeError: 'Article' object does not support item assignment
- Pydantic models are immutable, can't use article['key'] = value
- Now converts to dict first, normalizes, returns dict
- Handles both Pydantic v1/v2 models and regular dicts

Files changed (1) hide show
  1. app/utils/date_parser.py +36 -7
app/utils/date_parser.py CHANGED
@@ -47,19 +47,48 @@ def parse_date_to_iso(date_str: str) -> str:
47
  return datetime.now(timezone.utc).isoformat().replace('+00:00', 'Z')
48
 
49
 
50
- def normalize_article_date(article: dict) -> dict:
51
  """
52
- Normalize the publishedAt field in an article dict
53
 
54
- Modifies article in-place and returns it.
 
 
 
 
 
 
55
  """
56
- if 'publishedAt' in article:
57
- article['publishedAt'] = parse_date_to_iso(article['publishedAt'])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58
  else:
59
  # If missing, use current time
60
- article['publishedAt'] = datetime.now(timezone.utc).isoformat().replace('+00:00', 'Z')
61
 
62
- return article
63
 
64
 
65
  def validate_date_format(date_str: str) -> bool:
 
47
  return datetime.now(timezone.utc).isoformat().replace('+00:00', 'Z')
48
 
49
 
50
+ def normalize_article_date(article):
51
  """
52
+ Normalize the publishedAt field in an article
53
 
54
+ HOTFIX (2026-01-23): Now handles both Pydantic Article models AND dicts
55
+
56
+ Args:
57
+ article: Article model or dict
58
+
59
+ Returns:
60
+ dict with normalized publishedAt field
61
  """
62
+ # HOTFIX: Convert Pydantic model to dict if needed
63
+ if hasattr(article, 'model_dump'):
64
+ # It's a Pydantic v2 model
65
+ article_dict = article.model_dump()
66
+ elif hasattr(article, 'dict'):
67
+ # It's a Pydantic v1 model
68
+ article_dict = article.dict()
69
+ elif isinstance(article, dict):
70
+ # Already a dict - create a copy to avoid mutating original
71
+ article_dict = article.copy()
72
+ else:
73
+ # Unknown type - try to convert to dict
74
+ article_dict = dict(article)
75
+
76
+ # Normalize the date
77
+ if 'publishedAt' in article_dict:
78
+ published_at = article_dict['publishedAt']
79
+ # Handle datetime objects
80
+ if isinstance(published_at, datetime):
81
+ article_dict['publishedAt'] = published_at.astimezone(timezone.utc).isoformat().replace('+00:00', 'Z')
82
+ elif isinstance(published_at, str):
83
+ article_dict['publishedAt'] = parse_date_to_iso(published_at)
84
+ else:
85
+ # Unknown type, use current time
86
+ article_dict['publishedAt'] = datetime.now(timezone.utc).isoformat().replace('+00:00', 'Z')
87
  else:
88
  # If missing, use current time
89
+ article_dict['publishedAt'] = datetime.now(timezone.utc).isoformat().replace('+00:00', 'Z')
90
 
91
+ return article_dict
92
 
93
 
94
  def validate_date_format(date_str: str) -> bool: