Repository navigation
Expand file tree
/
Copy pathdbTools.py
More file actions
75 lines (59 loc) · 3.16 KB
/
Copy pathdbTools.py
File metadata and controls
75 lines (59 loc) · 3.16 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
import psycopg2
from settings import db_url
class Connection:
def __new__(cls, db_url):
try:
connection = psycopg2.connect(
database = db_url[0],
user = db_url[0],
password = db_url[1],
host = db_url[2],
port = db_url[3]
)
cursor = connection.cursor()
return connection, cursor
except psycopg2.OperationalError:
return False
class UserPosition:
def __init__(self, db_url):
if Connection(db_url):
self.connection, self.cursor = Connection(db_url)
else:
print('error')
def set_getting_position(self, user_id):
self.cursor.execute("INSERT INTO user_position (user_id, faculty, course, group_name) VALUES (%s, %s, %s, %s)", (user_id, 'empty', 'empty', 'empty'))
self.connection.commit()
def set_faculty_position(self, user_id, faculty):
self.cursor.execute("UPDATE user_position SET faculty = (%s) WHERE id IN (SELECT max(id) FROM user_position WHERE user_id = (%s)) AND course = (%s) AND group_name = (%s)", (faculty, user_id, 'empty', 'empty'))
self.connection.commit()
def set_course_position(self, user_id, course):
self.cursor.execute("UPDATE user_position SET course = (%s) WHERE id IN (SELECT max(id) FROM user_position WHERE user_id = (%s)) AND group_name = (%s)", (course, user_id, 'empty'))
self.connection.commit()
def set_group_position(self, user_id, group_name):
self.cursor.execute("UPDATE user_position SET group_name = (%s) WHERE id IN (SELECT max(id) FROM user_position WHERE user_id = (%s)) AND group_name = (%s)", (group_name, user_id, 'empty'))
self.connection.commit()
def get_faculty_and_course(self, user_id):
self.cursor.execute("SELECT * FROM user_position WHERE id IN (SELECT max(id) FROM user_position WHERE user_id = (%s))", (user_id, ))
return tuple([element for index, element in enumerate(self.cursor.fetchone()) if index == 2 or index == 3])
def verification(self, user_id):
self.cursor.execute("SELECT group_name FROM user_position WHERE id IN (SELECT max(id) FROM user_position WHERE user_id = (%s))", (user_id, ))
return self.cursor.fetchone()[0]
def cancel_getting_started(self, user_id):
self.cursor.execute("DELETE FROM user_position WHERE user_id=%s", (user_id,))
self.connection.commit()
def cancel_faculty(self, user_id):
self.cursor.execute("UPDATE user_position SET faculty = (%s) WHERE id IN (SELECT max(id) FROM user_position WHERE user_id = (%s))", ('empty', user_id))
self.connection.commit()
def cancel_course(self, user_id):
self.cursor.execute("UPDATE user_position SET course = (%s) WHERE id IN (SELECT max(id) FROM user_position WHERE user_id = (%s) AND faculty != (%s))", ('empty', user_id, 'empty'))
self.connection.commit()
def cancel_group(self, user_id):
self.cursor.execute("UPDATE user_position SET group_name = (%s) WHERE id IN (SELECT max(id) FROM user_position WHERE user_id = (%s) AND faculty != (%s))", ('empty', user_id, 'empty'))
self.connection.commit()
def back_keyboard(self, user_id):
self.cursor.execute("SELECT * FROM user_position WHERE id IN (SELECT max(id) FROM user_position WHERE user_id = (%s))", (user_id, ))
count = -1
for index, field in enumerate(self.cursor.fetchone()):
if field != 'empty':
count += 1
return count