forked from itucsdb1515/itucsdb1515
-
Notifications
You must be signed in to change notification settings - Fork 1
/
block.py
108 lines (89 loc) · 3.75 KB
/
block.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
import psycopg2 as dbapi2
import datetime
class block:
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 BLOCKED(
PERSON_ID INTEGER NOT NULL REFERENCES USERS (USER_ID) ON DELETE CASCADE,
BLOCK_ID INTEGER NOT NULL REFERENCES USERS (USER_ID) ON DELETE CASCADE,
REASON VARCHAR(50),
DATE DATE NOT NULL,
PRIMARY KEY(PERSON_ID,BLOCK_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()
query = """DROP TABLE IF EXISTS BLOCKED"""
cursor.execute(query)
except dbapi2.DatabaseError:
connection.rollback()
finally:
connection.commit()
def show_blocked(self):
with dbapi2.connect(self.app.config['dsn']) as connection:
cursor = connection.cursor()
query = """ SELECT * FROM BLOCKED """
cursor.execute(query)
sportpage = cursor.fetchall()
return sportpage
def select_block(self):
with dbapi2.connect(self.app.config['dsn']) as connection:
cursor = connection.cursor()
query = """ SELECT * FROM BLOCKED"""
cursor.execute(query)
sportpage = cursor.fetchall()
return sportpage
def add_block(self, person_id, block_id, reason):
with dbapi2.connect(self.app.config['dsn']) as connection:
try:
date = datetime.date.today()
cursor = connection.cursor()
query = """INSERT INTO BLOCKED(PERSON_ID, BLOCK_ID, REASON, DATE) VALUES (%s, %s, %s, %s)"""
cursor.execute(query, (person_id, block_id, reason, date))
connection.commit()
cursor.close()
except dbapi2.DatabaseError:
connection.rollback()
finally:
connection.commit()
def delete_block(self, person_id, block_id):
with dbapi2.connect(self.app.config['dsn']) as connection:
try:
cursor = connection.cursor()
query = """DELETE FROM BLOCKED WHERE PERSON_ID = %s AND BLOCK_ID = %s"""
cursor.execute(query, (person_id, block_id))
connection.commit()
cursor.close()
except dbapi2.DatabaseError:
connection.rollback()
finally:
connection.commit()
def update_reason(self, person_id, block_id, reason):
with dbapi2.connect(self.app.config['dsn']) as connection:
try:
cursor = connection.cursor()
query = """UPDATE BLOCKED SET REASON = %s WHERE PERSON_ID=%s AND BLOCK_ID=%s"""
cursor.execute(query, (reason, person_id, block_id))
connection.commit()
cursor.close()
except dbapi2.DatabaseError:
connection.rollback()
finally:
connection.commit()
def select_users(self):
with dbapi2.connect(self.app.config['dsn']) as connection:
cursor = connection.cursor()
query = """ SELECT * FROM BLOCKED """
cursor.execute(query)
sportuser2 = cursor.fetchall()
return sportuser2