Mazenbs commited on
Commit
c557ee8
ยท
verified ยท
1 Parent(s): c539c6d

Update supabase_utils.py

Browse files
Files changed (1) hide show
  1. supabase_utils.py +37 -30
supabase_utils.py CHANGED
@@ -2,25 +2,17 @@ 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.environ.get("SUPABASE_URL")
13
  SUPABASE_KEY = os.environ.get("SUPABASE_SERVICE_ROLE_KEY")
14
 
15
- if not SUPABASE_URL or not SUPABASE_SERVICE_ROLE_KEY:
16
  raise ValueError("โŒ ู„ู… ูŠุชู… ุงู„ุนุซูˆุฑ ุนู„ู‰ ู…ูุงุชูŠุญ Supabase ููŠ Secrets")
17
 
18
- #SUPABASE_URL = os.getenv("SUPABASE_URL")
19
- #SUPABASE_KEY = os.getenv("SUPABASE_SERVICE_ROLE_KEY")
20
-
21
- #if not SUPABASE_URL or not SUPABASE_KEY:
22
- # raise Exception("ูŠุฌุจ ุชุนุฑูŠู SUPABASE_URL ูˆ SUPABASE_SERVICE_ROLE_KEY ููŠ .env")
23
-
24
  supabase: Client = create_client(SUPABASE_URL, SUPABASE_KEY)
25
 
26
 
@@ -32,20 +24,28 @@ def save_law_to_supabase(law_json: dict):
32
  law_json: dict ูŠุญุชูˆูŠ ุนู„ู‰ ุงู„ู…ูุงุชูŠุญ
33
  title, preamble, number, year, sections
34
  """
 
35
  title = law_json.get("title", "ุนู†ูˆุงู† ุบูŠุฑ ู…ุนุฑูˆู")
36
  preamble = law_json.get("preamble", "")
37
  law_number = law_json.get("number")
38
  law_year = law_json.get("year")
39
  sections = law_json.get("sections", [])
40
 
41
- # 1) ุงู„ุชุญู‚ู‚ ู…ู† ูˆุฌูˆุฏ ุงู„ู‚ุงู†ูˆู†
42
- existing = supabase.table("laws").select("id").eq("title", title).execute()
 
 
 
 
 
 
 
43
 
44
  if existing.data:
45
  law_id = existing.data[0]["id"]
46
  print(f"โš  ุงู„ู‚ุงู†ูˆู† ู…ูˆุฌูˆุฏ ู…ุณุจู‚ุงู‹ุŒ ูŠุชู… ุญุฐูู‡: {title}")
47
 
48
- # ุญุฐู ุงู„ู…ูˆุงุฏ ุงู„ู…ุฑุชุจุทุฉ ุจุงู„ุฃู‚ุณุงู…
49
  secs = supabase.table("sections").select("id").eq("law_id", law_id).execute()
50
  for s in secs.data:
51
  supabase.table("articles").delete().eq("chapter_id", s["id"]).execute()
@@ -56,7 +56,9 @@ def save_law_to_supabase(law_json: dict):
56
  # ุญุฐู ุงู„ู‚ุงู†ูˆู† ู†ูุณู‡
57
  supabase.table("laws").delete().eq("id", law_id).execute()
58
 
59
- # 2) ุฅุฏุฑุงุฌ ุงู„ู‚ุงู†ูˆู† ุงู„ุฌุฏูŠุฏ
 
 
60
  res_law = (
61
  supabase.table("laws")
62
  .insert({
@@ -68,44 +70,49 @@ def save_law_to_supabase(law_json: dict):
68
  })
69
  .execute()
70
  )
 
71
  law_id = res_law.data[0]["id"]
72
  print(f"โœ” ุชู… ุฅุฏุฑุงุฌ ุงู„ู‚ุงู†ูˆู† ุงู„ุฌุฏูŠุฏ: {title}")
73
 
74
- # 3) ุฅุฏุฑุงุฌ ุงู„ุฃู‚ุณุงู… ูˆุงู„ู…ูˆุงุฏ
 
 
75
  for sec in sections:
76
  sec_title = sec.get("title", "")
77
  sec_content = sec.get("content", "")
78
  sec_tables = sec.get("tables", [])
79
 
 
 
 
 
 
 
80
  res_sec = (
81
  supabase.table("sections")
82
  .insert({
83
  "law_id": law_id,
84
  "title": sec_title or "",
85
  "content": sec_content or "",
86
- "chapter_number": int(re.search(r"\d+", sec_title or "0").group(0))
87
- if re.search(r"\d+", sec_title or "") else 0,
88
  "tables": sec_tables or [],
89
  })
90
  .execute()
91
  )
 
92
  section_id = res_sec.data[0]["id"]
93
 
 
94
  for art in sec.get("articles", []):
95
- article_number = int(art["number"]) if art.get("number") else None
96
- article_content = art.get("text", "")
97
- article_tables = art.get("tables", [])
98
 
99
  supabase.table("articles").insert({
100
- "chapter_id": section_id,
101
  "article_number": article_number,
102
- "content": article_content or "",
103
- "tables": article_tables or [],
104
  }).execute()
105
 
106
- print(f"โœ” ุชู… ุญูุธ ุฌู…ูŠุน ุงู„ุฃู‚ุณุงู… ูˆุงู„ู…ูˆุงุฏ ู„ู„ู‚ุงู†ูˆู†: {title}")
107
-
108
-
109
- # ---------------------------------------------
110
- # ู…ุซุงู„ ุงุณุชุฎุฏุงู… (ูŠู…ูƒู† ุญุฐูู‡ ุฃูˆ ุชุนู„ูŠู‚ู‡)
111
- # ---------------------------------------------
 
2
  import os
3
  import re
4
  import json
 
 
5
 
6
  # ---------------------------------------------
7
+ # ุชุญู…ูŠู„ ู…ูุงุชูŠุญ Supabase ู…ู† Secrets
8
  # ---------------------------------------------
 
9
  SUPABASE_URL = os.environ.get("SUPABASE_URL")
10
  SUPABASE_KEY = os.environ.get("SUPABASE_SERVICE_ROLE_KEY")
11
 
12
+ if not SUPABASE_URL or not SUPABASE_KEY:
13
  raise ValueError("โŒ ู„ู… ูŠุชู… ุงู„ุนุซูˆุฑ ุนู„ู‰ ู…ูุงุชูŠุญ Supabase ููŠ Secrets")
14
 
15
+ # ุฅู†ุดุงุก ุงู„ุนู…ูŠู„
 
 
 
 
 
16
  supabase: Client = create_client(SUPABASE_URL, SUPABASE_KEY)
17
 
18
 
 
24
  law_json: dict ูŠุญุชูˆูŠ ุนู„ู‰ ุงู„ู…ูุงุชูŠุญ
25
  title, preamble, number, year, sections
26
  """
