-
Notifications
You must be signed in to change notification settings - Fork 3
/
util.py
75 lines (60 loc) · 2.48 KB
/
util.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
"""Utility functions that are used throughout the codebase."""
from functools import wraps
from src.flaskapp.db import coll
def format_string(input_):
"""Formats the input depending on if it is a string or a list of strings.
Determines the type of the input. If it is a string then strips leading spaces and lowercases
all letters. If it is a list, then the aforementioned process is applied to each of the list elements.
Arg:
s: string or list of strings that needs to be formatted
Return:
a formatted string is returned if a string is provided
a formatted list is returned if a list is provided
"""
if isinstance(input_, str):
return input_.strip().lower()
if isinstance(input_, list):
res = []
for element in input_:
if isinstance(element, str):
res.append(element.strip().lower())
else:
# NOTE when the element is not of string typ (we can handle this case different if necessary)
res.append(element)
return res
# NOTE Simply returns the input if the input is not a string or a list
return input_
# TestCases:
# print(format_string(" JaS on "))
# print(format_string([" JaS on C ", "CHE"]))
# print(format_string([" JaS on C ", "CHE", 6]))
# print(format_string(6))
# print(format_string({"TeamRU": 2020}))
def aggregate_team_meta(members):
team_members = coll("users").find({"_id": {"$in": members}})
skills, prizes, interests = set(), set(), set()
seriousness = 0
for member in team_members:
skills.update(member["skills"])
prizes.update(member["prizes"])
interests.update(member["interests"])
seriousness += member["seriousness"]
seriousness /= len(members)
return {
"skills": list(skills),
"prizes": list(prizes),
"interests": list(interests),
"seriousness": seriousness,
}
def cors(f):
"""Wrapper function that adds CORS headers to the return value of the function that is being wrapped"""
@wraps(f)
def wrapper(*args, **kwargs):
resp, status_code = f(*args, **kwargs)
headers = {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Headers": "Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token",
"Access-Control-Allow-Credentials": True,
}
return resp, status_code, headers
return wrapper