-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdelete_user.py
More file actions
90 lines (75 loc) · 2.84 KB
/
Copy pathdelete_user.py
File metadata and controls
90 lines (75 loc) · 2.84 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
import os
import sqlite3
import click
from app import app
from application.models.user import User
from application.utils.extensions import db
def get_database_connection():
# Check if DATABASE_URL is set in environment or .env
database_url = os.getenv("DATABASE_URL")
if database_url:
print(f"Using database URL: {database_url}")
return (
None # Return None to indicate that SQLAlchemy should handle the connection
)
else:
print("DATABASE_URL not set. Defaulting to local SQLite database.")
return sqlite3.connect(
"flaskkeyring.db"
) # Connect directly to SQLite if no DATABASE_URL
@click.command()
@click.option(
"--email",
prompt="Enter the user's email to delete",
help="The email of the user to delete, along with all related data.",
)
def delete_user(email):
# Check for a direct SQLite connection
conn = get_database_connection()
if conn:
# If using direct SQLite connection, perform raw SQL deletion
cursor = conn.cursor()
cursor.execute("SELECT id FROM users WHERE email = ?", (email,))
user = cursor.fetchone()
if not user:
print(f"No user found with email: {email}")
conn.close()
return
# Confirm deletion
confirm = input(
f"Are you sure you want to delete user '{email}' and all related data? (yes/no): "
)
if confirm.lower() != "yes":
print("Deletion aborted.")
conn.close()
return
# Delete related records manually with raw SQL
user_id = user[0]
cursor.execute("DELETE FROM folders WHERE user_id = ?", (user_id,))
cursor.execute("DELETE FROM passwords WHERE user_id = ?", (user_id,))
cursor.execute("DELETE FROM reset_tokens WHERE user_id = ?", (user_id,))
cursor.execute("DELETE FROM users WHERE id = ?", (user_id,))
conn.commit()
conn.close()
print(f"User '{email}' and all related data have been deleted (SQLite).")
else:
# If DATABASE_URL is set, use SQLAlchemy with Flask context
with app.app_context():
user = User.query.filter_by(email=email).first()
if not user:
print(f"No user found with email: {email}")
return
confirm = input(
f"Are you sure you want to delete user '{email}' and all related data? (yes/no): "
)
if confirm.lower() != "yes":
print("Deletion aborted.")
return
# Use SQLAlchemy to delete user and related data
db.session.delete(user)
db.session.commit()
print(
f"User '{email}' and all related data have been deleted (SQLAlchemy)."
)
if __name__ == "__main__":
delete_user()