-
Notifications
You must be signed in to change notification settings - Fork 0
/
manage.py
127 lines (106 loc) · 4.55 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
import os
from app.api.helpers.db import save_to_db
from app.models.event import Event, get_new_event_identifier
from app import manager
from app import current_app as app
from app.models import db
from app.models.speaker import Speaker
from app.models.module import Module
from populate_db import populate
from flask_migrate import stamp
from sqlalchemy.engine import reflection
from tests.all.integration.auth_helper import create_super_admin
@manager.command
def list_routes():
import urllib
output = []
for rule in app.url_map.iter_rules():
methods = ','.join(rule.methods)
line = urllib.parse.unquote("{:50s} {:20s} {}".format(
rule.endpoint, methods, rule))
output.append(line)
for line in sorted(output):
print(line)
@manager.command
def add_event_identifier():
events = Event.query.all()
for event in events:
event.identifier = get_new_event_identifier()
save_to_db(event)
@manager.command
def fix_digit_identifier():
events = Event.query.filter(Event.identifier.op('~')('^[0-9\.]+$')).all()
for event in events:
event.identifier = get_new_event_identifier()
db.session.add(event)
db.session.commit()
@manager.option('-n', '--name', dest='name', default='all')
@manager.option('-s', '--switch', dest='switch', default='off')
def module(name, switch):
keys = [i.name for i in Module.__table__.columns][1:]
convey = {"on": True, "off": False}
if switch not in ['on', 'off']:
print("Choose either state On/Off")
elif name == 'all':
for key in keys:
setattr(Module.query.first(), key, convey[switch])
print("Module %s turned %s" % (key, switch))
elif name in keys:
setattr(Module.query.first(), name, convey[switch])
print("Module %s turned %s" % (name, switch))
else:
print("Invalid module selected")
db.session.commit()
@manager.option('-e', '--event', help='Event ID. Eg. 1')
def fix_speaker_images(event):
from app.helpers.sessions_speakers.speakers import speaker_image_sizes
from app.helpers.sessions_speakers.speakers import save_resized_photo
import urllib
from app.helpers.storage import generate_hash
event_id = int(event)
image_sizes = speaker_image_sizes()
speakers = Speaker.query.filter_by(event_id=event_id).all()
for speaker in speakers:
if speaker.photo and speaker.photo.strip() != '':
file_relative_path = 'static/media/temp/' + generate_hash(str(speaker.id)) + '.jpg'
file_path = app.config['BASE_DIR'] + '/' + file_relative_path
urllib.urlretrieve(speaker.photo, file_path)
speaker.small = save_resized_photo(file_path, event_id, speaker.id, 'small', image_sizes)
speaker.thumbnail = save_resized_photo(file_path, event_id, speaker.id, 'thumbnail', image_sizes)
speaker.icon = save_resized_photo(file_path, event_id, speaker.id, 'icon', image_sizes)
db.session.add(speaker)
os.remove(file_path)
print("Downloaded " + speaker.photo + " into " + file_relative_path)
print("Processed - " + str(speaker.id))
db.session.commit()
@manager.option('-c', '--credentials', help='Super admin credentials. Eg. username:password')
def initialize_db(credentials):
with app.app_context():
populate_data = True
inspector = reflection.Inspector.from_engine(db.engine)
table_name = 'events'
table_names = inspector.get_table_names()
print("[LOG] Existing tables:")
print("[LOG] " + ','.join(table_names))
if table_name not in table_names:
print("[LOG] Table not found. Attempting creation")
try:
db.create_all()
stamp()
except Exception:
populate_data = False
print("[LOG] Could not create tables. Either database does not exist or tables already created")
if populate_data:
credentials = credentials.split(":")
admin_email = os.environ.get('SUPER_ADMIN_EMAIL', credentials[0])
admin_password = os.environ.get('SUPER_ADMIN_PASSWORD', credentials[1])
create_super_admin(admin_email, admin_password)
populate()
else:
print("[LOG] Tables already exist. Skipping data population & creation.")
@manager.command
def prepare_kubernetes_db(credentials='open_event_test_user@fossasia.org:fossasia'):
with app.app_context():
initialize_db(credentials)
if __name__ == "__main__":
manager.run()