66import pytz
77import datetime
88import random
9+ from contextlib import contextmanager
910
1011# Non STL imports
1112from telegram import InlineKeyboardButton , InlineKeyboardMarkup , Update
3940bsasTz = pytz .timezone ("America/Argentina/Buenos_Aires" )
4041
4142
43+ @contextmanager
44+ def get_session ():
45+ """Provide a transactional scope around a series of operations."""
46+ session = Session ()
47+ try :
48+ yield session
49+ session .commit ()
50+ except :
51+ session .rollback ()
52+ raise
53+ finally :
54+ session .close ()
55+
56+
4257async def start (update : Update , context : ContextTypes .DEFAULT_TYPE ):
4358 await update .message .reply_text (
4459 "Hola, ¿qué tal? ¡Mandame /help si no sabés qué puedo hacer!" )
4560
4661
4762async def help (update : Update , context : ContextTypes .DEFAULT_TYPE ):
4863 message_text = "Comandos disponibles:\n "
49- for name , command_info in sorted (COMMANDS .items ()):
50- if 'description' in command_info and command_info ['description' ]:
51- message_text += f"/{ name } - { command_info ['description' ]} \n "
64+ with get_session () as session :
65+ commands = session .query (Command ).filter_by (enabled = True ).order_by (Command .name ).all ()
66+ for command in commands :
67+ message_text += f"/{ command .name } - { command .description } \n "
5268 await update .message .reply_text (message_text )
5369
5470
@@ -57,8 +73,7 @@ async def estasvivo(update: Update, context: ContextTypes.DEFAULT_TYPE):
5773
5874
5975async def list_buttons (update : Update , context : ContextTypes .DEFAULT_TYPE , listable_type ):
60- session = Session ()
61- try :
76+ with get_session () as session :
6277 buttons = session .query (listable_type ).filter_by (validated = True ).order_by (listable_type .name ).all ()
6378 keyboard = []
6479 columns = 3
@@ -71,8 +86,6 @@ async def list_buttons(update: Update, context: ContextTypes.DEFAULT_TYPE, lista
7186 reply_markup = InlineKeyboardMarkup (keyboard )
7287 await update .message .reply_text (text = "Grupos: " , disable_web_page_preview = True ,
7388 reply_markup = reply_markup )
74- finally :
75- session .close ()
7689
7790
7891async def listar (update : Update , context : ContextTypes .DEFAULT_TYPE ):
@@ -92,16 +105,13 @@ async def listarotro(update: Update, context: ContextTypes.DEFAULT_TYPE):
92105
93106
94107async def cubawiki (update : Update , context : ContextTypes .DEFAULT_TYPE ):
95- session = Session ()
96- try :
108+ with get_session () as session :
97109 group = session .query (Obligatoria ).filter (
98110 Obligatoria .chat_id == str (update .message .chat .id ),
99111 Obligatoria .cubawiki_url != None
100112 ).first ()
101113 if group :
102114 await update .message .reply_text (group .cubawiki_url )
103- finally :
104- session .close ()
105115
106116
107117async def suggest_listable (update : Update , context : ContextTypes .DEFAULT_TYPE , listable_type ):
@@ -115,14 +125,11 @@ async def suggest_listable(update: Update, context: ContextTypes.DEFAULT_TYPE, l
115125 " <nombre>|<link>" )
116126 return
117127
118- session = Session ()
119- try :
128+ with get_session () as session :
120129 group = listable_type (name = name , url = url )
121130 session .add (group )
122- session .commit ()
131+ session .flush ()
123132 group_id = group .id
124- finally :
125- session .close ()
126133
127134 keyboard = [
128135 [
@@ -240,8 +247,7 @@ async def colaborar(update: Update, context: ContextTypes.DEFAULT_TYPE):
240247# Manda una imagen a partir de su path al chat del update dado
241248async def mandar_imagen (chat_id , context : ContextTypes .DEFAULT_TYPE , file_path ):
242249 await context .bot .send_chat_action (chat_id = chat_id , action = ChatAction .UPLOAD_PHOTO )
243- session = Session ()
244- try :
250+ with get_session () as session :
245251 file = session .query (File ).filter_by (path = file_path ).first ()
246252 if file :
247253 msg = await context .bot .send_photo (
@@ -252,17 +258,13 @@ async def mandar_imagen(chat_id, context: ContextTypes.DEFAULT_TYPE, file_path):
252258 chat_id = chat_id , photo = f , allow_sending_without_reply = True )
253259 new_file = File (path = file_path , file_id = msg .photo [0 ].file_id )
254260 session .add (new_file )
255- session .commit ()
256- finally :
257- session .close ()
258261
259262
260263# Manda un documento a partir de su path al chat del update dado
261264async def mandar_pdf (chat_id , context : ContextTypes .DEFAULT_TYPE , file_path ):
262265 await context .bot .send_chat_action (
263266 chat_id = chat_id , action = ChatAction .UPLOAD_DOCUMENT )
264- session = Session ()
265- try :
267+ with get_session () as session :
266268 file = session .query (File ).filter_by (path = file_path ).first ()
267269 if file :
268270 msg = await context .bot .send_document (
@@ -273,9 +275,6 @@ async def mandar_pdf(chat_id, context: ContextTypes.DEFAULT_TYPE, file_path):
273275 chat_id = chat_id , document = f , allow_sending_without_reply = True )
274276 new_file = File (path = file_path , file_id = msg .document .file_id )
275277 session .add (new_file )
276- session .commit ()
277- finally :
278- session .close ()
279278
280279
281280# Responde una imagen a partir de su path al chat del update dado
@@ -293,18 +292,15 @@ async def button(update: Update, context: ContextTypes.DEFAULT_TYPE):
293292 message = query .message
294293 buttonType , id , action = query .data .split ("|" )
295294
296- session = Session ()
297- try :
295+ with get_session () as session :
298296 if buttonType == "Listable" :
299297 group = session .query (Listable ).filter_by (id = int (id )).first ()
300298 if group :
301299 if action == "1" :
302300 group .validated = True
303- session .commit ()
304301 action_text = "\n ¡Aceptado!"
305302 else :
306303 session .delete (group )
307- session .commit ()
308304 action_text = "\n ¡Rechazado!"
309305 await query .edit_message_text (text = message .text + action_text )
310306
@@ -313,17 +309,13 @@ async def button(update: Update, context: ContextTypes.DEFAULT_TYPE):
313309 if noticia :
314310 if action == "1" :
315311 noticia .validated = True
316- session .commit ()
317312 action_text = "\n ¡Aceptado!"
318313 await context .bot .send_message (chat_id = NOTICIAS_CHATID ,
319314 text = noticia .text , parse_mode = ParseMode .MARKDOWN )
320315 else :
321316 session .delete (noticia )
322- session .commit ()
323317 action_text = "\n ¡Rechazado!"
324318 await query .edit_message_text (text = message .text + action_text )
325- finally :
326- session .close ()
327319
328320
329321COMMANDS = {
@@ -375,6 +367,10 @@ async def button(update: Update, context: ContextTypes.DEFAULT_TYPE):
375367 'handler' : sugerirotro ,
376368 'description' : 'Sugiere un grupo de otra categoría.'
377369 },
370+ 'agregargrupo' : {
371+ 'handler' : agregargrupo ,
372+ 'description' : 'Muestra ayuda para sugerir un grupo.'
373+ },
378374 'campusvivo' : {
379375 'handler' : campusvivo ,
380376 'description' : 'Verifica si el Campus Virtual está funcionando.'
0 commit comments