-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
226 lines (180 loc) · 5.8 KB
/
app.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
"""
Per eseguire:
set FLASK_APP=app.py
set FLASK_DEBUG=true
flask run
oppure
python app.py
Per cambiare configurazione da ambiente:
set FLASK_CONFIG=...
"""
import os
from project import create_app, db
from flask import render_template
from flask_migrate import Migrate
#app = create_app()
app = create_app(os.getenv('FLASK_CONFIG') or 'default')
# Create db and migrations
Migrate(app, db)
"""
Per "navigare" in modalità shell
Use shell_context_processor() to add other automatic imports.
"""
@app.shell_context_processor
def make_shell_context():
return dict(
db=db,
Tag=Tag,
Corso=Corso,
Serata=Serata,
Ruolo=Ruolo,
Utente=Utente,
Post=Post,
Comment=Comment,
)
"""
Per i test di unità automatici, il decorator
app.cli.command permette di creare comandi "custom".
Il nome della funzione "decorata", in questo caso "test" sarà il comando per richiamarla.
In questo caso l'implementazione di test() invoca il test runner del package unittest.
Quindi per lanciare i test automatici:
set FLASK_APP=app.py
flask test
"""
@app.cli.command()
def test():
"""Run the unit tests."""
import unittest
# tests è il modulo
tests = unittest.TestLoader().discover("tests")
unittest.TextTestRunner(verbosity=2).run(tests)
"""
Creazione del DB senza utenti e post, ideale per la produzione
"""
@app.cli.command("create_db")
def create_db():
FLASK_CONFIG = os.getenv("FLASK_CONFIG", "None")
app = create_app(FLASK_CONFIG)
app_context = app.app_context()
app_context.push()
from project.serate.models import Serata
from project.corsi.models import Corso
from project.tags.models import Tag
from project.ruoli.models import Ruolo
try:
ruolo_list = Ruolo.query.all()
except Exception as message:
print("No db data")
print("Creazione struttura senza utenti, post e commenti")
db.create_all()
print("Start creating roles")
Ruolo.insert_roles()
print("Start creating tags")
Tag.insert_test_tags()
print("Start creating corsi")
Corso.insert_test_corsi()
print("Start creating serate")
Serata.insert_test_serate()
db.session.remove()
app_context.pop()
"""
Creazione del DB con utenti, post e commenti, ideale in sviluppo per vedere l'app in uso
"""
@app.cli.command("create_test_db")
def create_test_db():
print("Start creating test db")
FLASK_CONFIG = os.getenv("FLASK_CONFIG", "None")
app = create_app(FLASK_CONFIG)
app_context = app.app_context()
app_context.push()
from project.serate.models import Serata
from project.corsi.models import Corso
from project.tags.models import Tag
from project.ruoli.models import Ruolo
from project.utenti.models import Utente
from project.blog.models import Post
from project.commenti.models import Comment
from random import randint
from faker import Faker
try:
user_list = Utente.query.all()
course_list = Corso.query.all()
post_list = Post.query.all()
comment_list = Comment.query.all()
post_list = Post.query.all()
ruolo_list = Ruolo.query.all()
print("DB Tables already exists")
except Exception as message:
print(f"No db data exist, inserting them:")
def users(count=100):
fake = Faker("it_IT")
i = 0
while i < count:
u = Utente(
email=fake.email(),
username=fake.user_name(),
password="password",
confirmed=True,
name=fake.name(),
location=fake.city(),
about_me=fake.text(),
member_since=fake.past_date(),
)
db.session.add(u)
try:
db.session.commit()
i += 1
except IntegrityError:
db.session.rollback()
def posts(count=100):
fake = Faker("it_IT")
user_count = Utente.query.count()
for i in range(count):
u = Utente.query.offset(randint(0, user_count - 1)).first()
p = Post(body=fake.text(), timestamp=fake.past_date(), author=u)
db.session.add(p)
db.session.commit()
def comments(count=100):
fake = Faker("it_IT")
user_count = Utente.query.count()
post_count = Post.query.count()
for i in range(count):
u = Utente.query.offset(randint(0, user_count - 1)).first()
p = Post.query.offset(randint(0, post_count - 1)).first()
c = Comment(
body=fake.text(), timestamp=fake.past_date(), post=p, author=u
)
db.session.add(c)
db.session.commit()
print("Creating structure")
db.create_all()
db.session.commit()
print("Creating roles")
Ruolo.insert_roles()
print("Creating fake users")
users(2)
print("Creating test users")
Utente.insert_test_users()
print("Creating tags")
Tag.insert_test_tags()
print("Creating corsi")
Corso.insert_test_corsi()
print("Creating serate")
Serata.insert_test_serate()
print("Creating posts fake")
posts(3)
print("Creating commenti fake")
comments(3)
print("\nDB Dummy data inserted succesfully")
db.session.remove()
app_context.pop()
"""
Prova "flask pippo" :-)
"""
@app.cli.command()
def pippo():
print("Bravo! Hai capito come funziona il decorator cli di flask")
# Per visualizzare facilmente tutte le configurazioni
print(app.config)
if __name__ == "__main__":
app.run()