-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter_sql.py
73 lines (56 loc) · 1.91 KB
/
filter_sql.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
from sqlalchemy import Column, Numeric, String, UnicodeText
from . import BASE, SESSION
class Filter(BASE):
__tablename__ = "catfilters"
chat_id = Column(String(14), primary_key=True)
keyword = Column(UnicodeText, primary_key=True, nullable=False)
reply = Column(UnicodeText)
f_mesg_id = Column(Numeric)
def __init__(self, chat_id, keyword, reply, f_mesg_id):
self.chat_id = str(chat_id)
self.keyword = keyword
self.reply = reply
self.f_mesg_id = f_mesg_id
def __eq__(self, other):
return bool(
isinstance(other, Filter)
and self.chat_id == other.chat_id
and self.keyword == other.keyword
)
Filter.__table__.create(checkfirst=True)
def get_filter(chat_id, keyword):
try:
return SESSION.query(Filter).get((str(chat_id), keyword))
finally:
SESSION.close()
def get_filters(chat_id):
try:
return SESSION.query(Filter).filter(Filter.chat_id == str(chat_id)).all()
finally:
SESSION.close()
def add_filter(chat_id, keyword, reply, f_mesg_id):
to_check = get_filter(chat_id, keyword)
if not to_check:
adder = Filter(str(chat_id), keyword, reply, f_mesg_id)
SESSION.add(adder)
SESSION.commit()
return True
rem = SESSION.query(Filter).get((str(chat_id), keyword))
SESSION.delete(rem)
SESSION.commit()
adder = Filter(str(chat_id), keyword, reply, f_mesg_id)
SESSION.add(adder)
SESSION.commit()
return False
def remove_filter(chat_id, keyword):
to_check = get_filter(chat_id, keyword)
if not to_check:
return False
rem = SESSION.query(Filter).get((str(chat_id), keyword))
SESSION.delete(rem)
SESSION.commit()
return True
def remove_all_filters(chat_id):
if saved_filter := SESSION.query(Filter).filter(Filter.chat_id == str(chat_id)):
saved_filter.delete()
SESSION.commit()