forked from selfedu-rus/flasksite-16
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFDataBase.py
More file actions
109 lines (89 loc) · 3.64 KB
/
FDataBase.py
File metadata and controls
109 lines (89 loc) · 3.64 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
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
import sqlite3
import time
import math
import re
from flask import url_for
class FDataBase:
def __init__(self, db):
self.__db = db
self.__cur = db.cursor()
def getMenu(self):
sql = '''SELECT * FROM mainmenu'''
try:
self.__cur.execute(sql)
res = self.__cur.fetchall()
if res: return res
except:
print("Ошибка чтения из БД")
return []
def addPost(self, title, text, url):
try:
self.__cur.execute(f"SELECT COUNT() as `count` FROM posts WHERE url LIKE '{url}'")
res = self.__cur.fetchone()
if res['count'] > 0:
print("Статья с таким url уже существует")
return False
base = url_for('static', filename='images_html')
text = re.sub(r"(?P<tag><img\s+[^>]*src=)(?P<quote>[\"'])(?P<url>.+?)(?P=quote)>",
"\\g<tag>" + base + "/\\g<url>>",
text)
tm = math.floor(time.time())
self.__cur.execute("INSERT INTO posts VALUES(NULL, ?, ?, ?, ?)", (title, text, url, tm))
self.__db.commit()
except sqlite3.Error as e:
print("Ошибка добавления статьи в БД "+str(e))
return False
return True
def getPost(self, alias):
try:
self.__cur.execute(f"SELECT title, text FROM posts WHERE url LIKE '{alias}' LIMIT 1")
res = self.__cur.fetchone()
if res:
return res
except sqlite3.Error as e:
print("Ошибка получения статьи из БД "+str(e))
return (False, False)
def getPostsAnonce(self):
try:
self.__cur.execute(f"SELECT id, title, text, url FROM posts ORDER BY time DESC")
res = self.__cur.fetchall()
if res: return res
except sqlite3.Error as e:
print("Ошибка получения статьи из БД "+str(e))
return []
def addUser(self, name, email, hpsw):
try:
self.__cur.execute(f"SELECT COUNT() as `count` FROM users WHERE email LIKE '{email}'")
res = self.__cur.fetchone()
if res['count'] > 0:
print("Пользователь с таким email уже существует")
return False
tm = math.floor(time.time())
self.__cur.execute("INSERT INTO users VALUES(NULL, ?, ?, ?, ?)", (name, email, hpsw, tm))
self.__db.commit()
except sqlite3.Error as e:
print("Ошибка добавления пользователя в БД "+str(e))
return False
return True
def getUser(self, user_id):
try:
self.__cur.execute(f"SELECT * FROM users WHERE id = {user_id} LIMIT 1")
res = self.__cur.fetchone()
if not res:
print("Пользователь не найден")
return False
return res
except sqlite3.Error as e:
print("Ошибка получения данных из БД "+str(e))
return False
def getUserByEmail(self, email):
try:
self.__cur.execute(f"SELECT * FROM users WHERE email = '{email}' LIMIT 1")
res = self.__cur.fetchone()
if not res:
print("Пользователь не найден")
return False
return res
except sqlite3.Error as e:
print("Ошибка получения данных из БД "+str(e))
return False