-
Notifications
You must be signed in to change notification settings - Fork 0
/
manage.py
107 lines (86 loc) · 2.79 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import re
import sys
from flask_migrate import Migrate, MigrateCommand
from flask_script import Manager, prompt, prompt_pass, Shell
from app import app
from apps.auth import models
with app.app_context():
db = app.db
# flask-migrate instance
migrate = Migrate(app, db)
# flask-script instance
manager = Manager(app)
# shell context
def _make_context():
return dict(app=app, db=db, models=models)
def _validuser():
"""
Valid user data
"""
username = prompt('Username')
if not username:
sys.exit('\nCould not create user: Username is obligatory')
firstname = prompt('First name')
if not firstname:
sys.exit('\nCould not create user: First name is obligatory')
lastname = prompt('Last name')
if not lastname:
sys.exit('\nCould not create user: Last name is obligatory')
email = prompt('User E-Mail')
if not email:
sys.exit('\nCould not create user: E-Mail did not match')
EMAIL_REGEX = re.compile(r"[^@]+@[^@]+\.[^@]+")
if not EMAIL_REGEX.match(email):
sys.exit('\nCould not create user: Invalid E-Mail addresss')
password = prompt_pass('User password')
password_confirm = prompt_pass('Confirmed password')
if not password == password_confirm:
sys.exit('\nCould not create user: Passwords did not match')
return {
'username': username,
'firstname': firstname,
'lastname': lastname,
'email': email,
'password': password,
}
@manager.command
def createsuperuser():
"""
Method for create super user
"""
data = _validuser()
user = models.User(
username=data['username'],
email=data['email'],
first_name=data['firstname'],
last_name=data['lastname'],
password=data['password'],
is_active=True,
is_admin=True
)
db.session.add(user)
db.session.commit()
print("SuperUser created!")
@manager.command
def createuser():
"""
Method for create user
"""
data = _validuser()
user = models.User(
username=data['username'],
email=data['email'],
first_name=data['firstname'],
last_name=data['lastname'],
password=data['password'],
is_active=True,
is_admin=False
)
db.session.add(user)
db.session.commit()
print("User created!")
# Managers commands
manager.add_command('db', MigrateCommand)
manager.add_command("shell", Shell(make_context=_make_context))
if __name__ == '__main__':
manager.run()