-
Notifications
You must be signed in to change notification settings - Fork 0
/
manage.py
199 lines (157 loc) · 5.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
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
import sys
import os
from itertools import groupby
from flask import current_app
try:
from metabomatch.private_keys import GUEST_USER_ID
except ImportError:
GUEST_USER_ID = os.environ.get('GUEST_USER_ID')
from sqlalchemy import func
from sqlalchemy.exc import IntegrityError, OperationalError
from flask.ext.script import (Manager, Shell, Server, prompt, prompt_pass,
prompt_bool)
from flask.ext.migrate import MigrateCommand
from metabomatch.softwares.models import Software, SentenceSoftwareMapping, Sentence, \
get_nb_commits, Upvote, ProConsUpvote, ProCons
from metabomatch.app import create_app
from metabomatch.extensions import db
from metabomatch.flaskbb.utils.populate import (create_test_data, create_welcome_forum,
create_admin_user, create_default_groups, create_default_settings)
# Use the development configuration if available
try:
from metabomatch.configs.development import DevelopmentConfig as Config
except ImportError:
from metabomatch.configs.default import DefaultConfig as Config
flask_app = create_app(Config)
manager = Manager(flask_app)
# Run local server
manager.add_command("runserver", Server("localhost", port=5000))
# Migration commands
manager.add_command('db', MigrateCommand)
# Add interactive project shell
def make_shell_context():
return dict(app=current_app, db=db)
manager.add_command("shell", Shell(make_context=make_shell_context))
@manager.command
def initdb():
"""Creates the database."""
db.create_all()
@manager.command
def dropdb():
"""Deletes the database"""
db.drop_all()
@manager.command
def createall(dropdb=False, createdb=False):
"""Creates the database with some testing content.
If you do not want to drop or create the db add
'-c' (to not create the db) and '-d' (to not drop the db)
"""
if not dropdb:
flask_app.logger.info("Dropping database...")
db.drop_all()
if not createdb:
flask_app.logger.info("Creating database...")
db.create_all()
flask_app.logger.info("Creating test data...")
create_test_data()
@manager.option('-u', '--username', dest='username')
@manager.option('-p', '--password', dest='password')
@manager.option('-e', '--email', dest='email')
def create_admin(username=None, password=None, email=None):
if not (username and password and email):
username = prompt("Username")
email = prompt("A valid email address")
password = prompt_pass("Password")
create_admin_user(username=username, password=password, email=email)
@manager.option('-u', '--username', dest='username')
@manager.option('-p', '--password', dest='password')
@manager.option('-e', '--email', dest='email')
def initflaskbb(username=None, password=None, email=None):
"""Initializes FlaskBB with all necessary data"""
flask_app.logger.info("Creating default data...")
try:
create_default_groups()
create_default_settings()
except IntegrityError:
flask_app.logger.error("Couldn't create the default data because it already "
"exist!")
if prompt_bool("Do you want to recreate the database? (y/n)"):
db.session.rollback()
db.drop_all()
db.create_all()
create_default_groups()
create_default_settings()
else:
sys.exit(0)
except OperationalError:
flask_app.logger.error("No database found.")
if prompt_bool("Do you want to create the database now? (y/n)"):
db.session.rollback()
db.create_all()
create_default_groups()
create_default_settings()
else:
sys.exit(0)
flask_app.logger.info("Creating admin user...")
if username and password and email:
create_admin_user(username=username, password=password, email=email)
else:
create_admin()
flask_app.logger.info("Creating welcome forum...")
create_welcome_forum()
flask_app.logger.info(
"Congratulations! FlaskBB has been successfully installed")
@manager.command
def update_softwares_rates():
softwares = Software.query.all()
for i in range(2):
for s in softwares:
s.populate()
Software.set_rankings(softwares)
@manager.command
def init_rankings():
Software.set_rankings()
@manager.command
def fix_upvotes():
sentence_soft_map = SentenceSoftwareMapping.query.all()
for o in sentence_soft_map:
if o.software.name in ('Proteowizard', 'Rdisop'):
o.upvote -= 1
o.save()
# upvotes = Upvote.query.all()
# for key, group in groupby(upvotes, key=lambda _: _.sentence_software_mapping.software_id):
# up = set(group)
# i = 0
# if key in ('Proteowizard', 'Rdisop'):
# for u in up:
# if u.user_id == GUEST_USER_ID:
# u.delete()
# break
# to_break = False
# while i < 3 and not to_break:
# for u in up:
# if u.user_id == GUEST_USER_ID:
# u.delete()
# up.remove(u)
# i += 1
# break
# if GUEST_USER_ID not in {u.user_id for u in up}:
# to_break = True
@manager.command
def reset_upvotes():
sentence_soft_map = SentenceSoftwareMapping.query.all()
for o in sentence_soft_map:
o.upvote = 0
o.save()
upvotes = Upvote.query.all()
for u in upvotes:
u.delete()
@manager.command
def fix_procon_upvote():
procon_upvote = ProCons.query.all()
for p in procon_upvote:
if p.software_name == 'Proteowizard':
p.kind = 'pro'
p.save()
if __name__ == "__main__":
manager.run()