This repository was archived by the owner on Dec 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_manager.py
More file actions
78 lines (62 loc) · 2.05 KB
/
data_manager.py
File metadata and controls
78 lines (62 loc) · 2.05 KB
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
from psycopg2 import sql
import connection
import bcrypt
def hash_password(plain_text_password):
hashed_bytes = bcrypt.hashpw(plain_text_password.encode('utf-8'), bcrypt.gensalt())
return hashed_bytes.decode('utf-8')
def verify_password(plain_text_password, hashed_password):
hashed_bytes_password = hashed_password.encode('utf-8')
return bcrypt.checkpw(plain_text_password.encode('utf-8'), hashed_bytes_password)
@connection.connection_handler
def get_users(cursor):
query = """
SELECT * FROM api_users"""
cursor.execute(query)
return cursor.fetchall()
@connection.connection_handler
def get_user_id(cursor, username):
query = """
SELECT id FROM api_users
WHERE username = %(username)s
"""
cursor.execute(query, {'username': username})
return cursor.fetchone()
@connection.connection_handler
def get_password(cursor, username):
query = """
SELECT password FROM api_users
WHERE username = %(username)s
"""
cursor.execute(query, {'username': username})
return cursor.fetchone()
@connection.connection_handler
def add_user(cursor, username, password):
query = """
INSERT INTO api_users (username, password)
VALUES (
%(username)s,
%(password)s)
"""
cursor.execute(query, {'username': username, 'password': password})
@connection.connection_handler
def save_vote(cursor, planet_id, planet_name, user_id, time):
query = sql.SQL("""
INSERT INTO planet_votes (planet_id, planet_name, user_id, submission_time)
VALUES ({planet_id},
{planet_name},
{user_id},
{time})
RETURNING id
""").format(planet_id=sql.Literal(planet_id), planet_name=sql.Literal(planet_name),
user_id=sql.Literal(user_id), time=sql.Literal(time))
cursor.execute(query)
return cursor.fetchall()
@connection.connection_handler
def get_all_votes(cursor):
query = """
SELECT planet_name, COUNT(planet_name) AS count FROM planet_votes
GROUP BY planet_name
ORDER BY count DESC
"""
cursor.execute(query)
return cursor.fetchall()