-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmanage.py
executable file
·159 lines (118 loc) · 3.3 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
#!/usr/bin/env python
# coding=utf-8
from datetime import datetime
from logging import FileHandler
from os import mkdir, system
import os
from os.path import exists
import csv
import random
import string
import bleach
from flask import current_app as app
from flask.ext.script import Manager
from abilian.core.subjects import User
from website.application import create_app, db
DEBUG = True
OSDC_DATA = u""""""
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()
def gen_password(length=12):
chars = string.ascii_letters + string.digits + '!@#$%^&*()'
random.seed = (os.urandom(1024))
return ''.join(random.choice(chars) for i in range(length))
@manager.command
def add_user(email):
password = gen_password(12)
print password
user = User(email=email, password=password)
db.session.add(user)
db.session.commit()
print("Password updated: {}".format(password))
@manager.command
def import_excel():
from website.excel import Loader
loader = Loader("instance/data/OWF14 Program.xlsx")
loader.load()
#db.session.commit()
@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 serve(server='0.0.0.0', port=5000, 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()