-
Notifications
You must be signed in to change notification settings - Fork 12
/
food.py
129 lines (101 loc) · 4.04 KB
/
food.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
import sentry_sdk
from flask import request, Flask, send_from_directory
from flask_migrate import Migrate
from sentry_sdk.integrations.flask import FlaskIntegration
from werkzeug import exceptions
from werkzeug.datastructures import ImmutableMultiDict
from models import db
import config
if config.use_sentry:
sentry_sdk.init(
dsn=config.sentry_dsn,
integrations=[FlaskIntegration()],
# Set traces_sample_rate to 1.0 to capture 100%
# of transactions for performance monitoring.
# We recommend adjusting this value in production.
traces_sample_rate=1.0,
)
app = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"] = config.db_url
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
app.config["SECRET_KEY"] = config.secret_key
app.config["OIDC_CLIENT_SECRETS"] = config.oidc_client_secrets_json
app.config["OIDC_SCOPES"] = "openid profile"
app.config["OIDC_OVERWRITE_REDIRECT_URI"] = config.oidc_redirect_uri
# Ensure DB tables are created.
db.init_app(app)
# Ensure the DB is able to determine migration needs.
migrate = Migrate(app, db, compare_type=True)
with app.app_context():
# Ensure our database is present.
db.create_all()
# Import functions with routes.
# TODO(spotlightishere): Convert to blueprints
import responses
import thepantry
action_list = {
"webApi_document_template": responses.document_template,
"webApi_area_list": responses.area_list,
"webApi_category_list": responses.category_list,
"webApi_area_shopinfo": responses.shop_info,
"webApi_shop_list": responses.shop_list,
"webApi_shop_one": responses.shop_one,
"webApi_menu_list": responses.menu_list,
"webApi_item_list": responses.item_list,
"webApi_item_one": responses.item_one,
"webApi_Authkey": responses.generate_auth_key,
"webApi_basket_list": responses.basket_list,
"webApi_basket_reset": responses.basket_reset,
"webApi_basket_add": responses.basket_add,
"webApi_validate_condition": responses.validate_condition,
"webApi_order_done": responses.order_done,
"webApi_inquiry_done": responses.inquiry_done,
"webApi_basket_delete": responses.basket_delete,
"webApi_basket_modify": responses.basket_modify,
}
@app.route("/nwapi.php", methods=["GET"])
def base_api():
try:
# These values should be consistent for both v1 and v512.
if request.args["platform"] != "wii":
return exceptions.BadRequest()
action = request.args["action"]
return action_list[action](request)
except KeyError:
# This is not an action or a format we know of.
return exceptions.NotFound()
def print_multi(passed_dict):
if isinstance(passed_dict, ImmutableMultiDict):
passed_dict = passed_dict.items(multi=True)
for key, value in passed_dict:
try:
# Encode as UTF-8
value = value.encode("shift-jis").decode("utf-8")
print(f"{key} -> {value}")
except Exception as e:
# If it errors, leave as is with a note.
print(f"An error occurred while decoding key {e}")
print(f"Its value is '{value}' (not decoded)")
@app.route("/nwapi.php", methods=["POST"])
def error_api():
if request.form.get("action") is None:
print("Received an error!")
print_multi(request.args)
print_multi(request.form)
return action_list["webApi_document_template"](request)
try:
# These values should be consistent for both v1 and v512.
if request.form["platform"] != "wii":
return exceptions.BadRequest()
action = request.form["action"]
return action_list[action](request)
except KeyError:
# This is not an action or a format we know of.
return exceptions.NotFound()
if app.debug:
@app.route("/logoimg2/<filename>")
def serve_logo(filename):
return send_from_directory("images", filename)
@app.route("/itemimg/<category_code>/<filename>")
def serve_food_image(category_code, filename):
return send_from_directory(f"images/{category_code}/", filename)