Mazenbs commited on
Commit
1b21de5
·
verified ·
1 Parent(s): a7b79bc

Update supabase_utils.py

Browse files
Files changed (1) hide show
  1. supabase_utils.py +32 -22
supabase_utils.py CHANGED
@@ -53,33 +53,43 @@ def save_law_to_supabase(law_json: dict):
53
  raise Exception(f"❌ فشل إدراج/تحديث القانون: {title}")
54
 
55
  # -------------------------------
56
- # 2) UPSERT الأقسام
57
  # -------------------------------
58
  for sec in sections:
59
  sec_title = sec.get("title", "")
60
  sec_content = sec.get("content", "")
61
  sec_tables = sec.get("tables", [])
62
 
63
- res_sec = (
64
- supabase.table("sections")
65
- .insert(
66
- {
67
- "law_id": law_id,
68
- "title": sec_title or "",
69
- "content": sec_content or "",
70
- "chapter_number": (
71
- int(re.search(r"\d+", sec_title or "0").group(0))
72
- if re.search(r"\d+", sec_title or "")
73
- else 0
74
- ),
75
- "tables": sec_tables or [],
76
- }
77
- )
78
- .execute()
79
- )
80
-
81
 
82
- section_id = res_sec.data[0]["id"]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83
 
84
  # -------------------------------
85
  # 3) UPSERT المواد لكل قسم
@@ -94,12 +104,12 @@ def save_law_to_supabase(law_json: dict):
94
  .upsert(
95
  {
96
  "chapter_id": section_id,
97
- "id_law": law_id, # ربط المادة بالقانون مباشرة
98
  "article_number": article_number,
99
  "content": article_content or "",
100
  "tables": article_tables or [],
101
  },
102
- on_conflict=["chapter_id", "law_id", "article_number"], # المفتاح الفريد للمادة
103
  )
104
  .execute()
105
  )
 
53
  raise Exception(f"❌ فشل إدراج/تحديث القانون: {title}")
54
 
55
  # -------------------------------
56
+ # 2) إدخال الأقسام (insert عادي مع التحقق)
57
  # -------------------------------
58
  for sec in sections:
59
  sec_title = sec.get("title", "")
60
  sec_content = sec.get("content", "")
61
  sec_tables = sec.get("tables", [])
62
 
63
+ # التحقق من وجود القسم مسبقًا
64
+ existing_section = (
65
+ supabase.table("sections")
66
+ .select("id")
67
+ .eq("law_id", law_id)
68
+ .eq("title", sec_title)
69
+ .execute()
70
+ )
 
 
 
 
 
 
 
 
 
 
71
 
72
+ if existing_section.data and len(existing_section.data) > 0:
73
+ section_id = existing_section.data[0]["id"]
74
+ else:
75
+ res_sec = (
76
+ supabase.table("sections")
77
+ .insert(
78
+ {
79
+ "law_id": law_id,
80
+ "title": sec_title or "",
81
+ "content": sec_content or "",
82
+ "chapter_number": (
83
+ int(re.search(r"\d+", sec_title or "0").group(0))
84
+ if re.search(r"\d+", sec_title or "")
85
+ else 0
86
+ ),
87
+ "tables": sec_tables or [],
88
+ }
89
+ )
90
+ .execute()
91
+ )
92
+ section_id = res_sec.data[0]["id"]
93
 
94
  # -------------------------------
95
  # 3) UPSERT المواد لكل قسم
 
104
  .upsert(
105
  {
106
  "chapter_id": section_id,
107
+ "id_law": law_id,
108
  "article_number": article_number,
109
  "content": article_content or "",
110
  "tables": article_tables or [],
111
  },
112
+ on_conflict=["chapter_id", "id_law", "article_number"],
113
  )
114
  .execute()
115
  )