DawnC commited on
Commit
04b305f
1 Parent(s): ac129b1

Update history_manager.py

Browse files
Files changed (1) hide show
  1. history_manager.py +46 -5
history_manager.py CHANGED
@@ -70,11 +70,52 @@ class UserHistoryManager:
70
  # return False
71
 
72
  def save_history(self, user_preferences: dict = None, results: list = None, search_type: str = "criteria", description: str = None) -> bool:
73
- history_entry = {
74
- "timestamp": taipei_time.strftime("%Y-%m-%d %H:%M:%S"),
75
- "search_type": search_type,
76
- "results": results
77
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78
 
79
  def get_history(self) -> list:
80
  """獲取搜尋歷史"""
 
70
  # return False
71
 
72
  def save_history(self, user_preferences: dict = None, results: list = None, search_type: str = "criteria", description: str = None) -> bool:
73
+ """
74
+ Save search history with proper timestamp handling.
75
+
76
+ Args:
77
+ user_preferences: User's search preferences
78
+ results: List of breed recommendations
79
+ search_type: Type of search performed
80
+ description: Search description if applicable
81
+
82
+ Returns:
83
+ bool: Success status of save operation
84
+ """
85
+ try:
86
+ # Initialize timezone and current time
87
+ taipei_tz = pytz.timezone('Asia/Taipei')
88
+ current_time = datetime.now(taipei_tz)
89
+
90
+ # Create history entry with timestamp
91
+ history_entry = {
92
+ "timestamp": current_time.strftime("%Y-%m-%d %H:%M:%S"),
93
+ "search_type": search_type,
94
+ "results": results
95
+ }
96
+
97
+ # Add preferences to history entry if provided
98
+ if user_preferences:
99
+ history_entry["preferences"] = user_preferences
100
+
101
+ # Read existing history
102
+ with open(self.history_file, 'r', encoding='utf-8') as f:
103
+ history = json.load(f)
104
+
105
+ # Add new entry and maintain history limit
106
+ history.append(history_entry)
107
+ if len(history) > 20: # Keep last 20 entries
108
+ history = history[-20:]
109
+
110
+ # Save updated history
111
+ with open(self.history_file, 'w', encoding='utf-8') as f:
112
+ json.dump(history, f, ensure_ascii=False, indent=2)
113
+
114
+ return True
115
+
116
+ except Exception as e:
117
+ print(f"Error saving history: {str(e)}")
118
+ return False
119
 
120
  def get_history(self) -> list:
121
  """獲取搜尋歷史"""