Mazenbs commited on
Commit
019355b
·
verified ·
1 Parent(s): 24684f0

Update supabase_utils.py

Browse files
Files changed (1) hide show
  1. supabase_utils.py +35 -35
supabase_utils.py CHANGED
@@ -1,10 +1,8 @@
1
  from supabase import create_client, Client
2
  import os
3
  import re
4
- import json
5
  from dotenv import load_dotenv
6
 
7
-
8
  # ---------------------------------------------
9
  # تهيئة Supabase
10
  # ---------------------------------------------
@@ -17,22 +15,22 @@ if not SUPABASE_URL or not SUPABASE_KEY:
17
 
18
  supabase: Client = create_client(SUPABASE_URL, SUPABASE_KEY)
19
 
20
-
21
  # ---------------------------------------------
22
- # حفظ القانون في قاعدة البيانات
23
  # ---------------------------------------------
24
- def save_law_to_supabase(law_json):
25
  """
26
- law_json: dict يحتوي على المفتاح 'title', 'preamble', 'sections', 'number', 'year'
27
  """
28
  title = law_json.get("title", "عنوان غير معروف")
29
  preamble = law_json.get("preamble", "")
30
- law_number = law_json.get("number") # استخدم None بدل null
31
  law_year = law_json.get("year")
32
-
33
  sections = law_json.get("sections", [])
34
 
35
- # 1) UPSERT القانون مباشرة باستخدام المفتاح الفريد (number_law, year_law, title)
 
 
36
  res = (
37
  supabase.table("laws")
38
  .upsert(
@@ -43,7 +41,7 @@ def save_law_to_supabase(law_json):
43
  "preamble": preamble or "",
44
  "law_type": "def",
45
  },
46
- on_conflict=["number_law", "year_law", "title"], # المفتاح الفريد الثلاثي
47
  )
48
  .execute()
49
  )
@@ -54,13 +52,14 @@ def save_law_to_supabase(law_json):
54
  else:
55
  raise Exception(f"❌ فشل إدراج/تحديث القانون: {title}")
56
 
57
- # 2) إدراج الأقسام والمواد كما في الكود السابق
 
 
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
- # UPSERT الأقسام بناءً على law_id و title
64
  res_sec = (
65
  supabase.table("sections")
66
  .upsert(
@@ -75,34 +74,35 @@ def save_law_to_supabase(law_json):
75
  ),
76
  "tables": sec_tables or [],
77
  },
78
- on_conflict=["law_id", "title"], # المفتاح الفريد للأقسام
79
  )
80
  .execute()
81
  )
 
82
  section_id = res_sec.data[0]["id"]
83
 
84
- # إدراج المواد
85
- # إدراج المواد
86
- for art in sec.get("articles", []):
87
- article_number = int(art.get("number")) if art.get("number") else None
88
- article_content = art.get("text", "")
89
- article_tables = art.get("tables", [])
 
90
 
91
- # UPSERT المواد بناءً على chapter_id و article_number مع id_law
92
- res_art = (
93
- supabase.table("articles")
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", "id_law", "article_number"], # المفتاح الفريد المركب للمواد
103
- )
104
- .execute()
105
- )
106
- article_id = res_art.data[0]["id"]
107
 
108
  print(f"✔ تم حفظ/تحديث جميع الأقسام والمواد والجداول للقانون: {title}")
 
1
  from supabase import create_client, Client
2
  import os
3
  import re
 
4
  from dotenv import load_dotenv
5
 
 
6
  # ---------------------------------------------
7
  # تهيئة Supabase
8
  # ---------------------------------------------
 
15
 
16
  supabase: Client = create_client(SUPABASE_URL, SUPABASE_KEY)
17
 
 
18
  # ---------------------------------------------
19
+ # حفظ القانون في قاعدة البيانات مع الأقسام والمواد
20
  # ---------------------------------------------
21
+ def save_law_to_supabase(law_json: dict):
22
  """
23
+ law_json: dict يحتوي على المفاتيح 'title', 'preamble', 'sections', 'number', 'year'
24
  """
25
  title = law_json.get("title", "عنوان غير معروف")
26
  preamble = law_json.get("preamble", "")
27
+ law_number = law_json.get("number")
28
  law_year = law_json.get("year")
 
29
  sections = law_json.get("sections", [])
30
 
31
+ # -------------------------------
32
+ # 1) UPSERT القانون
33
+ # -------------------------------
34
  res = (
35
  supabase.table("laws")
36
  .upsert(
 
41
  "preamble": preamble or "",
42
  "law_type": "def",
43
  },
44
+ on_conflict=["number_law", "year_law", "title"],
45
  )
46
  .execute()
47
  )
 
52
  else:
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
  .upsert(
 
74
  ),
75
  "tables": sec_tables or [],
76
  },
77
+ on_conflict=["law_id", "title"],
78
  )
79
  .execute()
80
  )
81
+
82
  section_id = res_sec.data[0]["id"]
83
 
84
+ # -------------------------------
85
+ # 3) UPSERT المواد لكل قسم
86
+ # -------------------------------
87
+ for art in sec.get("articles", []):
88
+ article_number = int(art.get("number")) if art.get("number") else None
89
+ article_content = art.get("text", "")
90
+ article_tables = art.get("tables", [])
91
 
92
+ res_art = (
93
+ supabase.table("articles")
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", "article_number"], # المفتاح الفريد للمادة
103
+ )
104
+ .execute()
105
+ )
106
+ article_id = res_art.data[0]["id"]
 
107
 
108
  print(f"✔ تم حفظ/تحديث جميع الأقسام والمواد والجداول للقانون: {title}")