forked from Garpusha/PYnder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db_function.py
283 lines (255 loc) · 10.8 KB
/
db_function.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
import os.path
import configparser
import models as m
import sqlalchemy as sq
from sqlalchemy.orm import sessionmaker
def get_db_config(ini_file: str = "config.ini") -> str:
"""
Функция читает из конфигурационного файла, по умолчанию config.ini
:param ini_file: .ini filename with path if necessary, by default - config.ini
:return: DSN string for sqlalchemy engine creation:
postgresql://{db_user}:{db_pwd}@{db_ip}:{db_port}/{db_name}
"""
if not os.path.exists(ini_file):
print(f"Configuration file '{ini_file}' not found.")
exit()
config = configparser.ConfigParser()
config.read(ini_file)
try:
db_ip = config.get("DataBase", "IP")
db_port = config.get("DataBase", "Port")
db_name = config.get("DataBase", "DBName")
db_user = config.get("DataBase", "User")
db_pwd = config.get("DataBase", "Password")
except configparser.Error as error_msg:
print(f"Error occurred. {error_msg}")
exit()
return f"postgresql://{db_user}:{db_pwd}@{db_ip}:{db_port}/{db_name}"
class PYnder_DB:
def __init__(self, rebuild: bool):
self.engine = sq.create_engine(get_db_config("config.ini"))
self.Session = sessionmaker(bind=self.engine)
self.session = self.Session()
if rebuild:
self.delete_structure()
self.create_structure()
def get_favorite(self, vk_owner_id_: str) -> list:
"""
Метод возвращает список избранного
:param vk_owner_id_: str Owner's VK ID
:return: list of dicts {
vk_id:str(50),
first_name:str(20),
last_name:str(20),
city(20):str,
sex:integer,
birth_date:date,
url:str,
images[{'url':'image_1.jpg', likes: 5}, {'url':'image_2.jpg', likes: 3}, {'url':'image_3.jpg', likes: 15}]
}
"""
with self.session as db:
# Reading data from tables
owner_pk_ = self.get_pk(m.Owner.id, vk_owner_id_)
if owner_pk_ == -1:
return []
my_query = db.query(
m.Owner.id,
m.VKUser.vk_id,
m.VKUser.first_name,
m.VKUser.last_name,
m.VKUser.city,
m.VKUser.sex,
m.VKUser.birth_date,
m.VKUser.url,
m.Photo.url,
m.Photo.likes,
).filter_by(id=owner_pk_)
my_query = my_query.join(m.Favorite, m.Favorite.owner_id == m.Owner.id)
my_query = my_query.join(m.VKUser, m.VKUser.id == m.Favorite.user_id)
my_query = my_query.join(m.Photo, m.Photo.user_id == m.VKUser.id)
result = my_query.all()
favorite_dict = {}
favorite_list = []
vk_ids = []
for element in result:
f_vkid = element[1]
if f_vkid not in vk_ids:
vk_ids.append(f_vkid)
favorite_dict["vk_id"] = element[1]
favorite_dict["first_name"] = element[2]
favorite_dict["last_name"] = element[3]
favorite_dict["city"] = element[4]
favorite_dict["sex"] = element[5]
favorite_dict[
"birth_date"
] = f"{element[6].day}.{element[6].month}.{element[6].year}"
favorite_dict["url"] = element[7]
favorite_dict["images"] = [{"url": element[8], "likes": element[9]}]
favorite_list.append(favorite_dict.copy())
else:
for index, item in enumerate(favorite_list):
if item["vk_id"] == f_vkid:
favorite_list[index]["images"].append(
{"url": element[8], "likes": element[9]}
)
break
return favorite_list
def get_pk(self, table_, id_: str) -> int:
"""
Метод возвращает primary key для определенного VK ID
:param table_ table object with parameter, e.g. m.VKUser.id
:param id_: VK ID:
:return: PK ID or -1 if not found
"""
with self.session as db:
my_query = db.query(table_).filter_by(vk_id=id_)
if my_query.count() == 0:
return -1
else:
return my_query.first()[0]
def is_in_favorite(self, vk_user_id_: str, vk_owner_id_: str) -> bool:
"""
Метод проверяет наличие пользователя ВК в избранном
:param vk_user_id_: User's VK ID
:param vk_owner_id_: Owner's VK ID
:return: True if in Favorites, False otherwise
"""
with self.session as db:
user_pk_ = self.get_pk(m.VKUser.id, vk_user_id_)
if user_pk_ == -1:
return False
my_query = db.query(m.Favorite.user_id).filter_by(user_id=user_pk_)
if my_query.count() == 0:
return False
owner_pk_ = self.get_pk(m.Owner.id, vk_owner_id_)
if owner_pk_ == -1:
return False
my_query = db.query(m.Favorite.owner_id).filter_by(owner_id=owner_pk_)
if my_query.count() == 0:
return False
my_query = db.query(m.Favorite.owner_id, m.Favorite.owner_id).filter_by(
user_id=user_pk_, owner_id=owner_pk_
)
if my_query.count() == 0:
return False
else:
return True
def delete_favorite(self, vk_user_id_: str, vk_owner_id_: str) -> bool:
"""
Метод удаляет запись из избранного. Если аккаунт ВК не находится в избранном у других пользователей бота,
он удаляется вместе с картинками
:param vk_user_id_: str(50) - VK User ID to be removed
:param vk_owner_id_: str(50) - Owner's VK ID
"""
# Checking is record exists, if not - exiting
with self.session as db:
user_pk_ = self.get_pk(m.VKUser.id, vk_user_id_)
if user_pk_ == -1:
return False
my_query = db.query(m.Favorite.user_id).filter_by(user_id=user_pk_)
if my_query.count() == 0:
return False
owner_pk_ = self.get_pk(m.Owner.id, vk_owner_id_)
if owner_pk_ == -1:
return False
my_query = db.query(m.Favorite.owner_id).filter_by(owner_id=owner_pk_)
if my_query.count() == 0:
return False
db.query(m.Favorite).filter_by(
user_id=user_pk_, owner_id=owner_pk_
).delete()
db.commit()
# checking are there any other owners who added same VK ID into the favorites
# if no - removing VK ID from other tables
my_query = db.query(m.Favorite.user_id).filter_by(user_id=user_pk_)
if my_query.count() == 0:
db.query(m.Photo).filter_by(user_id=user_pk_).delete()
db.commit()
db.query(m.VKUser).filter_by(id=user_pk_).delete()
db.commit()
return True
def create_structure(self):
"""
Метод создает структуру БД
:return: None\n
"""
print("Creating structure")
m.Base.metadata.create_all(self.engine)
def delete_structure(self):
"""
Метод удаляет БД - данные и структуру
:return: None\n
"""
print("Deleting structure")
m.Base.metadata.drop_all(self.engine)
def add_owner(self, owner_id_: str) -> bool:
"""
Метод добавляет пользователя в БД
:param owner_id_: str(50) - owner ID
:return: boolean - True if record was uploaded, False if not (already exists)
"""
with self.session as db:
my_query = db.query(m.Owner.vk_id).filter_by(vk_id=owner_id_)
if my_query.count() == 0:
my_record = m.Owner(vk_id=owner_id_)
db.add(my_record)
db.commit()
return True
return False
def add_favorite(self, new_record: dict, vk_owner_id_: str) -> bool:
"""
Метод добавляет запись в избранное, таблицы VKUsers и Photos
:param new_record: dictionary{
vk_id:str(50),
first_name:str(20),
last_name:str(20),
city(20):str,
sex:integer,
birth_date:date,
url:str,
images[{'url':'image_1.jpg', likes: 5}, {'url':'image_2.jpg', likes: 3}, {'url':'image_3.jpg', likes: 15}]
}
:param vk_owner_id_: str(50) - Owner's VK ID
"""
with self.session as db:
my_query = db.query(m.VKUser.vk_id).filter_by(vk_id=new_record["vk_id"])
is_user_exists = my_query.count() != 0
if not is_user_exists:
# adding IDs into the table VKUsers if not exists
my_record = m.VKUser(
vk_id=new_record["vk_id"],
first_name=new_record["first_name"],
last_name=new_record["last_name"],
city=new_record["city"],
sex=new_record["sex"],
birth_date=new_record["birth_date"],
url=new_record["url"],
)
db.add(my_record)
db.commit()
# Getting primary keys for the Favorite table
user_pk_ = self.get_pk(m.VKUser.id, new_record["vk_id"])
if user_pk_ == -1:
return False
owner_pk_ = self.get_pk(m.Owner.id, vk_owner_id_)
if owner_pk_ == -1:
return False
if not is_user_exists:
# Adding photos into the table Photo if user not exists
for image in new_record["images"]:
my_record = m.Photo(
user_id=user_pk_, url=image["url"], likes=image["likes"]
)
db.add(my_record)
db.commit()
# Adding IDs into the table Favorite if not exists
my_query = db.query(m.Favorite).filter_by(
user_id=user_pk_, owner_id=owner_pk_
)
if my_query.count() == 0:
my_record = m.Favorite(user_id=user_pk_, owner_id=owner_pk_)
db.add(my_record)
db.commit()
return True
return False