-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfhelpers.py
159 lines (136 loc) · 4.24 KB
/
fhelpers.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
import re
import sqlite3
from functools import wraps
from flask import redirect, session
# |----- SQL DATABASE HELPING FUNCTIONS ----|
def cursor_execute(query, *args):
'''
Executes a SQL query even if it has a variable number of arguments
'''
conn = sqlite3.connect('swappr.db')
cursor = conn.cursor()
cursor.execute(query, args)
conn.commit()
conn.close()
def cursor_fetch(query, *args):
'''
Fetches data from the database in two tuples, where the
first one has the columns and the second has the values.
Then it converts them into a json style data structure
with key/value dict logic.
'''
conn = sqlite3.connect('swappr.db')
cursor = conn.cursor()
cursor.execute(query, args)
fetched_data_tuples = cursor.fetchall()
fetched_data_json = []
for fetched_data_tuple in fetched_data_tuples:
fetched_data_json.append(
tuples_to_dict(
cursor.description,
fetched_data_tuple
)
)
conn.close()
return fetched_data_json
def tuples_to_dict(keys_tuple, values_tuple):
'''
Converts two tuples into one dictionary
'''
if values_tuple:
values = values_tuple
keys = [key[0] for key in keys_tuple]
return dict(zip(keys, values))
else:
return None
# |----- SIGNUP/ACCOUNT HELPING FUNCTIONS ----|
def strong_password(password):
'''
Checks if a password is strong enough. To return `True` the password
should contain at least one upper case letter, one lower case letter,
one decimal number, and one punctuation character, while its length
must be at least of eight characters - else returns `False`.
'''
password_regex_stipulations = re.compile(
r'^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[!@#$%^&*()_+])[\w!@#$%^&*()_+]{8,}$'
)
return bool(password_regex_stipulations.match(password))
def email_exists(email):
'''
Fetches user input email from the database to validate
its existence. In case email is not found, return None.
'''
conn = sqlite3.connect('swappr.db')
cursor = conn.cursor()
cursor.execute(
'SELECT * FROM users WHERE email = ?',
(email,)
)
result = cursor.fetchone()
conn.close()
return result is not None
def username_exists(username, *args):
'''
Fetches user input username from the database to validate
its existence. If username is not found, return None.
In case of update_username the function gets user_id in *args
so that it can check existent usernames excluding the one
of the connected user.
'''
conn = sqlite3.connect('swappr.db')
cursor = conn.cursor()
if not args:
cursor.execute(
'SELECT * FROM users WHERE username = ?',
(username,)
)
else:
cursor.execute(
'SELECT * FROM users WHERE username = ? AND id != ?',
(
username,
*args,
)
)
result = cursor.fetchone()
conn.close()
return result is not None
# |----- SUBMIT/EDIT-SUBMISSION HELPING FUNCTIONS ----|
def get_list_of_values(json_data, column_name):
'''
Converts a json file into a list with values of
the selected column
'''
return [item[column_name] for item in json_data]
def check_submitted_location(submitted_value, valid_values, error_message):
'''
Checks if a user's submitted value is actually
stored in the database
'''
if submitted_value not in valid_values:
raise ValueError(error_message)
else:
return True
# |----- ROUTING HELPING FUNCTIONS ----|
def login_required(f):
"""
Decorate routes to require login
"""
@wraps(f)
def decorated_function(*args, **kwargs):
if session.get("user_id") is None:
return redirect("/signin")
return f(*args, **kwargs)
return decorated_function
def comma(integer):
'''
Formats an integer by placing commas between thousands
'''
return f"{integer:,}"
def whitespace(text):
'''
Formats a snakecase string into a readable title by replacing
underscores with whitespaces and initial lowercase letters with
capital ones
'''
return text.replace('_', ' ').title()