-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsponsormatch.py
155 lines (126 loc) · 3.6 KB
/
sponsormatch.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
"""This script runs the application and contains some
convenient command line tools
"""
import os
import click
import sys
from dotenv import load_dotenv
load_dotenv()
from app import create_app
from app.fake import FakeDataGenerator
from app.models import (
User,
Role,
Image,
Event,
EventType,
Venue,
EventCategory,
ImageType,
Package,
Video,
Permission,
Sponsorship,
)
from flask_migrate import upgrade
from app.extensions import db, migrate
from app.error_handlers import (
page_not_found,
internal_server_error,
bad_request,
forbidden,
)
from werkzeug.exceptions import (
NotFound,
InternalServerError,
BadRequest,
Forbidden,
)
from app.search import MatchQuery, BooleanQuery, ElasticsearchClient
app = create_app(
os.environ.get("FLASK_CONFIG", "default"),
os.environ.get("USE_ELASTICSEARCH", 1) == 1,
)
# Register application wide error handlers
app.register_error_handler(NotFound, page_not_found)
app.register_error_handler(InternalServerError, internal_server_error)
app.register_error_handler(BadRequest, bad_request)
app.register_error_handler(Forbidden, forbidden)
# Register with Flask-Migrate
migrate.init_app(app, db)
@app.shell_context_processor
def make_shell_context():
"""Allow the models to be automatically imported
when a flask shell session is started
"""
return dict(
db=db,
User=User,
Role=Role,
Image=Image,
Event=Event,
EventType=EventType,
Venue=Venue,
EventCategory=EventCategory,
ImageType=ImageType,
Package=Package,
Video=Video,
Permission=Permission,
Sponsorship=Sponsorship,
MatchQuery=MatchQuery,
BooleanQuery=BooleanQuery,
)
@app.context_processor
def utility_functions():
"""Allow utility functions to be available
for use throught templates.
"""
def print_in_template(message):
print(str(message))
return dict(debug=print_in_template)
@app.cli.command()
@click.option(
"--fake-data/--no-fake-data",
default=False,
help="Setup the developement environment.",
)
def setup_environment(fake_data):
"""Cli command to setup the development environment. Starts up
Elasticsearch, sets up the database and inserts fake data into
the database if request.
"""
app.sqlalchemy_search_middleware._elasticsearch_client.delete_index(Event.__tablename__)
db.drop_all()
db.create_all()
# create or update roles, event types, categories, and image types
Role.insert_roles()
EventType.insert_event_types()
EventCategory.insert_event_categories()
ImageType.insert_image_types()
# add fake data to the database
if fake_data:
fake = FakeDataGenerator(48, 48)
fake.add_all()
@app.cli.command()
@click.option(
"--fake-data/--no-fake-data",
default=False,
help="Add fake data to the database before deployment.",
)
def deploy(fake_data):
"""Run the below set of tasks before deployment."""
# migrate database to latest revision
upgrade()
# create or update roles, event types, categories, and image types
Role.insert_roles()
EventType.insert_event_types()
EventCategory.insert_event_categories()
ImageType.insert_image_types()
es_client = ElasticsearchClient(app.config["ELASTICSEARCH_URL"])
es_client.create_index("events")
# add fake data to the database if there isn't already fake data in the tables
if fake_data:
fake = FakeDataGenerator(48, 48)
fake.add_all()
if __name__ == "__main__":
app.run(debug=True)