-
Notifications
You must be signed in to change notification settings - Fork 14
/
views.py
183 lines (158 loc) · 6.26 KB
/
views.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import boardlib.api.aurora
import flask
import climbdex.db
import json
blueprint = flask.Blueprint("view", __name__)
@blueprint.route("/")
def index():
return flask.render_template(
"boardSelection.html.j2",
)
@blueprint.route("/filter")
def filter():
board_name = flask.request.args.get("board")
layout_id = flask.request.args.get("layout")
size_id = flask.request.args.get("size")
set_ids = flask.request.args.getlist("set")
return flask.render_template(
"filterSelection.html.j2",
params=flask.request.args,
board_name=board_name,
layout_name=climbdex.db.get_data(
board_name, "layout_name", {"layout_id": layout_id}
)[0][0],
size_name=climbdex.db.get_data(
board_name, "size_name", {"layout_id": layout_id, "size_id": size_id}
)[0][0],
angles=climbdex.db.get_data(board_name, "angles", {"layout_id": layout_id}),
grades=climbdex.db.get_data(board_name, "grades"),
colors=climbdex.db.get_data(board_name, "colors", {"layout_id": layout_id}),
**get_draw_board_kwargs(board_name, layout_id, size_id, set_ids),
)
@blueprint.route("/results")
def results():
board_name = flask.request.args.get("board")
layout_id = flask.request.args.get("layout")
size_id = flask.request.args.get("size")
set_ids = flask.request.args.getlist("set")
login_cookie = flask.request.cookies.get(f"{board_name}_login")
ticked_climbs = get_ticked_climbs(board_name, login_cookie) if login_cookie else []
attempted_climbs = get_bids(board_name, login_cookie) if login_cookie else []
placement_positions = get_placement_positions(board_name, layout_id, size_id)
return flask.render_template(
"results.html.j2",
app_url=boardlib.api.aurora.WEB_HOSTS[board_name],
colors=climbdex.db.get_data(board_name, "colors", {"layout_id": layout_id}),
ticked_climbs=ticked_climbs,
attempted_climbs=attempted_climbs,
placement_positions=placement_positions,
grades=climbdex.db.get_data(board_name, "grades"),
led_colors=get_led_colors(board_name, layout_id),
**get_draw_board_kwargs(
board_name,
layout_id,
size_id,
set_ids,
),
)
@blueprint.route("/<board_name>/beta/<uuid>")
def beta(board_name, uuid):
beta = climbdex.db.get_data(board_name, "beta", {"uuid": uuid})
climb_name = climbdex.db.get_data(board_name, "climb", {"uuid": uuid})[0][0]
return flask.render_template(
"beta.html.j2",
beta=beta,
climb_name=climb_name,
)
@blueprint.route("/create")
def create():
board_name = flask.request.args.get("board")
layout_id = flask.request.args.get("layout")
size_id = flask.request.args.get("size")
set_ids = flask.request.args.getlist("set")
colors = climbdex.db.get_data(board_name, "colors", {"layout_id": layout_id})
app_url = boardlib.api.aurora.WEB_HOSTS[board_name]
placement_positions = get_placement_positions(board_name, layout_id, size_id)
return flask.render_template(
"climbCreation.html.j2",
app_url=app_url,
board=board_name,
layout_name=climbdex.db.get_data(
board_name, "layout_name", {"layout_id": layout_id}
)[0][0],
size_name=climbdex.db.get_data(
board_name, "size_name", {"layout_id": layout_id, "size_id": size_id}
)[0][0],
colors=colors,
led_colors=get_led_colors(board_name, layout_id),
placement_positions=placement_positions,
**get_draw_board_kwargs(board_name, layout_id, size_id, set_ids),
)
def get_draw_board_kwargs(board_name, layout_id, size_id, set_ids):
images_to_holds = {}
for set_id in set_ids:
image_filename = climbdex.db.get_data(
board_name,
"image_filename",
{"layout_id": layout_id, "size_id": size_id, "set_id": set_id},
)[0][0]
image_url = f"{boardlib.api.aurora.API_HOSTS[board_name]}/img/{image_filename}"
images_to_holds[image_url] = climbdex.db.get_data(
board_name, "holds", {"layout_id": layout_id, "set_id": set_id}
)
size_dimensions = climbdex.db.get_data(
board_name, "size_dimensions", {"size_id": size_id}
)[0]
return {
"images_to_holds": images_to_holds,
"edge_left": size_dimensions[0],
"edge_right": size_dimensions[1],
"edge_bottom": size_dimensions[2],
"edge_top": size_dimensions[3],
}
def get_ticked_climbs(board, login_cookie):
login_info = flask.json.loads(login_cookie)
logbook = boardlib.api.aurora.get_logbook(
board, login_info["token"], login_info["user_id"]
)
ticked_climbs = {}
normal_tick = 0
mirror_tick = 1
both_tick = 2
for log in logbook:
key = f'{log["climb_uuid"]}-{log["angle"]}'
tick_type = mirror_tick if log["is_mirror"] else normal_tick
existing_tick = ticked_climbs.get(key)
ticked_climbs[key] = (
both_tick
if existing_tick is not None and existing_tick != tick_type
else tick_type
)
return ticked_climbs
def get_bids(board, login_cookie):
db_path = f"data/{board}/db.sqlite3"
login_info = json.loads(login_cookie)
user_id = login_info["user_id"]
token = login_info["token"]
aggregated_logbook = boardlib.api.aurora.logbook_entries(board, token=token,username=None,password=None, user_id=user_id, db_path=db_path, grade_type="font",aggregate=True)
aggregated_json = aggregated_logbook.to_dict(orient='records')
formatted_json = {
entry['uid']: {
'total_tries': entry['tries'],
'total_sessions': entry['sessions'],
'last_try': entry['date']
} for entry in aggregated_json
}
return formatted_json
def get_placement_positions(board_name, layout_id, size_id):
binds = {"layout_id": layout_id, "size_id": size_id}
return {
placement_id: position
for placement_id, position in climbdex.db.get_data(board_name, "leds", binds)
}
def get_led_colors(board_name, layout_id):
binds = {"layout_id": layout_id}
return {
role_id: color
for role_id, color in climbdex.db.get_data(board_name, "led_colors", binds)
}