Mazenbs commited on
Commit
e2befa1
·
verified ·
1 Parent(s): 2a7f091

Update supabase_utils.py

Browse files
Files changed (1) hide show
  1. supabase_utils.py +47 -50
supabase_utils.py CHANGED
@@ -23,68 +23,63 @@ supabase: Client = create_client(SUPABASE_URL, SUPABASE_KEY)
23
  # ---------------------------------------------
24
  def save_law_to_supabase(law_json):
25
  """
26
- law_json: dict يحتوي على المفتاح 'title', 'preamble', 'sections'
27
  """
28
  title = law_json.get("title", "عنوان غير معروف")
29
  preamble = law_json.get("preamble", "")
 
 
 
30
  sections = law_json.get("sections", [])
31
 
32
- # 1) إدراج القانون إذا لم يكن موجودًا
33
- existing_law = supabase.table("laws").select("id").eq("title", title).execute()
34
- if existing_law.data and len(existing_law.data) > 0:
35
- law_id = existing_law.data[0]["id"]
36
- print(f"⚠ القانون موجود مسبقاً: {title}")
37
- else:
38
- res = (
39
- supabase.table("laws")
40
- .insert(
41
- {
42
- "title": title or "",
43
- "preamble": preamble or "",
44
- "law_type": "def",
45
- }
46
- )
47
- .execute()
48
  )
 
 
 
 
49
  law_id = res.data[0]["id"]
50
- print(f"✔ تم إدراج القانون: {title}")
 
 
51
 
52
- # 2) إدراج الأقسام والمواد مع الجداول داخل العمود jsonb
53
  for sec in sections:
54
  sec_title = sec.get("title", "")
55
  sec_content = sec.get("content", "")
56
  sec_tables = sec.get("tables", [])
57
 
58
- # التحقق من وجود القسم مسبقاً
59
- existing_section = (
60
  supabase.table("sections")
61
- .select("id")
62
- .eq("law_id", law_id)
63
- .eq("title", sec_title)
 
 
 
 
 
 
 
 
 
 
 
64
  .execute()
65
  )
66
-
67
- if existing_section.data and len(existing_section.data) > 0:
68
- section_id = existing_section.data[0]["id"]
69
- else:
70
- res_sec = (
71
- supabase.table("sections")
72
- .insert(
73
- {
74
- "law_id": law_id,
75
- "title": sec_title or "",
76
- "content": sec_content or "",
77
- "chapter_number": (
78
- int(re.search(r"\d+", sec_title or "0").group(0))
79
- if re.search(r"\d+", sec_title or "")
80
- else 0
81
- ),
82
- "tables": sec_tables or [], # تخزين الجداول مباشرة في jsonb
83
- }
84
- )
85
- .execute()
86
- )
87
- section_id = res_sec.data[0]["id"]
88
 
89
  # إدراج المواد
90
  for art in sec.get("articles", []):
@@ -92,18 +87,20 @@ def save_law_to_supabase(law_json):
92
  article_content = art.get("text", "")
93
  article_tables = art.get("tables", [])
94
 
 
95
  res_art = (
96
  supabase.table("articles")
97
- .insert(
98
  {
99
  "chapter_id": section_id,
100
  "article_number": article_number,
101
  "content": article_content or "",
102
- "tables": article_tables or [], # تخزين الجداول ه��ا أيضاً
103
- }
 
104
  )
105
  .execute()
106
  )
107
  article_id = res_art.data[0]["id"]
108
 
109
- print(f"✔ تم حفظ جميع الأقسام والمواد والجداول للقانون: {title}")
 
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(
39
+ {
40
+ "title": title or "",
41
+ "number_law": law_number,
42
+ "year_law": law_year,
43
+ "preamble": preamble or "",
44
+ "law_type": "def",
45
+ },
46
+ on_conflict=["number_law", "year_law", "title"], # المفتاح الفريد الثلاثي
 
 
 
 
47
  )
48
+ .execute()
49
+ )
50
+
51
+ if res.data and len(res.data) > 0:
52
  law_id = res.data[0]["id"]
53
+ print(f"✔ تم إدراج/تحديث القانون: {title}")
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(
67
+ {
68
+ "law_id": law_id,
69
+ "title": sec_title or "",
70
+ "content": sec_content or "",
71
+ "chapter_number": (
72
+ int(re.search(r"\d+", sec_title or "0").group(0))
73
+ if re.search(r"\d+", sec_title or "")
74
+ else 0
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
  for art in sec.get("articles", []):
 
87
  article_content = art.get("text", "")
88
  article_tables = art.get("tables", [])
89
 
90
+ # UPSERT المواد بناءً على chapter_id و article_number
91
  res_art = (
92
  supabase.table("articles")
93
+ .upsert(
94
  {
95
  "chapter_id": section_id,
96
  "article_number": article_number,
97
  "content": article_content or "",
98
+ "tables": article_tables or [],
99
+ },
100
+ on_conflict=["chapter_id", "article_number"], # المفتاح الفريد للمواد
101
  )
102
  .execute()
103
  )
104
  article_id = res_art.data[0]["id"]
105
 
106
+ print(f"✔ تم حفظ/تحديث جميع الأقسام والمواد والجداول للقانون: {title}")