-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy path__init__.py
More file actions
158 lines (139 loc) · 4.48 KB
/
__init__.py
File metadata and controls
158 lines (139 loc) · 4.48 KB
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
from flask import *
from os import environ
from flaskext.markdown import Markdown
from flask_pyoidc.provider_configuration import *
from flask_pyoidc.flask_pyoidc import OIDCAuthentication
from boto3 import client
from game_night.auth import require_gamemaster, require_read_key
from game_night.database import *
from game_night.game import Game
app = Flask(__name__)
app.config.update(
PREFERRED_URL_SCHEME = environ.get('URL_SCHEME', 'https'),
SECRET_KEY = environ['SECRET_KEY'], SERVER_NAME = environ['SERVER_NAME'],
WTF_CSRF_ENABLED = False
)
app.jinja_env.lstrip_blocks = True
app.jinja_env.trim_blocks = True
app.url_map.strict_slashes = False
Markdown(app)
_config = ProviderConfiguration(
environ['OIDC_ISSUER'],
client_metadata = ClientMetadata(
environ['OIDC_CLIENT_ID'], environ['OIDC_CLIENT_SECRET']
)
)
_auth = OIDCAuthentication({'default': _config}, app)
_s3 = client(
's3', aws_access_key_id = environ['S3_KEY'],
aws_secret_access_key = environ['S3_SECRET'],
endpoint_url = environ['S3_ENDPOINT']
)
@app.route('/api')
@require_read_key
def api():
return jsonify(list(get_games(request.args)))
@app.route('/api/count')
@require_read_key
def api_count():
return jsonify(get_count(request.args))
@app.route('/api/key', methods = ['GET', 'POST'])
@require_gamemaster
def api_key():
return jsonify(generate_api_key())
@app.route('/api/keys')
@require_gamemaster
def api_keys():
return jsonify(list(get_api_keys()))
@app.route('/api/newest')
@require_read_key
def api_newest():
return jsonify(list(get_newest_games(request.args)))
@app.route('/api/owners')
@require_read_key
def api_owners():
return jsonify(list(get_owners(request.args)))
@app.route('/api/random')
@app.route('/api/random/<int:sample_size>')
@require_read_key
def api_random(sample_size = 1):
sample = list(get_random_games(request.args, sample_size))
return jsonify(sample[0] if len(sample) == 1 else sample)
@app.route('/api/submitters')
@require_read_key
def api_submitters():
return jsonify(list(get_submitters(request.args)))
@app.route('/delete/<game_name>', methods = ['POST'])
@_auth.oidc_auth('default')
def delete(game_name):
if not delete_game(game_name, session['userinfo']['preferred_username']):
abort(404)
return redirect('/')
def _get_template_variables():
return {
'gamemaster': is_gamemaster(session['userinfo']['preferred_username']),
'image_url': environ['IMAGE_URL'],
'owners': get_owners(), 'players': get_players(),
'submitters': get_submitters()
}
@app.route('/')
@_auth.oidc_auth('default')
def index():
return render_template(
'index.html', games = get_games(request.args),
**_get_template_variables()
)
@app.route('/game/<game_name>')
@_auth.oidc_auth('default')
def game(game_name):
g = get_game(game_name)
if not g:
abort(404)
return render_template(
'game.html', expansions = list(get_game_names(g['name'])), game = g,
**_get_template_variables()
)
@app.route('/random')
@_auth.oidc_auth('default')
def random():
return render_template(
'index.html', games = get_random_games(request.args, 1),
**_get_template_variables()
)
@app.route('/submissions')
@_auth.oidc_auth('default')
def submissions():
return render_template(
'submissions.html',
games = get_submissions(
request.args,
session['userinfo']['preferred_username']
), **_get_template_variables()
)
@app.route('/submit', methods = ['GET', 'POST'])
@_auth.oidc_auth('default')
def submit():
if request.method == 'GET':
return render_template(
'submit.html',
form = Game(session['userinfo']['preferred_username']),
game_names = get_game_names(), **_get_template_variables()
)
game = Game()
if not game.validate():
return render_template(
'submit.html',
error = next(iter(game.errors.values()))[0], form = game,
game_names = get_game_names(), **_get_template_variables()
)
game = game.data
game = {k: v.strip() if type(v) == str else v for k,v in game.items()}
_s3.upload_fileobj(
game['image'], environ['S3_BUCKET'], game['name'] + '.jpg',
ExtraArgs = {
'ACL': 'public-read', 'ContentType': game['image'].content_type
}
)
insert_game(game, session['userinfo']['preferred_username'])
flash('Game successfully submitted.')
return redirect('/')