Mazenbs commited on
Commit
7be37dc
·
verified ·
1 Parent(s): 5a58792

Update supabase_utils.py

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