-
Notifications
You must be signed in to change notification settings - Fork 1
/
manage.py
executable file
·135 lines (96 loc) · 3.82 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/usr/bin/env python3
import os, sys, re
from collections import OrderedDict
from flask_script import Manager, prompt_bool, prompt, prompt_pass
from flask_security import registerable
from flask_migrate import Migrate, MigrateCommand
from core import db
from factory import create_app
from fixtures import load_all_fixtures
from voxolab.models import DecodeStatus, ProcessType
from models import AsrModel
from models import User
from models import Process
from sqlalchemy import or_
from math import log, exp
from api.process import process_api
sys.path.append(os.path.join(os.path.dirname(__file__)))
app = create_app()
manager = Manager(app)
migrate = Migrate(app, db)
manager.add_command('db', MigrateCommand)
@manager.command
def dropdb():
"Clear the existing database (drop_all)"
if prompt_bool(
"Are you sure you want to lose all your data"):
db.drop_all()
@manager.command
def initdb():
"Clear the existing database and create a fresh one (drop_all && create_all)"
if prompt_bool(
"Are you sure you want to lose all your data"):
db.drop_all()
db.create_all()
load_all_fixtures(db, app)
db.session.commit()
@manager.command
def gettoken(email=None):
"Get token for an user."
if not email:
email = prompt("Please enter the email of the user")
user = User.query.filter_by(email = email).one()
print(user.get_auth_token())
@manager.command
def createuser(email=None):
"Add a new user to the database"
if not email:
email = prompt("Please enter the email of the new user")
while (not is_email_valid(email)):
email = prompt("Please enter the email of the new user")
password = prompt_pass("Please enter the password")
password2 = prompt_pass("Please enter the password again")
while(password != password2):
print("Passwords are different, please enter the 2 passwords again.", file=sys.stderr)
password = prompt_pass("Please enter the password")
password2 = prompt_pass("Please enter the password again")
#credit = prompt("Please enter the credit (number of hours), -1 for no limit")
credit = -1
french_model = AsrModel.query.filter_by(name='french.studio.fr_FR').first()
english_model = AsrModel.query.filter_by(name='english.studio').first()
models = []
if french_model:
models.append(french_model)
if english_model:
models.append(english_model)
registerable.register_user(
email=email,
password=password,
credit=credit,
asr_models=models)
db.session.commit()
print("User '{user}' created successfully!".format(user=email))
def is_email_valid(email):
nb_email = db.session.query(User.id).filter_by(email=email).count()
if(nb_email):
print("User already in database. Please choose another email.", file=sys.stderr)
return False
if (not re.match(r"[^@]+@[^@]+\.[^@]+", email)):
print("Invalid email address.", file=sys.stderr)
return False
return True
@manager.command
def monthly_report():
durations = process_api.get_report()
#Formule: (1/(exp(0.47*ln(HEURES))))*100
for email in OrderedDict(sorted(durations.items(), key=lambda t: t[0])):
print("{}".format(email))
for year in OrderedDict(sorted(durations[email].items(), key=lambda t: t[0])):
for month in OrderedDict(sorted(durations[email][year].items(), key=lambda t: t[0])):
duration = durations[email][year][month]
price_per_hour = (1/(exp(0.47*log(duration/3600))))*100
price = price_per_hour*(duration/3600)
print(" {}/{}\t{:.2f} heures\t\t{:.2f} euros/heure\t\t{:.2f} euros".format(month, year, duration/3600, price_per_hour, price))
return True
if __name__ == "__main__":
manager.run()