-
Notifications
You must be signed in to change notification settings - Fork 0
/
routes.py
140 lines (103 loc) · 5.4 KB
/
routes.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
import flask
from flask import request, jsonify
from database import database
from database_query import DatabaseQuery
from reponses import ErrorResponses, SuccessResponses
from frozendict import frozendict
from utilities import filter_by_arglist
####################
# Global variables #
####################
_flask_application = flask.Flask(__name__)
_flask_application.config["DEBUG"] = True
@_flask_application.errorhandler(404)
def _route_page_not_found(error):
return ErrorResponses.not_found_error(error)
@_flask_application.route('/', methods=['GET'])
def _route_home():
return ErrorResponses.home_page_error()
@_flask_application.route('/<version>', methods=['GET'])
def _route_get_all_platforms(version):
if not version:
return ErrorResponses.version_not_inputted()
if not database.has_version(version):
return ErrorResponses.version_doesnt_exist(version)
db = database.to_dict(version)
db = filter_by_arglist('field', DatabaseQuery.filter_by_fields, db)
db = filter_by_arglist('q', DatabaseQuery.filter_by_queries, db)
db = filter_by_arglist('name', DatabaseQuery.filter_by_names, db)
return db
@_flask_application.route('/<version>/<platform_name>', methods=['GET'])
def _route_get_platform(version, platform_name):
if not version or not platform_name:
return ErrorResponses.version_or_name_not_inputted()
if not database.has_version(version):
return ErrorResponses.version_doesnt_exist(version)
if not database.contains_key(version, platform_name):
return ErrorResponses.platform_not_found(platform_name)
return database.get_by_key(version, platform_name)
@_flask_application.route('/<version>/<platform_name>', methods=['POST'])
def _route_add_platform(version, platform_name):
if not version or not platform_name:
return ErrorResponses.version_or_name_not_inputted()
if not database.has_version(version):
return ErrorResponses.version_doesnt_exist(version)
if database.contains_key(version, platform_name):
return ErrorResponses.platform_already_exists(platform_name)
database.set_by_key(version, platform_name, dict())
return SuccessResponses.successfully_added_platform()
@_flask_application.route('/<version>/<platform_name>', methods=['DELETE'])
def _route_remove_platform(version, platform_name):
if not version or not platform_name:
return ErrorResponses.version_or_name_not_inputted()
if not database.has_version(version):
return ErrorResponses.version_doesnt_exist(version)
if not database.contains_key(version, platform_name):
return ErrorResponses.platform_not_found(platform_name)
database.rm_by_key(version, platform_name)
return SuccessResponses.successfully_removed_platform()
@_flask_application.route('/<version>/<platform_name>/<field>', methods=['POST'])
def _route_change_platform_field(version, platform_name, field):
if not version or not platform_name or not field:
return ErrorResponses.version_name_or_field_not_inputted()
if not database.has_version(version):
return ErrorResponses.version_doesnt_exist(version)
value = request.form.get('value')
if not value:
return ErrorResponses.missing_value_in_form()
# If platform doesn't exist, give it a default value.
if not database.contains_key(version, platform_name):
database.set_by_key(version, platform_name, dict())
database.change_field_for_key(version, platform_name, field, value)
return SuccessResponses.successfully_changed_field()
@_flask_application.route('/<version>/<platform_name>/<field>', methods=['DELETE'])
def _route_remove_platform_field(version, platform_name, field):
if not version or not platform_name or not field:
return ErrorResponses.version_name_or_field_not_inputted()
if not database.has_version(version):
return ErrorResponses.version_doesnt_exist(version)
if not database.contains_key(version, platform_name):
return ErrorResponses.platform_not_found(platform_name)
if not database.key_contains_field(version, platform_name, field):
return ErrorResponses.field_missing_for_platform(field, platform_name)
database.remove_field_for_key(version, platform_name, field)
return SuccessResponses.successfully_removed_field()
@_flask_application.route('/<version>/<platform_name>/<field>', methods=['GET'])
def _route_get_field(version, platform_name, field):
if not version or not platform_name or not field:
return ErrorResponses.version_name_or_field_not_inputted()
if not database.has_version(version):
return ErrorResponses.version_doesnt_exist(version)
if not database.contains_key(version, platform_name):
return ErrorResponses.platform_not_found(platform_name)
if not database.key_contains_field(version, platform_name, field):
return ErrorResponses.field_missing_for_platform(field, platform_name)
return database.get_field_by_key(version, platform_name, field)
@_flask_application.route('/<version>/load', methods=['GET'])
def _route_load(version):
if not version:
return ErrorResponses.version_not_inputted()
database.reload_version(version)
return SuccessResponses.successfully_loaded_version()
def run_flask():
_flask_application.run()