forked from itucsdb1515/itucsdb1515
-
Notifications
You must be signed in to change notification settings - Fork 1
/
hypeblock.py
84 lines (77 loc) · 3.27 KB
/
hypeblock.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
import psycopg2 as dbapi2
import datetime
class Hypeblock:
def __init__(self, app):
self.app = app
def initialize_table(self):
with dbapi2.connect(self.app.config['dsn']) as connection:
try:
cursor = connection.cursor()
cursor.execute("""CREATE TABLE IF NOT EXISTS HYPEBLOCKS (
ID SERIAL PRIMARY KEY NOT NULL,
USER_ID INT NOT NULL REFERENCES USERS (USER_ID) ON DELETE CASCADE,
HYPE_ID INT NOT NULL REFERENCES HYPES (HYPE_ID) ON DELETE CASCADE,
DATE DATE NOT NULL,
REASON VARCHAR(50),
PERSONAL BOOLEAN NOT NULL,
UNIQUE(HYPE_ID,USER_ID)
)""")
connection.commit()
except dbapi2.DatabaseError:
connection.rollback()
finally:
connection.commit()
def drop_table(self):
with dbapi2.connect(self.app.config['dsn']) as connection:
try:
cursor = connection.cursor()
cursor.execute("""DROP TABLE IF EXISTS HYPEBLOCKS""")
connection.commit()
except dbapi2.DatabaseError:
connection.rollback()
finally:
connection.commit()
def List_BlockedHypes(self):
with dbapi2.connect(self.app.config['dsn']) as connection:
cursor = connection.cursor()
query = """ SELECT * FROM HYPEBLOCKS"""
cursor.execute(query)
blocks = cursor.fetchall()
return blocks
def Add_BlockedHypes(self, user_id, hype_id, reason, personal):
with dbapi2.connect(self.app.config['dsn']) as connection:
try:
date = datetime.date.today()
cursor = connection.cursor()
query = """INSERT INTO HYPEBLOCKS(USER_ID, HYPE_ID, DATE, REASON, PERSONAL) VALUES (%s, %s, %s, %s, %s)"""
cursor.execute(query, (user_id, hype_id, date, reason, personal))
connection.commit()
cursor.close()
except dbapi2.DatabaseError:
connection.rollback()
finally:
connection.commit()
def Delete_BlockedHypes(self, id):
with dbapi2.connect(self.app.config['dsn']) as connection:
try:
cursor = connection.cursor()
query = """DELETE FROM HYPEBLOCKS WHERE (ID = %s)"""
cursor.execute(query, (id,))
connection.commit()
cursor.close()
except dbapi2.DatabaseError:
connection.rollback()
finally:
connection.commit()
def Update_BlockedHypes(self, id, hype_id, reason, personal):
with dbapi2.connect(self.app.config['dsn']) as connection:
try:
cursor = connection.cursor()
query = """UPDATE HYPEBLOCKS SET REASON = %s, HYPE_ID = %s, PERSONAL = %s WHERE ID = %s"""
cursor.execute(query, (reason, hype_id, personal, id))
connection.commit()
cursor.close()
except dbapi2.DatabaseError:
connection.rollback()
finally:
connection.commit()