Repository navigation
Expand file tree
/
Copy pathsetup.py
More file actions
106 lines (79 loc) · 3.19 KB
/
Copy pathsetup.py
File metadata and controls
106 lines (79 loc) · 3.19 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
"""Interactive setup utility for NovaTalk.
This script guides a system administrator through configuring NovaTalk
for the first time. It creates a .env file, initialises the database,
and provisions the first administrator account.
"""
import getpass
import os
import secrets
from pathlib import Path
from dotenv import set_key
from app import create_app, db
from app.models import User
ENV_PATH = Path(".env")
def prompt(prompt_text, default=None, secret=False):
prompt_display = f"{prompt_text}"
if default:
prompt_display += f" [{default}]"
prompt_display += ": "
if secret:
value = getpass.getpass(prompt_display)
else:
value = input(prompt_display)
if not value and default is not None:
return default
return value
def generate_env(database_url: str, upload_folder: str, max_upload_mb: int):
secret_key = secrets.token_hex(16)
set_key(str(ENV_PATH), "SECRET_KEY", secret_key)
set_key(str(ENV_PATH), "DATABASE_URL", database_url)
set_key(str(ENV_PATH), "UPLOAD_FOLDER", upload_folder)
set_key(str(ENV_PATH), "MAX_UPLOAD_MB", str(max_upload_mb))
print("[+] .env file generated.")
def initialise_database(app):
with app.app_context():
db.create_all()
print("[+] Database tables created.")
existing_admin = User.query.filter_by(is_admin=True).first()
if existing_admin:
print("[!] Admin account already exists, skipping admin creation.")
return
print("\nCreate the first administrator account")
display_name = prompt("Display name", "Administrator")
username = prompt("Username", "admin")
email = prompt("Email address", "admin@example.com")
password = getpass.getpass("Password: ")
admin = User(
display_name=display_name,
username=username.strip().lower().lstrip("@"),
email=email.lower(),
is_admin=True,
)
admin.set_password(password)
db.session.add(admin)
db.session.commit()
print("[+] Admin user created.")
def main():
print("NovaTalk setup wizard\n=====================")
print("Provide your MySQL database connection details. The database must already exist.")
mysql_host = prompt("MySQL host", "localhost")
mysql_port = prompt("MySQL port", "3306")
mysql_db = prompt("Database name", "novatalk")
mysql_user = prompt("Database user", "novatalk")
mysql_password = prompt("Database password", secret=True)
database_url = f"mysql+pymysql://{mysql_user}:{mysql_password}@{mysql_host}:{mysql_port}/{mysql_db}"
upload_folder = prompt("Upload directory", str(Path("app/static/uploads").resolve()))
max_upload_input = prompt("Maximum upload size (MB)", "30")
try:
max_upload_mb = max(1, int(max_upload_input))
except ValueError:
print("[!] Invalid size provided. Using default 30 MB.")
max_upload_mb = 30
if not ENV_PATH.exists():
ENV_PATH.touch()
generate_env(database_url, upload_folder, max_upload_mb)
app = create_app()
initialise_database(app)
print("\nSetup complete! You can now run NovaTalk with 'python app.py'.")
if __name__ == "__main__":
main()