-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmanage.py
executable file
·85 lines (65 loc) · 2.29 KB
/
manage.py
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
import os
from uuid import uuid4
import click
from flask_migrate import Migrate
from app import create_app, db
from app.constants import division, roles
from app.models import User, Request
app = create_app(os.getenv('FLASK_CONFIG') or 'default')
migrate = Migrate(app, db)
def make_shell_context():
return dict(app=app, db=db, User=User, Request=Request)
@app.cli.command()
def list_routes():
"""List all routes in the Flask application."""
import urllib.parse
output = []
for rule in app.url_map.iter_rules():
methods = ','.join(rule.methods)
url = urllib.parse.unquote("{:50s} {:20s} {}".format(
rule.endpoint,
methods,
rule
))
output.append(url)
for line in sorted(output):
click.echo(line)
@app.cli.command()
def test():
"""Run the unit tests."""
import unittest
tests = unittest.TestLoader().discover('tests')
unittest.TextTestRunner(verbosity=2).run(tests)
@app.cli.command()
def assign_admin_role():
"""Assigns the admin role to a specified user based on their email from the command line."""
user_email = input("Enter user email: ").strip()
user = User.query.filter_by(email=user_email).first()
if user is None:
print("User not found.")
else:
# Update user role and division
user.role = roles.ADMIN
user.division = division.ADM
db.session.commit()
print(f"Successfully assigned admin role to user {user_email}")
@app.cli.command()
def create_test_admin_user():
"""Allows the user to create a test admin account from the command line"""
first_name = input("Enter user first name: ").strip()
last_name = input("Enter user last name: ").strip()
email = input("Enter user email: ").strip()
if not (first_name and last_name and email):
print("First name, last name, and email are required.")
return
new_user = User(email=email,
division=division.MRMD,
first_name=first_name,
last_name=last_name,
guid=uuid4().hex,
phone=None,
address=None,
role=roles.ADMIN)
db.session.add(new_user)
db.session.commit()
print("Account successfully created!")