-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
392 lines (340 loc) · 13.4 KB
/
Copy pathdatabase.py
File metadata and controls
392 lines (340 loc) · 13.4 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
import os
from datetime import datetime
from supabase import create_client
url = os.getenv("SUPABASE_URL")
key = os.getenv("SUPABASE_KEY")
try:
supabase = create_client(url, key)
print("✅ Supabase connected successfully")
except Exception as e:
print(f"❌ Supabase connection error: {e}")
supabase = None
# ─── USER MANAGEMENT ─────────────────────────────────────────────
def get_user(telegram_id: str):
if not supabase:
return None
try:
result = supabase.table("users").select("*").eq("telegram_id", str(telegram_id)).execute()
if result.data and len(result.data) > 0:
return result.data[0]
return None
except Exception as e:
print(f"get_user error: {e}")
return None
def create_user(telegram_id: str, username: str):
if not supabase:
return None
try:
today = datetime.now()
user_data = {
"telegram_id": str(telegram_id),
"username": str(username)[:100],
"is_premium": False,
"slides_today": 0,
"last_used_date": today.strftime("%Y-%m-%d"),
"total_presentations": 0,
"joined": today.isoformat(),
"premium_activated_by": None,
"premium_date": None,
"url_uses_this_month": 0,
"file_uses_this_month": 0,
"last_month_reset": today.strftime("%Y-%m"),
"theme": "classic",
# ── NEW COLUMNS ──
"preferred_pack": "executive", # 'executive' or 'magazine'
"accent_color": None, # hex string e.g. '#6B2D8B' or None
}
result = supabase.table("users").insert(user_data).execute()
print(f"✅ User created: {telegram_id} ({username})")
if result.data and len(result.data) > 0:
return result.data[0]
return user_data
except Exception as e:
print(f"create_user error: {e}")
return None
def get_or_create_user(telegram_id: str, username: str):
user = get_user(telegram_id)
if not user:
user = create_user(telegram_id, username)
if not user:
user = get_user(telegram_id)
return user
# ─── DAILY / MONTHLY RESET ───────────────────────────────────────
def reset_daily_if_needed(telegram_id: str):
if not supabase:
return
try:
user = get_user(telegram_id)
if not user:
return
today = datetime.now().strftime("%Y-%m-%d")
if user.get("last_used_date", "") != today:
supabase.table("users").update({
"slides_today": 0,
"last_used_date": today
}).eq("telegram_id", str(telegram_id)).execute()
except Exception as e:
print(f"reset_daily error: {e}")
def reset_monthly_if_needed(telegram_id: str):
if not supabase:
return
try:
user = get_user(telegram_id)
if not user:
return
current_month = datetime.now().strftime("%Y-%m")
if user.get("last_month_reset", "") != current_month:
supabase.table("users").update({
"url_uses_this_month": 0,
"file_uses_this_month": 0,
"last_month_reset": current_month
}).eq("telegram_id", str(telegram_id)).execute()
except Exception as e:
print(f"reset_monthly error: {e}")
# ─── USAGE TRACKING ──────────────────────────────────────────────
def increment_usage(telegram_id: str):
if not supabase:
return
try:
user = get_user(telegram_id)
if not user:
return
supabase.table("users").update({
"slides_today": user.get("slides_today", 0) + 1,
"total_presentations": user.get("total_presentations", 0) + 1
}).eq("telegram_id", str(telegram_id)).execute()
except Exception as e:
print(f"increment_usage error: {e}")
def increment_url_usage(telegram_id: str):
if not supabase:
return
try:
reset_monthly_if_needed(telegram_id)
user = get_user(telegram_id)
if not user:
return
supabase.table("users").update({
"url_uses_this_month": user.get("url_uses_this_month", 0) + 1
}).eq("telegram_id", str(telegram_id)).execute()
except Exception as e:
print(f"increment_url error: {e}")
def increment_file_usage(telegram_id: str):
if not supabase:
return
try:
reset_monthly_if_needed(telegram_id)
user = get_user(telegram_id)
if not user:
return
supabase.table("users").update({
"file_uses_this_month": user.get("file_uses_this_month", 0) + 1
}).eq("telegram_id", str(telegram_id)).execute()
except Exception as e:
print(f"increment_file error: {e}")
# ─── CHECKS ──────────────────────────────────────────────────────
def can_generate(telegram_id: str) -> bool:
try:
reset_daily_if_needed(telegram_id)
user = get_user(telegram_id)
if not user:
return True
if user.get("is_premium", False):
return True
return user.get("slides_today", 0) < 2
except Exception as e:
print(f"can_generate error: {e}")
return True
def can_use_url(telegram_id: str) -> bool:
try:
reset_monthly_if_needed(telegram_id)
user = get_user(telegram_id)
if not user:
return True
if user.get("is_premium", False):
return True
return user.get("url_uses_this_month", 0) < 1
except Exception as e:
print(f"can_use_url error: {e}")
return True
def can_use_file(telegram_id: str) -> bool:
try:
reset_monthly_if_needed(telegram_id)
user = get_user(telegram_id)
if not user:
return True
if user.get("is_premium", False):
return True
return user.get("file_uses_this_month", 0) < 1
except Exception as e:
print(f"can_use_file error: {e}")
return True
def is_premium(telegram_id: str) -> bool:
try:
user = get_user(str(telegram_id))
if not user:
return False
return user.get("is_premium", False)
except Exception as e:
print(f"is_premium error: {e}")
return False
# ─── PREMIUM MANAGEMENT ──────────────────────────────────────────
def activate_premium(telegram_id: str, activated_by: str):
if not supabase:
return False
try:
user = get_user(str(telegram_id))
if not user:
return False
supabase.table("users").update({
"is_premium": True,
"premium_activated_by": activated_by,
"premium_date": datetime.now().isoformat()
}).eq("telegram_id", str(telegram_id)).execute()
return True
except Exception as e:
print(f"activate_premium error: {e}")
return False
def revoke_premium(telegram_id: str):
if not supabase:
return False
try:
supabase.table("users").update({
"is_premium": False
}).eq("telegram_id", str(telegram_id)).execute()
return True
except Exception as e:
print(f"revoke_premium error: {e}")
return False
# ─── STATISTICS ──────────────────────────────────────────────────
def get_all_users():
if not supabase:
return {}
try:
result = supabase.table("users").select("*").execute()
return {u["telegram_id"]: u for u in result.data}
except Exception as e:
print(f"get_all_users error: {e}")
return {}
def get_premium_users():
if not supabase:
return {}
try:
result = supabase.table("users").select("*").eq("is_premium", True).execute()
return {u["telegram_id"]: u for u in result.data}
except Exception as e:
print(f"get_premium_users error: {e}")
return {}
def get_total_stats():
if not supabase:
return {"total_users": 0, "premium_users": 0, "free_users": 0, "total_slides_generated": 0}
try:
result = supabase.table("users").select("*").execute()
users = result.data
total = len(users)
premium = sum(1 for u in users if u.get("is_premium", False))
total_slides = sum(u.get("total_presentations", 0) for u in users)
return {
"total_users": total,
"premium_users": premium,
"free_users": total - premium,
"total_slides_generated": total_slides
}
except Exception as e:
print(f"get_total_stats error: {e}")
return {"total_users": 0, "premium_users": 0, "free_users": 0, "total_slides_generated": 0}
# ─── THEME ───────────────────────────────────────────────────────
def get_user_theme(telegram_id: str) -> str:
try:
user = get_user(str(telegram_id))
if not user:
return "classic"
return user.get("theme", "classic")
except Exception as e:
print(f"get_user_theme error: {e}")
return "classic"
def save_user_theme(telegram_id: str, theme: str):
if not supabase:
return False
try:
supabase.table("users").update({
"theme": theme
}).eq("telegram_id", str(telegram_id)).execute()
return True
except Exception as e:
print(f"save_user_theme error: {e}")
return False
# ─── PACK & COLOR (NEW) ──────────────────────────────────────────
def get_user_pack(telegram_id: str) -> str:
"""Get user's preferred pack: 'executive' or 'magazine'"""
try:
user = get_user(str(telegram_id))
if not user:
return "executive"
return user.get("preferred_pack", "executive") or "executive"
except Exception as e:
print(f"get_user_pack error: {e}")
return "executive"
def save_user_pack(telegram_id: str, pack: str):
"""Save user's preferred pack"""
if not supabase:
return False
try:
supabase.table("users").update({
"preferred_pack": pack
}).eq("telegram_id", str(telegram_id)).execute()
print(f"📦 Saved pack {pack} for {telegram_id}")
return True
except Exception as e:
print(f"save_user_pack error: {e}")
return False
def get_user_accent(telegram_id: str):
"""Get user's saved accent color (hex string or None)"""
try:
user = get_user(str(telegram_id))
if not user:
return None
return user.get("accent_color", None)
except Exception as e:
print(f"get_user_accent error: {e}")
return None
def save_user_accent(telegram_id: str, color: str):
"""Save user's accent color. color = hex string like '#6B2D8B' or name like 'purple'"""
if not supabase:
return False
try:
supabase.table("users").update({
"accent_color": color
}).eq("telegram_id", str(telegram_id)).execute()
print(f"🎨 Saved accent {color} for {telegram_id}")
return True
except Exception as e:
print(f"save_user_accent error: {e}")
return False
# ─── TODAY USAGE ─────────────────────────────────────────────────
def get_today_usage(telegram_id: str) -> int:
try:
user = get_user(str(telegram_id))
if not user:
return 0
reset_daily_if_needed(telegram_id)
user = get_user(str(telegram_id))
return user.get("slides_today", 0) if user else 0
except Exception as e:
print(f"get_today_usage error: {e}")
return 0
# ─── DEBUG ───────────────────────────────────────────────────────
def debug_print_users():
if not supabase:
print("Supabase not connected")
return
try:
result = supabase.table("users").select("*").execute()
print(f"\n📊 USERS IN DATABASE: {len(result.data)}")
for user in result.data:
print(f" - {user.get('telegram_id')} | {user.get('username')} | "
f"Premium: {user.get('is_premium')} | "
f"Pack: {user.get('preferred_pack','executive')} | "
f"Color: {user.get('accent_color','default')}")
print("")
except Exception as e:
print(f"debug error: {e}")