forked from javigallostra/RocoLib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ticklist_handler.py
94 lines (85 loc) · 3.33 KB
/
ticklist_handler.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
from utils.utils import load_data
from config import BOULDER_COLOR_MAP, FEET_MAPPINGS
from db import mongodb_controller
from utils.utils import make_boulder_data_valid_js
from models import TickListProblem
from werkzeug.utils import secure_filename
from pymongo.database import Database
from werkzeug.wrappers.request import Request
from werkzeug.local import LocalProxy
def get_wall_radius(wall_path: str, database: Database) -> float:
"""
Wall path is expected to be: 'gym/wall'
"""
return mongodb_controller.get_walls_radius_all(database)[wall_path]
def delete_problem_from_ticklist(request: Request, current_user: LocalProxy, database: Database):
"""
Delete a problem from a user's ticklist
"""
# needed values: gym, id, section, is_done
boulder_data = make_boulder_data_valid_js(request.form.get('boulder_data'))
boulder = {
'gym': boulder_data.get('gym'),
'iden':
mongodb_controller.get_boulder_by_name(
boulder_data.get('gym'),
request.form.get('name'),
database
).get('_id', ''),
'is_done': boulder_data.get('is_done'),
'section': boulder_data.get('section')
}
# update user's ticklist
return [
TickListProblem(p) for p in mongodb_controller.delete_boulder_in_ticklist(boulder, current_user.id, database)
]
def load_user_ticklist(current_user, database: Database):
"""
Load a user's ticklist
"""
# get boulders in ticklist and extra required values
boulder_list = [
mongodb_controller.get_ticklist_boulder(problem, database) for problem in current_user.ticklist
]
unique_sections = dict()
walls_list = []
for boulder in boulder_list:
boulder['feet'] = FEET_MAPPINGS[boulder['feet']]
boulder['safe_name'] = secure_filename(boulder['name'])
boulder['radius'] = get_wall_radius(
boulder['gym'] + '/' + boulder['section'], database)
boulder['color'] = BOULDER_COLOR_MAP[boulder['difficulty']]
if boulder['gym'] not in unique_sections.keys() and boulder['section'] not in unique_sections.values():
unique_sections[boulder['gym']] = boulder['section']
walls_list.append({
'gym_name': mongodb_controller.get_gym_pretty_name(boulder['gym'], database),
'image': boulder['section'],
'name': mongodb_controller.get_wall_name(boulder['gym'], boulder['section'], database)
})
return boulder_list, walls_list
def add_boulder_to_ticklist(request, current_user, database: Database, mark_as_done_clicked=False) -> list[TickListProblem]:
"""
Add a boulder to a user's ticklist
"""
# needed values: gym, id, section, is_done
data, _ = load_data(request)
boulder = {
'gym': data.get('gym'),
'iden':
mongodb_controller.get_boulder_by_name(
data.get('gym'),
data.get('name'),
database
).get('_id', ''),
'is_done': True if data.get('is_done', '') else False,
'section': data.get('section')
}
# update user's ticklist
return [
TickListProblem(p) for p in mongodb_controller.put_boulder_in_ticklist(
boulder,
current_user.id,
database,
mark_as_done_clicked
)
]