-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinit.py
More file actions
103 lines (79 loc) · 3.15 KB
/
init.py
File metadata and controls
103 lines (79 loc) · 3.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import logging
import sys
# Import the environment file loader
from app.core.utils.sensitive import load_environment_files
import os
# Load environment variables
load_environment_files()
# Force Python to look for the supabase package in site-packages first
# This prevents our local 'supabase' module from shadowing the installed library
for path in sys.path:
if "site-packages" in path:
sys.path.insert(0, path)
break
# Now import from the actual supabase-py library
try:
# Try alternative import path (depending on how the package was installed)
from supabase import create_client, Client
except ImportError:
raise ImportError(
"Could not import 'create_client' from either 'supabase_py' or 'supabase'. "
"Please ensure the supabase-py package is installed using: "
"pip install supabase"
)
logger = logging.getLogger(" apps.supabase_home")
# Global variable to hold the Supabase client instance
_supabase_client = None
def initialize_supabase() -> Client:
"""
Initialize the Supabase client using settings from Django configuration.
This function checks for required environment variables and creates a
Supabase client instance using the official supabase-py library.
Returns:
Client: An initialized Supabase client instance
Raises:
ValueError: If required environment variables are missing
"""
global _supabase_client
# If client is already initialized, return it
if _supabase_client is not None:
return _supabase_client
# Check for required environment variables
supabase_url = os.getenv("SUPABASE_URL")
# Determine which key to use: prefer service role key for admin operations
service_role_key = os.getenv("SUPABASE_SERVICE_ROLE_KEY")
if service_role_key:
supabase_key = service_role_key
else:
supabase_key = os.getenv("SUPABASE_ANON_KEY")
if not supabase_url:
error_msg = "SUPABASE_URL is not set in environment variables"
logger.error(error_msg)
raise ValueError(error_msg)
if not supabase_key:
error_msg = "SUPABASE_SERVICE_ROLE_KEY or SUPABASE_ANON_KEY is not set in environment variables"
logger.error(error_msg)
raise ValueError(error_msg)
# Log initialization (without sensitive info)
logger.info(f"Initializing Supabase client with URL: {supabase_url}")
try:
# Create the Supabase client
_supabase_client = create_client(supabase_url, supabase_key)
logger.info("Supabase client initialized successfully")
return _supabase_client
except Exception as e:
logger.exception(f"Failed to initialize Supabase client: {str(e)}")
raise
def get_supabase_client() -> Client:
"""
Get the Supabase client instance, initializing it if necessary.
This is the recommended way to access the Supabase client throughout the application.
Returns:
Client: The Supabase client instance
Raises:
ValueError: If required environment variables are missing
"""
global _supabase_client
if _supabase_client is None:
return initialize_supabase()
return _supabase_client