-
Notifications
You must be signed in to change notification settings - Fork 1
/
user.py
142 lines (125 loc) · 4.62 KB
/
user.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
import _sqlite3 as db
class User():
def __init__(self):
self.login = None
self.password = None
self.user_id = None
'''
Инициализируем новую базу данных для хранения юзеров
'''
self.conn = db.connect('transaction.db')
self.cursor = self.conn.cursor()
sql_query1 = '''
create table if not exists users(
user_id integer primary key UNIQUE ,
login string UNIQUE,
password string,
name string
)
'''
self.cursor.execute(sql_query1)
# сохраняем изменения
self.conn.commit()
def delete_transaction_users(self, user_id):
self.conn = db.connect('transaction.db')
self.cursor = self.conn.cursor()
sql_query = '''
delete from users where wallet_id = ?
'''
self.cursor.execute(sql_query)
self.conn.commit()
def delete_all_database_users(self):
# удаляет всю базу данных
self.conn = db.connect('transaction.db')
self.cursor = self.conn.cursor()
sql_query = '''
delete from users
'''
self.cursor.execute(sql_query)
self.conn.commit()
def get_id(self, login, password):
self.conn = db.connect('transaction.db')
self.cursor = self.conn.cursor()
sql_query1 = '''
select user_id from users where login = '{}' and password = '{}'
'''.format(login, password)
self.cursor.execute(sql_query1)
id = self.cursor.fetchone()[0]
return id
def init_user(self,login,password,name):
'''
Метод для регистрации пользователя
:param login:
:param password:
:param name:
:return:
'''
data1 = (login,password,name)
self.conn = db.connect('transaction.db')
self.cursor = self.conn.cursor()
sql_query3 = '''
select * from users where login = '{}'
'''.format(login)
self.cursor.execute(sql_query3)
res = self.cursor.fetchone()
if res == None:
sql_query1 = 'INSERT INTO users(login,password,name) VALUES (?, ?, ?)'
self.cursor.execute(sql_query1, data1)
sql_query2 = '''
select * from users where login = '{}' and password = '{}'
'''.format(login, password)
self.cursor.execute(sql_query2)
res = self.cursor.fetchone()
self.user_id = res[0]
self.login = res[1]
self.password = res[2]
self.conn.commit()
return True
else:
return False
def init_wallet(self, value, name_wallet):
'''
Метод для регистрации кошелька пользователя
:param value:
:param name_wallet:
:return:
'''
self.conn = db.connect('transaction.db')
self.cursor = self.conn.cursor()
# id = self.get_id(self.login,self.password)
data = (self.user_id, value, name_wallet)
sql_query = 'INSERT INTO wallets(user_id,value,name_wallet) VALUES (?,?,?)'
# сохраняем изменения
self.cursor.execute(sql_query, data)
self.conn.commit()
def sign_in(self,login,password):
'''
Метод, который реалтзует вход пользователя
:param login:
:param password:
:return:
'''
self.conn = db.connect('transaction.db')
self.cursor = self.conn.cursor()
sql_query1 = '''
select * from users where login = '{}' and password = '{}'
'''.format(login, password)
self.cursor.execute(sql_query1)
res = self.cursor.fetchone()
if res:
self.user_id = res[0]
self.login = res[1]
self.password = res[2]
self.conn.commit()
return True
else:
#print('Wrong login or passsword')
return False
def exit(self):
'''
Метод для выхода пользователя
:return:
'''
self.login = None
self.password = None
#u1 = User()