amauricunha commited on
Commit
eeb4425
·
verified ·
1 Parent(s): 527c3f3

Update database.py

Browse files
Files changed (1) hide show
  1. database.py +33 -8
database.py CHANGED
@@ -269,11 +269,17 @@ def create_user(email, password):
269
  password_hash, salt = hash_password(password)
270
  confirmation_token = generate_token()
271
 
 
 
 
 
 
 
272
  # Insert user
273
  cursor = db.execute(
274
- '''INSERT INTO users (email, password_hash, salt, confirmation_token)
275
- VALUES (?, ?, ?, ?)''',
276
- (email, password_hash, salt, confirmation_token)
277
  )
278
  user_id = cursor.lastrowid
279
 
@@ -285,12 +291,17 @@ def create_user(email, password):
285
 
286
  db.commit()
287
 
288
- logger.info(f"User created: {email}")
 
 
 
 
289
  return {
290
  'success': True,
291
  'user_id': user_id,
292
  'confirmation_token': confirmation_token,
293
- 'message': 'User created successfully'
 
294
  }
295
 
296
  except Exception as e:
@@ -480,7 +491,7 @@ def get_current_user():
480
 
481
  # Email functionality (for Hugging Face Spaces)
482
  def send_confirmation_email(email, token):
483
- """Send confirmation email (simplified for HF Spaces)"""
484
  try:
485
  # For Hugging Face Spaces, we'll use environment variables for SMTP
486
  smtp_server = os.environ.get('SMTP_SERVER', 'smtp.gmail.com')
@@ -489,7 +500,7 @@ def send_confirmation_email(email, token):
489
  smtp_password = os.environ.get('SMTP_PASSWORD')
490
 
491
  if not all([smtp_username, smtp_password]):
492
- logger.warning("SMTP credentials not configured")
493
  return False
494
 
495
  # Create confirmation URL (will be updated with actual domain)
@@ -518,7 +529,12 @@ def send_confirmation_email(email, token):
518
 
519
  msg.attach(MIMEText(body, 'html'))
520
 
521
- # Send email
 
 
 
 
 
522
  server = smtplib.SMTP(smtp_server, smtp_port)
523
  server.starttls()
524
  server.login(smtp_username, smtp_password)
@@ -526,11 +542,20 @@ def send_confirmation_email(email, token):
526
  server.sendmail(smtp_username, email, text)
527
  server.quit()
528
 
 
 
 
529
  logger.info(f"Confirmation email sent to {email}")
530
  return True
531
 
532
  except Exception as e:
533
  logger.error(f"Error sending confirmation email: {e}")
 
 
 
 
 
 
534
  return False
535
 
536
  # --- CONTENT CURATION FUNCTIONS ---
 
269
  password_hash, salt = hash_password(password)
270
  confirmation_token = generate_token()
271
 
272
+ # For HF Spaces demo mode, auto-confirm emails if SMTP not configured
273
+ auto_confirm = not all([
274
+ os.environ.get('SMTP_USERNAME'),
275
+ os.environ.get('SMTP_PASSWORD')
276
+ ])
277
+
278
  # Insert user
279
  cursor = db.execute(
280
+ '''INSERT INTO users (email, password_hash, salt, confirmation_token, is_confirmed)
281
+ VALUES (?, ?, ?, ?, ?)''',
282
+ (email, password_hash, salt, confirmation_token, auto_confirm)
283
  )
284
  user_id = cursor.lastrowid
285
 
 
291
 
292
  db.commit()
293
 
294
+ if auto_confirm:
295
+ logger.info(f"User created and auto-confirmed: {email} (demo mode)")
296
+ else:
297
+ logger.info(f"User created: {email}")
298
+
299
  return {
300
  'success': True,
301
  'user_id': user_id,
302
  'confirmation_token': confirmation_token,
303
+ 'message': 'User created successfully',
304
+ 'auto_confirmed': auto_confirm
305
  }
306
 
307
  except Exception as e:
 
491
 
492
  # Email functionality (for Hugging Face Spaces)
493
  def send_confirmation_email(email, token):
494
+ """Send confirmation email (simplified for HF Spaces with timeout)"""
495
  try:
496
  # For Hugging Face Spaces, we'll use environment variables for SMTP
497
  smtp_server = os.environ.get('SMTP_SERVER', 'smtp.gmail.com')
 
500
  smtp_password = os.environ.get('SMTP_PASSWORD')
501
 
502
  if not all([smtp_username, smtp_password]):
503
+ logger.warning("SMTP credentials not configured - skipping email")
504
  return False
505
 
506
  # Create confirmation URL (will be updated with actual domain)
 
529
 
530
  msg.attach(MIMEText(body, 'html'))
531
 
532
+ # Send email with timeout
533
+ import socket
534
+
535
+ # Set socket timeout to prevent hanging
536
+ socket.setdefaulttimeout(10)
537
+
538
  server = smtplib.SMTP(smtp_server, smtp_port)
539
  server.starttls()
540
  server.login(smtp_username, smtp_password)
 
542
  server.sendmail(smtp_username, email, text)
543
  server.quit()
544
 
545
+ # Reset socket timeout
546
+ socket.setdefaulttimeout(None)
547
+
548
  logger.info(f"Confirmation email sent to {email}")
549
  return True
550
 
551
  except Exception as e:
552
  logger.error(f"Error sending confirmation email: {e}")
553
+ # Reset socket timeout on error
554
+ try:
555
+ import socket
556
+ socket.setdefaulttimeout(None)
557
+ except:
558
+ pass
559
  return False
560
 
561
  # --- CONTENT CURATION FUNCTIONS ---