forked from csud-reservation/flask-backend
/
manage.py
executable file
·183 lines (138 loc) · 4.85 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#!/usr/bin/env python
import os
from app import create_app, db
from app.models import User, Role, Reservation, Room, Timeslot, Weekday, reservations_users, reservations_timeslots
from flask_script import Manager, Shell, Server
from flask_migrate import Migrate, MigrateCommand
# génération schéma relationnel
import sadisplay
# interface d'administration
from flask_admin import Admin
from flask_admin.contrib.sqla import ModelView
from data.utils import extract_sigles, extract_timeslots, extract_rooms
from load_csv import load_teachers, load_reservations
# On passe les modèles à la création de l'application pour pouvoir y initialiser Flask-Restless
models = dict(
User=User,
Role=Role,
Reservation=Reservation,
Room=Room,
Timeslot=Timeslot,
Weekday=Weekday
)
app = create_app(os.getenv('FLASK_CONFIG') or 'default', models)
manager = Manager(app)
migrate = Migrate(app, db)
# initialisation de Admin
admin = Admin(app, name='CSUD Réservation : admin', template_mode='bootstrap3')
for model in models.values():
admin.add_view(ModelView(model, db.session))
def make_shell_context():
return dict(dict(app=app, db=db).items() + models.items())
if os.getenv('C9_HOSTNAME'):
manager.add_command("runserver", Server(
host=os.getenv('C9_IP'),
port=os.getenv('C9_PORT')
))
print("App started on Cloud9, available on", 'https://' + os.getenv('C9_HOSTNAME'))
manager.add_command("shell", Shell(make_context=make_shell_context, use_ipython=True))
manager.add_command('db', MigrateCommand)
tmp_timetable_txt = "data/timetable.txt"
tmp_rooms_txt = "data/rooms.txt"
@manager.command
def txt(input_pdf, out_txt):
try:
from sh import pdf2txt, rm
except:
print("impossible d'exécuter la commande pdf2txt nécessaire à la conversion du fichier. Installer d'abord le package python pdf miner avec sudo apt-get install python-pdfminer")
pdf2txt(input_pdf, _out = out_txt)
@manager.command
def initdb():
db.drop_all()
db.create_all()
timeslots = extract_timeslots()
rooms = extract_rooms()
sigles = extract_sigles()
# auto data loading from different datasources
Timeslot.insert_timeslots(timeslots)
Weekday.insert_days()
Role.insert_roles()
User.insert_admin()
User.insert_teachers(load_teachers())
Room.insert_rooms(rooms)
@manager.command
def erd():
''' outputs entity relationship diagram into erd.png '''
try:
from sh import eralchemy, mv
eralchemy('-i', app.config['SQLALCHEMY_DATABASE_URI'], '-o', 'app/static/erd.png')
except ImportError as e:
print(str(e))
@manager.command
def rel(file_format):
''' outputs relational diagram into rel.png '''
try:
from sh import dot, cp
desc = sadisplay.describe([
User,
Role,
Reservation,
Room,
Timeslot,
Weekday,
reservations_users,
reservations_timeslots
])
with open('schema.dot', 'w', encoding='utf-8') as f:
f.write(sadisplay.dot(desc))
dot("-T"+file_format, "schema.dot", "-o", "app/static/rel."+file_format)
except ImportError as e:
print(str(e))
@manager.command
def sqldump():
from sqlalchemy import create_engine
from sqlalchemy.schema import CreateTable
models = [User, Role, Reservation, Room, Timeslot, Weekday]
association_tables = [reservations_users, reservations_timeslots]
sql_url = app.config['SQLALCHEMY_DATABASE_URI']
db_engine = create_engine(sql_url)
with open('app/static/data-dev-ddl.sql', 'w') as fd:
for model in models:
sql_ddl = CreateTable(model.__table__).compile(db_engine)
sql_ddl = str(sql_ddl).strip()
fd.write(str(sql_ddl) + ';\n\n')
for table in association_tables:
sql_ddl = CreateTable(table).compile(db_engine)
sql_ddl = str(sql_ddl).strip()
fd.write(str(sql_ddl) + ';\n\n')
try:
from sh import c9
c9('app/static/data-dev-ddl.sql')
except:
pass
@manager.command
def clean():
files = [
'rel.*',
'schema.*'
]
from sh import rm
for file in files:
rm("-f", file)
@manager.command
def load(data_file='data/edt.csv'):
from datetime import date
""" Loads data from CSV file generated by EDT (Emploi Du Temps) """
load_reservations(
db,
start_date=date(2016, 8, 29),
end_date=date(2017, 6, 30),
filename=data_file)
@manager.command
def test():
"""Run the unit tests."""
import unittest
tests = unittest.TestLoader().discover('tests')
unittest.TextTestRunner(verbosity=2).run(tests)
if __name__ == '__main__':
manager.run()