-
Notifications
You must be signed in to change notification settings - Fork 0
/
admin.py
126 lines (92 loc) · 3.2 KB
/
admin.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
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
import argparse
import sys
from flask_sqlalchemy import SQLAlchemy
from app import app
from src.models.enums.Role import Role
db = SQLAlchemy()
from src.models.Action import Action
from src.models.Answer import Answer
from src.models.Application import Application
from src.models.OAuth import OAuth
from src.models.Question import Question
from src.models.User import User
db.init_app(app)
def successful_exit(message):
"""prints a message and exits"""
print(message)
exit(0)
def exit_with_error(error_message):
"""prints an error to stderr and exits"""
print(error_message, file=sys.stderr)
exit(1)
def get_user_email(email):
"""Returns user associated with user Email"""
return db.session.query(User) \
.filter(User.email == email) \
.first()
def make_admin():
"""Makes a user an admin"""
print('What is the email of the user you are making an admin')
email_str = input()
user = get_user_email(email_str)
if user is None:
exit_with_error('User does not exist. Exiting.')
user.role = Role.admin
try:
db.session.commit()
except Exception as e:
db.session.rollback()
exit_with_error('Error occurred while persisting: {e}. Exiting.')
successful_exit(f"Made user {email_str} an admin.")
def remove_admin():
"""Removes a user as an admin"""
print('What is the email of the user you are removing as an admin')
email_str = input()
user = get_user_email(email_str)
if user is None:
exit_with_error('User does not exist. Exiting.')
user.role = Role.user
try:
db.session.commit()
except Exception as e:
db.session.rollback()
exit_with_error('Error occurred while persisting: {e}. Exiting.')
successful_exit(f"Removed user {email_str} as an admin.")
def drop_database():
"""Drop the database"""
print(
'Are you absolutely sure you want to proceed with dropping the database? This action is irreversible. [YES or NO]'
)
confirmation = input()
if confirmation != 'YES':
exit('Confirmation not received. Exiting.')
try:
Action.__table__.drop(db.engine)
OAuth.__table__.drop(db.engine)
Answer.__table__.drop(db.engine)
Application.__table__.drop(db.engine)
Question.__table__.drop(db.engine)
User.__table__.drop(db.engine)
except Exception as e:
exit_with_error(f"Error occurred while dropping database: {e}. Exiting.")
successful_exit('Dropped database.')
commands = {
'make-admin': make_admin,
'remove-admin': remove_admin,
'drop-database': drop_database,
}
parser = argparse.ArgumentParser(description='Administrate the Flask app')
parser.add_argument('-c',
'--command',
default='make-admin',
choices=list(commands.keys()),
type=str) # optional, default is 'make-admin'
if __name__ == '__main__':
args = parser.parse_args()
command = args.command
# check if command is a valid option
if command not in commands:
exit_with_error(f"command {command} is not valid. Exiting.")
func = commands[command]
with app.app_context():
func()