forked from lanmomo/lanmomo-website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
executable file
·89 lines (65 loc) · 2.29 KB
/
app.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
#!/usr/bin/env python3
import os
import sys
import json
import re
from flask import Flask, send_from_directory, jsonify, request
from database import db_session, init_db, init_engine
from models import Subscribtion
app = Flask(__name__)
@app.route('/api/games', methods=['GET'])
def get_games():
return jsonify(games), 200
@app.route('/api/servers', methods=['GET'])
def get_servers():
return jsonify({'error': 'Not implemented'}), 500
@app.route('/api/servers', methods=['POST'])
def update_server():
return jsonify({'error': 'Not implemented'}), 500
@app.route('/api/subscribe', methods=['POST'])
def sub():
print('wat')
req = request.get_json()
if 'email' in req:
email = req['email']
match = re.search(r"(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)",
email)
if match:
return subscribe_email(email)
else:
return bad_request()
return bad_request()
@app.route('/')
def index():
return send_from_directory('public', 'index.html')
@app.route('/<path:path>')
def static_files(path):
return send_from_directory('public', path)
@app.teardown_appcontext
def shutdown_session(exception=None):
db_session.remove()
def bad_request():
return jsonify({'message': 'Les informations données sont invalides ou ' +
'incomplètes'}), 400
def subscribe_email(email):
if Subscribtion.query.filter(Subscribtion.email == email).count() == 0:
sub = Subscribtion(email)
db_session.add(sub)
db_session.commit()
return jsonify({'message': 'Votre courriel a été ajouté sur la liste.' +
'Vous receverez un courriel dès que les billets ' +
'seront en vente !'}), 200
else:
return jsonify({'message': 'Votre courriel est déjà la liste ! ' +
'Vous receverez un courriel dès que les billets ' +
'seront en vente !'}), 200
def setup(conf_path):
global app, games
app.config.from_pyfile(conf_path)
init_engine(app.config['DATABASE_URI'])
init_db()
with open('config/games.json') as data_file:
games = json.load(data_file)
return app
if __name__ == '__main__':
setup('config/default_config.py').run(debug=True)