forked from fossasia/open-event-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
manage.py
156 lines (131 loc) · 5.19 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
import logging
import os
from flask_migrate import MigrateCommand, stamp
from flask_script import Manager
from sqlalchemy import or_
from sqlalchemy.engine import reflection
from app.api.helpers.db import save_to_db
from app.instance import current_app as app
from app.api.helpers.tasks import resize_event_images_task, resize_speaker_images_task
from app.models import db
from app.models.event import Event, get_new_event_identifier
from app.models.speaker import Speaker
from populate_db import populate
from tests.all.integration.auth_helper import create_super_admin
logger = logging.getLogger(__name__)
manager = Manager(app)
manager.add_command('db', MigrateCommand)
@manager.command
def list_routes():
import urllib
output = []
for rule in app.url_map.iter_rules():
methods = ','.join(rule.methods)
line = urllib.parse.unquote(f"{rule.endpoint:50s} {methods:20s} {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_event_and_speaker_images():
events = Event.query.filter(
Event.original_image_url.isnot(None),
or_(
Event.thumbnail_image_url == None,
Event.large_image_url == None,
Event.icon_image_url == None,
),
).all()
logger.info('Resizing images of %s events...', len(events))
for event in events:
logger.info('Resizing Event %s', event.id)
resize_event_images_task.delay(event.id, event.original_image_url)
speakers = Speaker.query.filter(
Speaker.photo_url.isnot(None),
or_(
Speaker.icon_image_url == None,
Speaker.small_image_url == None,
Speaker.thumbnail_image_url == None,
),
).all()
logger.info('Resizing images of %s speakers...', len(speakers))
for speaker in speakers:
logging.info('Resizing Speaker %s', speaker.id)
resize_speaker_images_task.delay(speaker.id, speaker.photo_url)
@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('-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='[email protected]:fossasia'):
with app.app_context():
initialize_db(credentials)
if __name__ == "__main__":
manager.run()