-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmanage.py
executable file
·233 lines (187 loc) · 5.81 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
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
227
228
229
230
231
#!/usr/bin/env python
# coding=utf-8
from datetime import datetime
from dircache import listdir
from os import mkdir, system
import os
from os.path import exists
import csv
import bleach
from flask import current_app as app
from flask.ext.script import Manager
from icalendar import Calendar
from website.application import create_app, db
from website.crm.models import Speaker, Talk, Track2
DEBUG = True
OSDC_DATA = u"""Maik,Aussendorf,[email protected]
Sam,Bessalah,[email protected]
Sébastien,Blanc,[email protected]
François-Xavier,Bois,[email protected]
Amaury,Bouchard,[email protected]
Julien,Bourdin,[email protected]
Remi,Collet,[email protected]
Christian,Couder,[email protected]
Bertrand,Dechoux,[email protected]
Kamel Ibn Aziz,Derouiche,[email protected]
Christophe,Desclaux,[email protected]
Laurent,Doguin,[email protected]
Sébastien,Douche,[email protected]
Christophe,Fergeau,[email protected]
Haïkel,Guémar,[email protected]
Viktor,Horvath,[email protected]
Damien,Krotkine,[email protected]
Fabrice,Le Fessant,[email protected]
Mathieu,Lecarme,[email protected]
Jonathan,MERCIER,[email protected]
Bruno,Michel,[email protected]
Gael,Pasgrimaud,[email protected]
Julien,Pauli,[email protected]
Rodolphe,Quiédeville,[email protected]
Philippe,Robin,[email protected]
Frank,Rousseau,[email protected]
Romuald,Rozan,[email protected]
Michael,Scherer,[email protected]
Pierre,Schweitzer,[email protected]
Basile,Starynkevitch,[email protected]
Christophe,Villeneuve,[email protected]"""
manager = Manager(create_app)
@manager.shell
def make_shell_context():
"""
Updates shell. (XXX: not sure what this does).
"""
return dict(app=app, db=db)
@manager.command
def dump_routes():
"""
Dump all the routes declared by the application.
"""
for rule in app.url_map.iter_rules():
print rule, rule.methods, rule.endpoint
@manager.command
def load_data():
feedback = csv.reader(open("feedback.csv"))
for row in feedback:
row = map(lambda x: unicode(x, "utf8"), row)
date = datetime.strptime(row[3], "%Y-%m-%d %H:%M:%S.%f")
reg = Registration(email=row[0],
first_name=u"",
last_name=row[1],
organization=row[2],
date=date)
db.session.add(reg)
db.session.commit()
@manager.command
def create_db():
if not exists("data"):
mkdir("data")
with app.app_context():
db.create_all()
@manager.command
def drop_db():
"""
Drop the DB.
"""
# TODO: ask for confirmation.
with app.app_context():
db.drop_all()
@manager.command
def add_user(email, password):
from website.security import User2
user = User2(email=email, password=password, active=True)
db.session.add(user)
db.session.commit()
print "Password updated"
@manager.command
def set_password(email, password):
from website.security import User2
user = User2.query.filter(User2.email==email).one()
user.password = password
db.session.commit()
print "Password updated"
@manager.command
def index_content():
whoosh = app.extensions['whoosh']
pages = app.extensions['pages']
for page in pages:
doc = {}
doc['path'] = page['path']
doc['title'] = unicode(page['title'])
doc['content'] = page.body
doc['summary'] = bleach.clean(page.html, tags=[], strip=True)
if len(doc['summary']) > 200:
doc['summary'] = doc['summary'][0:200] + '...'
whoosh.add_document(doc)
@manager.command
def build():
""" Builds this site.
"""
print("Building website...")
app.debug = False
asset_manager = app.extensions['asset_manager']
asset_manager.config['ASSETS_DEBUG'] = False
freezer = app.extensions['freezer']
freezer.freeze()
system("cp ./static/*.ico ./build/")
system("cp ./static/*.txt ./build/")
system("cp ./static/*.xml ./build/")
print("Done.")
@manager.command
def load_osdc():
cal = Calendar.from_ical(open('data/osdc.ics','rb').read())
speakers = {}
osdc1 = Track2.query.get(28)
osdc2 = Track2.query.get(48)
osdc3 = Track2.query.get(49)
for component in cal.walk():
if component.name != 'VEVENT':
continue
title = component['summary']
room_name = component['location']
abstract = component['description']
speaker_name = component['organizer']
starts_at = component['dtstart'].dt
ends_at = component['dtend'].dt
duration = int((ends_at - starts_at).total_seconds() / 60)
speaker = speakers.get(speaker_name, None)
if not speaker:
speaker = Speaker(first_name="", last_name=speaker_name)
speakers[speaker_name] = speaker
talk = Talk(title=title, abstract=abstract,
starts_at=starts_at, duration=duration)
talk.speakers.append(speaker)
if 'Gopher' in room_name:
talk.track = osdc3
else:
if starts_at.day == 4:
talk.track = osdc1
else:
talk.track = osdc2
db.session.commit()
@manager.command
def update_osdc():
for line in OSDC_DATA.split("\n"):
first_name, last_name, email = line.split(",")
name = first_name + " " + last_name
speaker = Speaker.query.filter(Speaker.last_name == name).first()
if speaker:
print "Updating speaker", name
speaker.first_name = first_name
speaker.last_name = last_name
speaker.email = email
db.session.commit()
@manager.command
def serve(server='0.0.0.0', port=5002, debug=DEBUG):
""" Serves this site.
"""
if not debug:
import logging
file_handler = FileHandler("error.log")
file_handler.setLevel(logging.WARNING)
app.logger.addHandler(file_handler)
asset_manager = app.extensions['asset_manager']
asset_manager.config['ASSETS_DEBUG'] = debug
app.debug = debug
app.run(host=server, port=port, debug=debug)
if __name__ == '__main__':
manager.run()