-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdao.py
305 lines (242 loc) · 8.91 KB
/
dao.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
import datetime
import psycopg2
import os
from dotenv import load_dotenv, find_dotenv
from datetime import date
from utils import topics2tag_id, list_to_pg_array_text, list_to_pg_array_int
load_dotenv(find_dotenv())
DATABASE_NAME = os.environ.get('DATABASE_NAME')
DATABASE_USER = os.environ.get('DATABASE_USER')
DATABASE_PASSWORD = os.environ.get('DATABASE_PASSWORD')
DATABASE_HOST = os.environ.get('DATABASE_HOST')
DATABASE_PORT = os.environ.get('DATABASE_PORT')
def get_db_connection():
return psycopg2.connect(
database=DATABASE_NAME,
user=DATABASE_USER,
password=DATABASE_PASSWORD,
host=DATABASE_HOST,
port=DATABASE_PORT
)
def get_communities():
conn = get_db_connection()
cur = conn.cursor()
cur.execute("SELECT comm_name, comm_id, comm_img, comm_istelegram FROM community")
rows = cur.fetchall()
conn.close()
return rows
all_groups = get_communities()
def get_events_from_date_interval(from_date, to_date):
conn = get_db_connection()
cur = conn.cursor()
cur.execute(f"""
SELECT
post.post_url,
post.event_title,
post.event_date,
post.event_place,
post.event_short_desc,
post.event_picture_url,
community.comm_name,
post.event_duplicates
FROM post
JOIN community ON post.comm_id = community.comm_id
WHERE post.event_date >= '{str(from_date)}' and post.event_date <= '{str(to_date)}'
ORDER BY post.event_date
""")
rows = cur.fetchall()
conn.close()
return rows
def get_actual_events():
conn = get_db_connection()
cur = conn.cursor()
cur.execute(f"""
SELECT
post.post_url,
post.event_title,
post.event_date,
post.event_place,
post.event_short_desc,
post.event_picture_url,
community.comm_name,
post.event_duplicates
FROM post
JOIN community ON post.comm_id = community.comm_id
WHERE post.event_date >= '{str(datetime.datetime.now())}'
OR post.event_date = '{str(datetime.datetime(date.today().year, date.today().month, date.today().day))}'
ORDER BY post.event_date
""")
rows = cur.fetchall()
conn.close()
return rows
def get_actual_events_by_topic(topic_name: str):
conn = get_db_connection()
tag_id = topics2tag_id[topic_name]
cur = conn.cursor()
cur.execute(f"""
SELECT
post.post_url,
post.event_title,
post.event_date,
post.event_place,
post.event_short_desc,
post.event_picture_url,
community.comm_name,
post.event_duplicates
FROM tags
JOIN post ON tags.comm_id = post.comm_id AND tags.post_id = post.post_id
JOIN community ON post.comm_id = community.comm_id
WHERE (post.event_date >= '{str(datetime.datetime.now())}'
OR post.event_date = '{str(datetime.datetime(date.today().year, date.today().month, date.today().day))}')
AND tags.tag_id = {tag_id}
ORDER BY post.event_date
""")
rows = cur.fetchall()
conn.close()
return rows
def get_actual_events_by_topic_list(topics_list: list):
conn = get_db_connection()
cur = conn.cursor()
cur.execute(f"""
SELECT
post.post_url,
post.event_title,
post.event_date,
post.event_place,
post.event_short_desc,
post.event_picture_url,
community.comm_name,
post.event_duplicates
FROM tags
JOIN post ON tags.comm_id = post.comm_id AND tags.post_id = post.post_id
JOIN community ON post.comm_id = community.comm_id
WHERE (post.event_date >= '{str(datetime.datetime.now())}'
OR post.event_date = '{str(datetime.datetime(date.today().year, date.today().month, date.today().day))}')
AND tags.tag_id = ANY (ARRAY [{list_to_pg_array_int(topics_list)}])
ORDER BY post.event_date
""")
rows = cur.fetchall()
conn.close()
return rows
def set_suggested_event_source(user_id, username, url):
conn = get_db_connection()
cur = conn.cursor()
cur.execute(f"INSERT INTO suggested_event_sources (user_id, username, url) "
f"VALUES ({user_id}, '{username}', '{url}')")
conn.commit()
conn.close()
def set_suggested_functionality(user_id, username, suggestion):
conn = get_db_connection()
cur = conn.cursor()
cur.execute(f"INSERT INTO suggested_functionality (user_id, username, suggestion) "
f"VALUES ({user_id}, '{username}', '{suggestion}')")
conn.commit()
conn.close()
def check_new_user(user_id, username):
is_new = False
conn = get_db_connection()
cur = conn.cursor()
cur.execute(f"SELECT user_id FROM users WHERE user_id = {user_id}")
rows = cur.fetchall()
if len(rows) == 0:
cur.execute(f"INSERT INTO users (user_id, username) "
f"VALUES ({user_id}, '{username}')")
is_new = True
conn.commit()
conn.close()
return is_new
def get_users_count():
conn = get_db_connection()
cur = conn.cursor()
cur.execute("SELECT count(*) user_id FROM users")
rows = cur.fetchall()
count = rows[0][0]
conn.close()
return count
def get_user_id_list():
conn = get_db_connection()
cur = conn.cursor()
cur.execute("SELECT user_id FROM users")
rows = cur.fetchall()
conn.close()
return rows
def get_notifications_user_id_list():
conn = get_db_connection()
cur = conn.cursor()
cur.execute("SELECT user_id FROM users_notification")
rows = cur.fetchall()
conn.close()
return rows
def log_action(action, user_id, username):
conn = get_db_connection()
cur = conn.cursor()
cur.execute(f"INSERT INTO users_log (timestamp, action, user_id, username) "
f"VALUES ('{datetime.datetime.now()}', '{action}', {user_id}, '{username}')")
conn.commit()
conn.close()
def get_user_selected_comm(user_id):
conn = get_db_connection()
cur = conn.cursor()
cur.execute(f"SELECT selected_comm FROM users WHERE user_id = {user_id}")
rows = cur.fetchall()
conn.close()
return rows[0][0]
def set_user_selected_comm(user_id, comm):
conn = get_db_connection()
cur = conn.cursor()
cur.execute(f"UPDATE users SET selected_comm = ARRAY [{list_to_pg_array_text(comm)}] WHERE user_id = {user_id}")
conn.commit()
conn.close()
def check_new_users_notif_row(user_id):
conn = get_db_connection()
cur = conn.cursor()
cur.execute(f"SELECT user_id FROM users_notification WHERE user_id = {user_id}")
rows = cur.fetchall()
if len(rows) == 0:
cur.execute(f"INSERT INTO users_notification (user_id)"
f"VALUES ({user_id})")
conn.commit()
conn.close()
def set_push_interval(user_id, interval):
check_new_users_notif_row(user_id)
conn = get_db_connection()
cur = conn.cursor()
cur.execute(f"UPDATE users_notification SET push_interval = {interval} WHERE user_id = {user_id}")
conn.commit()
conn.close()
set_next_push_date(user_id, datetime.datetime.now() + datetime.timedelta(interval))
def get_push_interval(user_id):
conn = get_db_connection()
cur = conn.cursor()
cur.execute(f"SELECT push_interval FROM users_notification WHERE user_id = {user_id}")
rows = cur.fetchall()
conn.close()
return rows[0][0]
def get_user_push_tags(user_id):
check_new_users_notif_row(user_id)
conn = get_db_connection()
cur = conn.cursor()
cur.execute(f"SELECT push_tags FROM users_notification WHERE user_id = {user_id}")
rows = cur.fetchall()
conn.close()
return rows[0][0]
def set_user_push_tags(user_id, tags):
check_new_users_notif_row(user_id)
conn = get_db_connection()
cur = conn.cursor()
cur.execute(f"UPDATE users_notification SET push_tags = ARRAY [{list_to_pg_array_int(tags)}] WHERE user_id = {user_id}")
conn.commit()
conn.close()
def get_next_push_date(user_id):
conn = get_db_connection()
cur = conn.cursor()
cur.execute(f"SELECT next_push_date FROM users_notification WHERE user_id = {user_id}")
rows = cur.fetchall()
conn.close()
return rows[0][0]
def set_next_push_date(user_id, date):
conn = get_db_connection()
cur = conn.cursor()
cur.execute(f"UPDATE users_notification SET next_push_date = TIMESTAMP '{date}' WHERE user_id = {user_id}")
conn.commit()
conn.close()