dbworker.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. import psycopg2
  2. from config import dbname, user, password, host
  3. connection = psycopg2.connect(dbname=dbname, user=user, password=password, host=host)
  4. cursor = connection.cursor()
  5. # get all users
  6. def get_all_users():
  7. with connection:
  8. cursor.execute(
  9. """
  10. SELECT user_id
  11. FROM users
  12. """
  13. )
  14. try:
  15. return [i[0] for i in cursor.fetchall()]
  16. except Exception as e:
  17. print(e)
  18. return None
  19. # state
  20. def get_current_state(user_id):
  21. with connection:
  22. cursor.execute(
  23. """
  24. SELECT state
  25. FROM users
  26. WHERE user_id = (%s)
  27. """,
  28. (str(user_id),),
  29. )
  30. try:
  31. return cursor.fetchone()[0]
  32. except Exception as e:
  33. print(e)
  34. return None
  35. def set_state(state, user_id):
  36. with connection:
  37. cursor.execute(
  38. """
  39. UPDATE users
  40. SET state = (%s)
  41. WHERE user_id = (%s)
  42. """,
  43. (state, str(user_id)),
  44. )
  45. connection.commit()
  46. # user_id
  47. def get_and_set_id(user_id):
  48. with connection:
  49. cursor.execute("SELECT 1 FROM users WHERE user_id = (%s)", (str(user_id),))
  50. a = cursor.fetchone()
  51. if a is None:
  52. cursor.execute(
  53. """
  54. INSERT INTO users (user_id, klas, subject, author, type, maintopic, subtopic, subsubtopic, exercise, state)
  55. VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
  56. """,
  57. (
  58. str(user_id),
  59. 1,
  60. "None",
  61. "None",
  62. "None",
  63. "None",
  64. "None",
  65. "None",
  66. "None",
  67. 0,
  68. ),
  69. )
  70. connection.commit()
  71. # klas
  72. def get_klas(user_id):
  73. with connection:
  74. cursor.execute(
  75. """
  76. SELECT klas
  77. FROM users
  78. WHERE user_id = (%s)
  79. """,
  80. (str(user_id),),
  81. )
  82. try:
  83. return cursor.fetchone()[0]
  84. except Exception as e:
  85. print(e)
  86. return None
  87. def set_klas(klas, user_id):
  88. with connection:
  89. cursor.execute(
  90. """
  91. UPDATE users
  92. SET klas = (%s)
  93. WHERE user_id = (%s)
  94. """,
  95. (klas, str(user_id)),
  96. )
  97. connection.commit()
  98. # subject
  99. def get_subjects(klas):
  100. with connection:
  101. cursor.execute(
  102. """
  103. SELECT subject, max(id) from gdz
  104. WHERE klas = (%s)
  105. GROUP BY subject
  106. ORDER BY max(id)
  107. """,
  108. (klas,),
  109. )
  110. try:
  111. return cursor.fetchall()
  112. except Exception as e:
  113. print(e)
  114. return None
  115. def set_subject(subject, user_id):
  116. with connection:
  117. cursor.execute(
  118. """
  119. UPDATE users
  120. SET subject = (%s)
  121. WHERE user_id = (%s)
  122. """,
  123. (subject, str(user_id)),
  124. )
  125. connection.commit()
  126. def get_subject(user_id):
  127. with connection:
  128. cursor.execute(
  129. """
  130. SELECT subject
  131. FROM users
  132. WHERE user_id = (%s)
  133. """,
  134. (str(user_id),),
  135. )
  136. try:
  137. return cursor.fetchone()[0]
  138. except Exception as e:
  139. print(e)
  140. return None
  141. # author
  142. def get_authors(klas, subject):
  143. with connection:
  144. cursor.execute(
  145. """
  146. SELECT author, max(id) from gdz
  147. WHERE klas = (%s) AND subject = (%s)
  148. GROUP BY author
  149. ORDER BY max(id)
  150. """,
  151. (klas, subject),
  152. )
  153. try:
  154. return cursor.fetchall()
  155. except Exception as e:
  156. print(e)
  157. return None
  158. def get_author(user_id):
  159. with connection:
  160. cursor.execute(
  161. """
  162. SELECT author
  163. FROM users
  164. WHERE user_id = (%s)
  165. """,
  166. (str(user_id),),
  167. )
  168. try:
  169. return cursor.fetchone()[0]
  170. except Exception as e:
  171. print(e)
  172. return None
  173. def set_author(author, user_id):
  174. with connection:
  175. cursor.execute(
  176. """
  177. UPDATE users
  178. SET author = (%s)
  179. WHERE user_id = (%s)
  180. """,
  181. (author, str(user_id)),
  182. )
  183. connection.commit()
  184. # types
  185. def get_types(klas, subject, author):
  186. with connection:
  187. cursor.execute(
  188. """
  189. SELECT type, max(id) from gdz
  190. WHERE klas = (%s) AND subject = (%s) AND author = (%s)
  191. GROUP BY type
  192. ORDER BY max(id)
  193. """,
  194. (klas, subject, author),
  195. )
  196. try:
  197. return cursor.fetchall()
  198. except Exception as e:
  199. print(e)
  200. return None
  201. def get_type(user_id):
  202. with connection:
  203. cursor.execute(
  204. """
  205. SELECT type
  206. FROM users
  207. WHERE user_id = (%s)
  208. """,
  209. (str(user_id),),
  210. )
  211. try:
  212. return cursor.fetchone()[0]
  213. except Exception as e:
  214. print(e)
  215. return None
  216. def set_type(type, user_id):
  217. with connection:
  218. cursor.execute(
  219. """
  220. UPDATE users
  221. SET type = (%s)
  222. WHERE user_id = (%s)
  223. """,
  224. (type, str(user_id)),
  225. )
  226. connection.commit()
  227. # maintopic
  228. def get_maintopics(klas, subject, author, type):
  229. with connection:
  230. cursor.execute(
  231. """
  232. SELECT maintopic, max(id) from gdz
  233. WHERE klas = (%s) AND subject = (%s) AND author = (%s) AND type = (%s)
  234. GROUP BY maintopic
  235. ORDER BY max(id)
  236. """,
  237. (klas, subject, author, type),
  238. )
  239. try:
  240. return cursor.fetchall()
  241. except Exception as e:
  242. print(e)
  243. return None
  244. def get_maintopic(user_id):
  245. with connection:
  246. cursor.execute(
  247. """
  248. SELECT maintopic
  249. FROM users
  250. WHERE user_id = (%s)
  251. """,
  252. (str(user_id),),
  253. )
  254. try:
  255. return cursor.fetchone()[0]
  256. except Exception as e:
  257. print(e)
  258. return None
  259. def set_maintopic(maintopic, user_id):
  260. with connection:
  261. cursor.execute(
  262. """
  263. UPDATE users
  264. SET maintopic = (%s)
  265. WHERE user_id = (%s)
  266. """,
  267. (maintopic, str(user_id)),
  268. )
  269. connection.commit()
  270. # subtopic
  271. def get_subtopics(klas, subject, author, type, maintopic):
  272. with connection:
  273. cursor.execute(
  274. """
  275. SELECT subtopic, max(id) from gdz
  276. WHERE klas = (%s) AND subject = (%s) AND author = (%s) AND type = (%s) AND maintopic = (%s)
  277. GROUP BY subtopic
  278. ORDER BY max(id)
  279. """,
  280. (klas, subject, author, type, maintopic),
  281. )
  282. try:
  283. return cursor.fetchall()
  284. except Exception as e:
  285. print(e)
  286. return None
  287. def get_subtopic(user_id):
  288. with connection:
  289. cursor.execute(
  290. """
  291. SELECT subtopic
  292. FROM users
  293. WHERE user_id = (%s)
  294. """,
  295. (str(user_id),),
  296. )
  297. try:
  298. return cursor.fetchone()[0]
  299. except Exception as e:
  300. print(e)
  301. return None
  302. def set_subtopic(subtopic, user_id):
  303. with connection:
  304. cursor.execute(
  305. """
  306. UPDATE users
  307. SET subtopic = (%s)
  308. WHERE user_id = (%s)
  309. """,
  310. (subtopic, str(user_id)),
  311. )
  312. connection.commit()
  313. # subsubtopic
  314. def get_subsubtopics(klas, subject, author, type, maintopic, subtopic):
  315. with connection:
  316. cursor.execute(
  317. """
  318. SELECT subsubtopic, max(id) from gdz
  319. WHERE klas = (%s) AND subject = (%s) AND author = (%s) AND type = (%s) AND maintopic = (%s) AND subtopic = (%s)
  320. GROUP BY subsubtopic
  321. ORDER BY max(id)
  322. """,
  323. (klas, subject, author, type, maintopic, subtopic),
  324. )
  325. try:
  326. return cursor.fetchall()
  327. except Exception as e:
  328. print(e)
  329. return None
  330. def get_subsubtopic(user_id):
  331. with connection:
  332. cursor.execute(
  333. """
  334. SELECT subsubtopic
  335. FROM users
  336. WHERE user_id = (%s)
  337. """,
  338. (str(user_id),),
  339. )
  340. try:
  341. return cursor.fetchone()[0]
  342. except Exception as e:
  343. print(e)
  344. return None
  345. def set_subsubtopic(subsubtopic, user_id):
  346. with connection:
  347. cursor.execute(
  348. """
  349. UPDATE users
  350. SET subsubtopic = (%s)
  351. WHERE user_id = (%s)
  352. """,
  353. (subsubtopic, str(user_id)),
  354. )
  355. connection.commit()
  356. # exercise
  357. def get_exercises(klas, subject, author, type, maintopic, subtopic, subsubtopic):
  358. with connection:
  359. cursor.execute(
  360. """
  361. SELECT exercise, max(id)
  362. FROM gdz
  363. WHERE klas = (%s) AND subject = (%s) AND author = (%s) AND type = (%s) AND maintopic = (%s) AND subtopic = (%s) AND subsubtopic = (%s)
  364. GROUP BY exercise
  365. ORDER BY max(id)
  366. """,
  367. (klas, subject, author, type, maintopic, subtopic, subsubtopic),
  368. )
  369. try:
  370. return cursor.fetchall()
  371. except Exception as e:
  372. print(e)
  373. return None
  374. def get_exercise(user_id):
  375. with connection:
  376. cursor.execute(
  377. """
  378. SELECT exercise
  379. FROM users
  380. WHERE user_id = (%s)
  381. """,
  382. (str(user_id),),
  383. )
  384. try:
  385. return cursor.fetchone()[0]
  386. except Exception as e:
  387. print(e)
  388. return None
  389. def set_exercise(exercise, user_id):
  390. with connection:
  391. cursor.execute(
  392. """
  393. UPDATE users
  394. SET exercise = (%s)
  395. WHERE user_id = (%s)
  396. """,
  397. (exercise, str(user_id)),
  398. )
  399. connection.commit()
  400. # solution
  401. def get_solution(
  402. klas, subject, author, type, maintopic, subtopic, subsubtopic, exercise
  403. ):
  404. with connection:
  405. cursor.execute(
  406. """
  407. SELECT solution_id
  408. FROM gdz
  409. WHERE klas = (%s) AND subject = (%s) AND author = (%s) AND type = (%s) AND maintopic = (%s) AND subtopic = (%s) AND subsubtopic = (%s) AND exercise = (%s)
  410. """,
  411. (klas, subject, author, type, maintopic, subtopic, subsubtopic, exercise),
  412. )
  413. sol_id = cursor.fetchone()[0]
  414. if not sol_id:
  415. cursor.execute(
  416. """
  417. SELECT exercise_url
  418. FROM gdz
  419. WHERE klas = (%s) AND subject = (%s) AND author = (%s) AND type = (%s) AND maintopic = (%s) AND subtopic = (%s) AND subsubtopic = (%s) AND exercise = (%s)
  420. """,
  421. (
  422. klas,
  423. subject,
  424. author,
  425. type,
  426. maintopic,
  427. subtopic,
  428. subsubtopic,
  429. exercise,
  430. ),
  431. )
  432. try:
  433. return cursor.fetchone()[0]
  434. except Exception as e:
  435. print(e)
  436. return None
  437. else:
  438. return sol_id
  439. def set_solution(
  440. klas, subject, author, type, maintopic, subtopic, subsubtopic, exercise, solution_id
  441. ):
  442. with connection:
  443. cursor.execute(
  444. """
  445. UPDATE gdz
  446. SET solution_id = (%s)
  447. WHERE klas = (%s) AND subject = (%s) AND author = (%s) AND type = (%s) AND maintopic = (%s) AND subtopic = (%s) AND subsubtopic = (%s) AND exercise = (%s)
  448. """,
  449. (
  450. solution_id,
  451. klas,
  452. subject,
  453. author,
  454. type,
  455. maintopic,
  456. subtopic,
  457. subsubtopic,
  458. exercise,
  459. ),
  460. )
  461. connection.commit()
  462. # save keyboards for 'back' func
  463. def set_keyboard_and_msg(data, user_id):
  464. with connection:
  465. cursor.execute(
  466. """
  467. UPDATE users
  468. SET markup = (%s)
  469. WHERE user_id = (%s)
  470. """,
  471. (psycopg2.Binary(data), str(user_id)),
  472. )
  473. connection.commit()
  474. def get_keyboard_and_msg(user_id):
  475. with connection:
  476. cursor.execute(
  477. """
  478. SELECT markup
  479. FROM users
  480. WHERE user_id = (%s)
  481. """,
  482. (str(user_id),),
  483. )
  484. try:
  485. return cursor.fetchone()[0]
  486. except Exception as e:
  487. print(e)
  488. return None
  489. # is_blocked user
  490. def set_is_blocked(user_id, value):
  491. with connection:
  492. cursor.execute(
  493. """
  494. UPDATE users
  495. SET is_blocked = (%s)
  496. WHERE user_id = (%s)
  497. """,
  498. (value, str(user_id)),
  499. )
  500. connection.commit()
  501. def get_is_blocked(user_id):
  502. with connection:
  503. cursor.execute(
  504. """
  505. SELECT is_blocked
  506. FROM users
  507. WHERE user_id = (%s)
  508. """,
  509. (str(user_id),),
  510. )
  511. try:
  512. return cursor.fetchone()[0]
  513. except Exception as e:
  514. print(e)
  515. return None