27
+
28
  title = law_json.get("title", "ุนู†ูˆุงู† ุบูŠุฑ ู…ุนุฑูˆู")
29
  preamble = law_json.get("preamble", "")
30
  law_number = law_json.get("number")
31
  law_year = law_json.get("year")
32
  sections = law_json.get("sections", [])
33
 
34
+ # ---------------------------------------------------------
35
+ # 1) ุงู„ุชุญู‚ู‚ ู…ู† ูˆุฌูˆุฏ ุงู„ู‚ุงู†ูˆู† ุณุงุจู‚ู‹ุง
36
+ # ---------------------------------------------------------
37
+ existing = (
38
+ supabase.table("laws")
39
+ .select("id")
40
+ .eq("title", title)
41
+ .execute()
42
+ )
43
 
44
  if existing.data:
45
  law_id = existing.data[0]["id"]
46
  print(f"โš  ุงู„ู‚ุงู†ูˆู† ู…ูˆุฌูˆุฏ ู…ุณุจู‚ุงู‹ุŒ ูŠุชู… ุญุฐูู‡: {title}")
47
 
48
+ # ุญุฐู ุงู„ู…ูˆุงุฏ ุงู„ุชุงุจุนุฉ ู„ู„ุฃู‚ุณุงู…
49
  secs = supabase.table("sections").select("id").eq("law_id", law_id).execute()
50
  for s in secs.data:
51
  supabase.table("articles").delete().eq("chapter_id", s["id"]).execute()
 
56
  # ุญุฐู ุงู„ู‚ุงู†ูˆู† ู†ูุณู‡
57
  supabase.table("laws").delete().eq("id", law_id).execute()
58
 
59
+ # ---------------------------------------------------------
60
+ # 2) ุฅุถุงูุฉ ุงู„ู‚ุงู†ูˆู† ุงู„ุฌุฏูŠุฏ
61
+ # ---------------------------------------------------------
62
  res_law = (
63
  supabase.table("laws")
64
  .insert({
 
70
  })
71
  .execute()
72
  )
73
+
74
  law_id = res_law.data[0]["id"]
75
  print(f"โœ” ุชู… ุฅุฏุฑุงุฌ ุงู„ู‚ุงู†ูˆู† ุงู„ุฌุฏูŠุฏ: {title}")
76
 
77
+ # ---------------------------------------------------------
78
+ # 3) ุฅุถุงูุฉ ุงู„ุฃู‚ุณุงู… ูˆุงู„ู…ูˆุงุฏ
79
+ # ---------------------------------------------------------
80
  for sec in sections:
81
  sec_title = sec.get("title", "")
82
  sec_content = sec.get("content", "")
83
  sec_tables = sec.get("tables", [])
84
 
85
+ # ุงุณุชุฎุฑุงุฌ ุฑู‚ู… ุงู„ู‚ุณู… ู…ู† ุงู„ุนู†ูˆุงู†
86
+ chapter_number = 0
87
+ match = re.search(r"\d+", sec_title or "")
88
+ if match:
89
+ chapter_number = int(match.group(0))
90
+
91
  res_sec = (
92
  supabase.table("sections")
93
  .insert({
94
  "law_id": law_id,
95
  "title": sec_title or "",
96
  "content": sec_content or "",
97
+ "chapter_number": chapter_number,
 
98
  "tables": sec_tables or [],
99
  })
100
  .execute()
101
  )
102
+
103
  section_id = res_sec.data[0]["id"]
104
 
105
+ # ----------------------- ุฅุถุงูุฉ ุงู„ู…ูˆุงุฏ -----------------------
106
  for art in sec.get("articles", []):
107
+ article_number = int(art["number"]) if art.get("number") else None
108
+ article_content = art.get("text", "")
109
+ article_tables = art.get("tables", [])
110
 
111
  supabase.table("articles").insert({
112
+ "chapter_id": section_id,
113
  "article_number": article_number,
114
+ "content": article_content or "",
115
+ "tables": article_tables or [],
116
  }).execute()
117
 
118
+ print(f"โœ” ุชู… ุญูุธ ุฌู…ูŠุน ุงู„ุฃู‚ุณุงู… ูˆุงู„ู…ูˆุงุฏ ู„ู„ู‚ุงู†ูˆู†: {title}")