DawnC commited on
Commit
f9cdc52
1 Parent(s): 2e2bac6

Update history_manager.py

Browse files
Files changed (1) hide show
  1. history_manager.py +60 -20
history_manager.py CHANGED
@@ -29,54 +29,94 @@ class UserHistoryManager:
29
  print(traceback.format_exc())
30
 
31
 
32
- def save_history(self, user_preferences: dict = None, results: list = None, search_type: str = "criteria", description: str = None) -> bool:
33
- """
34
- Save search history with proper timestamp handling.
35
 
36
- Args:
37
- user_preferences: User's search preferences
38
- results: List of breed recommendations
39
- search_type: Type of search performed
40
- description: Search description if applicable
41
 
42
- Returns:
43
- bool: Success status of save operation
44
- """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
  try:
46
- # Initialize timezone and current time
 
 
 
 
 
 
 
47
  taipei_tz = pytz.timezone('Asia/Taipei')
48
  current_time = datetime.now(taipei_tz)
49
-
50
- # Create history entry with timestamp
51
  history_entry = {
52
  "timestamp": current_time.strftime("%Y-%m-%d %H:%M:%S"),
53
  "search_type": search_type,
54
  "results": results
55
  }
56
-
57
- # Add preferences to history entry if provided
58
  if user_preferences:
59
  history_entry["preferences"] = user_preferences
60
 
61
- # Read existing history
62
  with open(self.history_file, 'r', encoding='utf-8') as f:
63
  history = json.load(f)
64
 
65
- # Add new entry and maintain history limit
66
  history.append(history_entry)
67
- if len(history) > 20: # Keep last 20 entries
68
  history = history[-20:]
69
 
70
- # Save updated history
71
  with open(self.history_file, 'w', encoding='utf-8') as f:
72
  json.dump(history, f, ensure_ascii=False, indent=2)
73
 
 
74
  return True
75
 
76
  except Exception as e:
77
  print(f"Error saving history: {str(e)}")
 
78
  return False
79
 
 
80
  def get_history(self) -> list:
81
  """獲取搜尋歷史"""
82
  try:
 
29
  print(traceback.format_exc())
30
 
31
 
32
+ # def save_history(self, user_preferences: dict = None, results: list = None, search_type: str = "criteria", description: str = None) -> bool:
33
+ # """
34
+ # Save search history with proper timestamp handling.
35
 
36
+ # Args:
37
+ # user_preferences: User's search preferences
38
+ # results: List of breed recommendations
39
+ # search_type: Type of search performed
40
+ # description: Search description if applicable
41
 
42
+ # Returns:
43
+ # bool: Success status of save operation
44
+ # """
45
+ # try:
46
+ # # Initialize timezone and current time
47
+ # taipei_tz = pytz.timezone('Asia/Taipei')
48
+ # current_time = datetime.now(taipei_tz)
49
+
50
+ # # Create history entry with timestamp
51
+ # history_entry = {
52
+ # "timestamp": current_time.strftime("%Y-%m-%d %H:%M:%S"),
53
+ # "search_type": search_type,
54
+ # "results": results
55
+ # }
56
+
57
+ # # Add preferences to history entry if provided
58
+ # if user_preferences:
59
+ # history_entry["preferences"] = user_preferences
60
+
61
+ # # Read existing history
62
+ # with open(self.history_file, 'r', encoding='utf-8') as f:
63
+ # history = json.load(f)
64
+
65
+ # # Add new entry and maintain history limit
66
+ # history.append(history_entry)
67
+ # if len(history) > 20: # Keep last 20 entries
68
+ # history = history[-20:]
69
+
70
+ # # Save updated history
71
+ # with open(self.history_file, 'w', encoding='utf-8') as f:
72
+ # json.dump(history, f, ensure_ascii=False, indent=2)
73
+
74
+ # return True
75
+
76
+ # except Exception as e:
77
+ # print(f"Error saving history: {str(e)}")
78
+ # return False
79
+
80
+ def save_history(self, user_preferences: dict = None, results: list = None, search_type: str = "criteria", description: str = None) -> bool:
81
  try:
82
+ # 確保 results 格式正確
83
+ if results:
84
+ for result in results:
85
+ if not all(k in result for k in ('breed', 'overall_score')):
86
+ print(f"Invalid result entry detected: {result}")
87
+ raise ValueError("Each result must include 'breed' and 'overall_score'.")
88
+
89
+ # 原始保存邏輯保持不變
90
  taipei_tz = pytz.timezone('Asia/Taipei')
91
  current_time = datetime.now(taipei_tz)
92
+
 
93
  history_entry = {
94
  "timestamp": current_time.strftime("%Y-%m-%d %H:%M:%S"),
95
  "search_type": search_type,
96
  "results": results
97
  }
 
 
98
  if user_preferences:
99
  history_entry["preferences"] = user_preferences
100
 
 
101
  with open(self.history_file, 'r', encoding='utf-8') as f:
102
  history = json.load(f)
103
 
 
104
  history.append(history_entry)
105
+ if len(history) > 20: # 保留最近 20
106
  history = history[-20:]
107
 
 
108
  with open(self.history_file, 'w', encoding='utf-8') as f:
109
  json.dump(history, f, ensure_ascii=False, indent=2)
110
 
111
+ print("History saved successfully:", history_entry)
112
  return True
113
 
114
  except Exception as e:
115
  print(f"Error saving history: {str(e)}")
116
+ print(traceback.format_exc())
117
  return False
118
 
119
+
120
  def get_history(self) -> list:
121
  """獲取搜尋歷史"""
122
  try: