Karma
commited on
Commit
·
ee060d8
1
Parent(s):
6d752f5
Create notes_sql.py
Browse files- Database/sql/notes_sql.py +187 -0
Database/sql/notes_sql.py
ADDED
@@ -0,0 +1,187 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import threading
|
2 |
+
|
3 |
+
from sqlalchemy import Boolean, Column, Integer, String, UnicodeText, distinct, func
|
4 |
+
|
5 |
+
from Database.sql import BASE, SESSION
|
6 |
+
from Mikobot.plugins.helper_funcs.msg_types import Types
|
7 |
+
|
8 |
+
|
9 |
+
class Notes(BASE):
|
10 |
+
__tablename__ = "notes"
|
11 |
+
chat_id = Column(String(14), primary_key=True)
|
12 |
+
name = Column(UnicodeText, primary_key=True)
|
13 |
+
value = Column(UnicodeText, nullable=False)
|
14 |
+
file = Column(UnicodeText)
|
15 |
+
is_reply = Column(Boolean, default=False)
|
16 |
+
has_buttons = Column(Boolean, default=False)
|
17 |
+
msgtype = Column(Integer, default=Types.BUTTON_TEXT.value)
|
18 |
+
|
19 |
+
def __init__(self, chat_id, name, value, msgtype, file=None):
|
20 |
+
self.chat_id = str(chat_id) # ensure string
|
21 |
+
self.name = name
|
22 |
+
self.value = value
|
23 |
+
self.msgtype = msgtype
|
24 |
+
self.file = file
|
25 |
+
|
26 |
+
def __repr__(self):
|
27 |
+
return "<Note %s>" % self.name
|
28 |
+
|
29 |
+
|
30 |
+
class Buttons(BASE):
|
31 |
+
__tablename__ = "note_urls"
|
32 |
+
id = Column(Integer, primary_key=True, autoincrement=True)
|
33 |
+
chat_id = Column(String(14), primary_key=True)
|
34 |
+
note_name = Column(UnicodeText, primary_key=True)
|
35 |
+
name = Column(UnicodeText, nullable=False)
|
36 |
+
url = Column(UnicodeText, nullable=False)
|
37 |
+
same_line = Column(Boolean, default=False)
|
38 |
+
|
39 |
+
def __init__(self, chat_id, note_name, name, url, same_line=False):
|
40 |
+
self.chat_id = str(chat_id)
|
41 |
+
self.note_name = note_name
|
42 |
+
self.name = name
|
43 |
+
self.url = url
|
44 |
+
self.same_line = same_line
|
45 |
+
|
46 |
+
|
47 |
+
Notes.__table__.create(checkfirst=True)
|
48 |
+
Buttons.__table__.create(checkfirst=True)
|
49 |
+
|
50 |
+
NOTES_INSERTION_LOCK = threading.RLock()
|
51 |
+
BUTTONS_INSERTION_LOCK = threading.RLock()
|
52 |
+
|
53 |
+
|
54 |
+
def add_note_to_db(chat_id, note_name, note_data, msgtype, buttons=None, file=None):
|
55 |
+
if not buttons:
|
56 |
+
buttons = []
|
57 |
+
|
58 |
+
with NOTES_INSERTION_LOCK:
|
59 |
+
prev = SESSION.query(Notes).get((str(chat_id), note_name))
|
60 |
+
if prev:
|
61 |
+
with BUTTONS_INSERTION_LOCK:
|
62 |
+
prev_buttons = (
|
63 |
+
SESSION.query(Buttons)
|
64 |
+
.filter(
|
65 |
+
Buttons.chat_id == str(chat_id),
|
66 |
+
Buttons.note_name == note_name,
|
67 |
+
)
|
68 |
+
.all()
|
69 |
+
)
|
70 |
+
for btn in prev_buttons:
|
71 |
+
SESSION.delete(btn)
|
72 |
+
SESSION.delete(prev)
|
73 |
+
note = Notes(
|
74 |
+
str(chat_id),
|
75 |
+
note_name,
|
76 |
+
note_data or "",
|
77 |
+
msgtype=msgtype.value,
|
78 |
+
file=file,
|
79 |
+
)
|
80 |
+
SESSION.add(note)
|
81 |
+
SESSION.commit()
|
82 |
+
|
83 |
+
for b_name, url, same_line in buttons:
|
84 |
+
add_note_button_to_db(chat_id, note_name, b_name, url, same_line)
|
85 |
+
|
86 |
+
|
87 |
+
def get_note(chat_id, note_name):
|
88 |
+
try:
|
89 |
+
return (
|
90 |
+
SESSION.query(Notes)
|
91 |
+
.filter(func.lower(Notes.name) == note_name, Notes.chat_id == str(chat_id))
|
92 |
+
.first()
|
93 |
+
)
|
94 |
+
finally:
|
95 |
+
SESSION.close()
|
96 |
+
|
97 |
+
|
98 |
+
def rm_note(chat_id, note_name):
|
99 |
+
with NOTES_INSERTION_LOCK:
|
100 |
+
note = (
|
101 |
+
SESSION.query(Notes)
|
102 |
+
.filter(func.lower(Notes.name) == note_name, Notes.chat_id == str(chat_id))
|
103 |
+
.first()
|
104 |
+
)
|
105 |
+
if note:
|
106 |
+
with BUTTONS_INSERTION_LOCK:
|
107 |
+
buttons = (
|
108 |
+
SESSION.query(Buttons)
|
109 |
+
.filter(
|
110 |
+
Buttons.chat_id == str(chat_id),
|
111 |
+
Buttons.note_name == note_name,
|
112 |
+
)
|
113 |
+
.all()
|
114 |
+
)
|
115 |
+
for btn in buttons:
|
116 |
+
SESSION.delete(btn)
|
117 |
+
|
118 |
+
SESSION.delete(note)
|
119 |
+
SESSION.commit()
|
120 |
+
return True
|
121 |
+
|
122 |
+
else:
|
123 |
+
SESSION.close()
|
124 |
+
return False
|
125 |
+
|
126 |
+
|
127 |
+
def get_all_chat_notes(chat_id):
|
128 |
+
try:
|
129 |
+
return (
|
130 |
+
SESSION.query(Notes)
|
131 |
+
.filter(Notes.chat_id == str(chat_id))
|
132 |
+
.order_by(Notes.name.asc())
|
133 |
+
.all()
|
134 |
+
)
|
135 |
+
finally:
|
136 |
+
SESSION.close()
|
137 |
+
|
138 |
+
|
139 |
+
def add_note_button_to_db(chat_id, note_name, b_name, url, same_line):
|
140 |
+
with BUTTONS_INSERTION_LOCK:
|
141 |
+
button = Buttons(chat_id, note_name, b_name, url, same_line)
|
142 |
+
SESSION.add(button)
|
143 |
+
SESSION.commit()
|
144 |
+
|
145 |
+
|
146 |
+
def get_buttons(chat_id, note_name):
|
147 |
+
try:
|
148 |
+
return (
|
149 |
+
SESSION.query(Buttons)
|
150 |
+
.filter(Buttons.chat_id == str(chat_id), Buttons.note_name == note_name)
|
151 |
+
.order_by(Buttons.id)
|
152 |
+
.all()
|
153 |
+
)
|
154 |
+
finally:
|
155 |
+
SESSION.close()
|
156 |
+
|
157 |
+
|
158 |
+
def num_notes():
|
159 |
+
try:
|
160 |
+
return SESSION.query(Notes).count()
|
161 |
+
finally:
|
162 |
+
SESSION.close()
|
163 |
+
|
164 |
+
|
165 |
+
def num_chats():
|
166 |
+
try:
|
167 |
+
return SESSION.query(func.count(distinct(Notes.chat_id))).scalar()
|
168 |
+
finally:
|
169 |
+
SESSION.close()
|
170 |
+
|
171 |
+
|
172 |
+
def migrate_chat(old_chat_id, new_chat_id):
|
173 |
+
with NOTES_INSERTION_LOCK:
|
174 |
+
chat_notes = (
|
175 |
+
SESSION.query(Notes).filter(Notes.chat_id == str(old_chat_id)).all()
|
176 |
+
)
|
177 |
+
for note in chat_notes:
|
178 |
+
note.chat_id = str(new_chat_id)
|
179 |
+
|
180 |
+
with BUTTONS_INSERTION_LOCK:
|
181 |
+
chat_buttons = (
|
182 |
+
SESSION.query(Buttons).filter(Buttons.chat_id == str(old_chat_id)).all()
|
183 |
+
)
|
184 |
+
for btn in chat_buttons:
|
185 |
+
btn.chat_id = str(new_chat_id)
|
186 |
+
|
187 |
+
SESSION.commit()
|