Skip to content

Commit 599e6ea

Browse files
committed
Adding context manager on core sql operations in case of errors with the connection | Related to #27
1 parent 0252338 commit 599e6ea

1 file changed

Lines changed: 57 additions & 55 deletions

File tree

tg_bot/modules/sql/locks_sql.py

Lines changed: 57 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -158,64 +158,66 @@ def update_restriction(chat_id, restr_type, locked):
158158

159159

160160
def is_locked(chat_id, lock_type):
161-
curr_perm = SESSION.query(Permissions).get(str(chat_id))
162-
SESSION.close()
163-
164-
if not curr_perm:
165-
return False
166-
167-
elif lock_type == "sticker":
168-
return curr_perm.sticker
169-
elif lock_type == "photo":
170-
return curr_perm.photo
171-
elif lock_type == "audio":
172-
return curr_perm.audio
173-
elif lock_type == "voice":
174-
return curr_perm.voice
175-
elif lock_type == "contact":
176-
return curr_perm.contact
177-
elif lock_type == "video":
178-
return curr_perm.video
179-
elif lock_type == "videonote":
180-
return curr_perm.videonote
181-
elif lock_type == "document":
182-
return curr_perm.document
183-
elif lock_type == "gif":
184-
return curr_perm.gif
185-
elif lock_type == "url":
186-
return curr_perm.url
187-
elif lock_type == "bots":
188-
return curr_perm.bots
189-
elif lock_type == "forward":
190-
return curr_perm.forward
191-
elif lock_type == "game":
192-
return curr_perm.game
193-
elif lock_type == "location":
194-
return curr_perm.location
161+
with PERM_LOCK:
162+
curr_perm = SESSION.query(Permissions).get(str(chat_id))
163+
SESSION.close()
164+
165+
if not curr_perm:
166+
return False
167+
168+
elif lock_type == "sticker":
169+
return curr_perm.sticker
170+
elif lock_type == "photo":
171+
return curr_perm.photo
172+
elif lock_type == "audio":
173+
return curr_perm.audio
174+
elif lock_type == "voice":
175+
return curr_perm.voice
176+
elif lock_type == "contact":
177+
return curr_perm.contact
178+
elif lock_type == "video":
179+
return curr_perm.video
180+
elif lock_type == "videonote":
181+
return curr_perm.videonote
182+
elif lock_type == "document":
183+
return curr_perm.document
184+
elif lock_type == "gif":
185+
return curr_perm.gif
186+
elif lock_type == "url":
187+
return curr_perm.url
188+
elif lock_type == "bots":
189+
return curr_perm.bots
190+
elif lock_type == "forward":
191+
return curr_perm.forward
192+
elif lock_type == "game":
193+
return curr_perm.game
194+
elif lock_type == "location":
195+
return curr_perm.location
195196

196197

197198
def is_restr_locked(chat_id, lock_type):
198-
curr_restr = SESSION.query(Restrictions).get(str(chat_id))
199-
SESSION.close()
200-
201-
if not curr_restr:
202-
return False
203-
204-
if lock_type == "messages":
205-
return curr_restr.messages
206-
elif lock_type == "media":
207-
return curr_restr.media
208-
elif lock_type == "other":
209-
return curr_restr.other
210-
elif lock_type == "previews":
211-
return curr_restr.preview
212-
elif lock_type == "all":
213-
return (
214-
curr_restr.messages
215-
and curr_restr.media
216-
and curr_restr.other
217-
and curr_restr.preview
218-
)
199+
with PERM_LOCK:
200+
curr_restr = SESSION.query(Restrictions).get(str(chat_id))
201+
SESSION.close()
202+
203+
if not curr_restr:
204+
return False
205+
206+
if lock_type == "messages":
207+
return curr_restr.messages
208+
elif lock_type == "media":
209+
return curr_restr.media
210+
elif lock_type == "other":
211+
return curr_restr.other
212+
elif lock_type == "previews":
213+
return curr_restr.preview
214+
elif lock_type == "all":
215+
return (
216+
curr_restr.messages
217+
and curr_restr.media
218+
and curr_restr.other
219+
and curr_restr.preview
220+
)
219221

220222

221223
def get_locks(chat_id):

0 commit comments

Comments
 (0)