Hamdy005 commited on
Commit
bffc068
·
1 Parent(s): 350f1f4

fix: improve PDF print layout and normalize 'topic' source type handling

Browse files
Files changed (1) hide show
  1. store.py +27 -5
store.py CHANGED
@@ -143,11 +143,19 @@ def list_materials(user_id: str) -> list[dict]:
143
  if client is not None:
144
  try:
145
  result = _robust_execute(client.table("materials").select("*").eq("user_id", user_id).order("created_at"))
146
- return list(reversed(result.data))
 
 
 
 
147
  except Exception:
148
  pass
149
  records = list(_in_memory.get("materials", {}).values())
150
- return list(reversed([r for r in records if r.get("user_id") == user_id]))
 
 
 
 
151
 
152
 
153
  def is_title_taken(title: str, exclude_id: Optional[str] = None, user_id: Optional[str] = None) -> bool:
@@ -198,14 +206,23 @@ def create_material(user_id: str, source_type: str, title: str,
198
  except Exception as e:
199
  logger.error(f"Failed to ensure profile for user {user_id}: {e}")
200
 
201
- data = {"user_id": user_id, "source_type": source_type, "title": title, "status": "pending",
 
 
 
 
 
202
  "created_at": now, "updated_at": now}
203
  if file_path:
204
  data["file_path"] = file_path
205
  if url:
206
  data["url"] = url
207
  result = _robust_execute(_table_supabase("materials").insert(data))
208
- return result.data[0]
 
 
 
 
209
 
210
 
211
  def update_material_status(material_id: str, status: str,
@@ -220,7 +237,12 @@ def get_material(material_id: str) -> Optional[dict]:
220
  if material_id.startswith("temp-"):
221
  return None
222
  result = _robust_execute(_table_supabase("materials").select("*").eq("id", material_id))
223
- return result.data[0] if result.data else None
 
 
 
 
 
224
 
225
 
226
  def rename_material(material_id: str, title: str):
 
143
  if client is not None:
144
  try:
145
  result = _robust_execute(client.table("materials").select("*").eq("user_id", user_id).order("created_at"))
146
+ data = list(reversed(result.data))
147
+ for r in data:
148
+ if r.get("source_type") == "url" and not r.get("url"):
149
+ r["source_type"] = "topic"
150
+ return data
151
  except Exception:
152
  pass
153
  records = list(_in_memory.get("materials", {}).values())
154
+ data = list(reversed([r for r in records if r.get("user_id") == user_id]))
155
+ for r in data:
156
+ if r.get("source_type") == "url" and not r.get("url"):
157
+ r["source_type"] = "topic"
158
+ return data
159
 
160
 
161
  def is_title_taken(title: str, exclude_id: Optional[str] = None, user_id: Optional[str] = None) -> bool:
 
206
  except Exception as e:
207
  logger.error(f"Failed to ensure profile for user {user_id}: {e}")
208
 
209
+ # Workaround for DB check constraint that restricts source_type to 'pdf' or 'url'
210
+ actual_source_type = source_type
211
+ if source_type == "topic":
212
+ actual_source_type = "url"
213
+
214
+ data = {"user_id": user_id, "source_type": actual_source_type, "title": title, "status": "pending",
215
  "created_at": now, "updated_at": now}
216
  if file_path:
217
  data["file_path"] = file_path
218
  if url:
219
  data["url"] = url
220
  result = _robust_execute(_table_supabase("materials").insert(data))
221
+
222
+ ret_data = result.data[0]
223
+ if ret_data.get("source_type") == "url" and not ret_data.get("url"):
224
+ ret_data["source_type"] = "topic"
225
+ return ret_data
226
 
227
 
228
  def update_material_status(material_id: str, status: str,
 
237
  if material_id.startswith("temp-"):
238
  return None
239
  result = _robust_execute(_table_supabase("materials").select("*").eq("id", material_id))
240
+ if result.data:
241
+ data = result.data[0]
242
+ if data.get("source_type") == "url" and not data.get("url"):
243
+ data["source_type"] = "topic"
244
+ return data
245
+ return None
246
 
247
 
248
  def rename_material(material_id: str, title: str):