33from datetime import datetime , timedelta
44
55from telegram .ext import CommandHandler
6+ from telegram .error import BadRequest
67from pycamp_bot .models import Pycampista , WizardAtPycamp
78from pycamp_bot .commands .auth import admin_needed
89from pycamp_bot .commands .manage_pycamp import get_active_pycamp
@@ -56,7 +57,7 @@ def compute_wizards_slots(pycamp):
5657 wizard_end = pycamp .end
5758 slots = []
5859 current_period = wizard_start
59- while current_period < wizard_end :
60+ while current_period < wizard_end : # TODO: check fields None
6061 slot_start = current_period
6162 slot_end = current_period + timedelta (minutes = pycamp .wizard_slot_duration )
6263 slots .append (
@@ -94,6 +95,7 @@ def define_wizards_schedule(pycamp):
9495 idx += 1
9596 return wizard_per_slot
9697
98+
9799async def become_wizard (update , context ):
98100 current_wizards = Pycampista .select ().where (Pycampista .wizard is True )
99101
@@ -172,7 +174,11 @@ async def notify_schedule_to_wizards(update, context, pycamp):
172174 (WizardAtPycamp .pycamp == pycamp ) & (WizardAtPycamp .wizard == wizard )
173175 ).order_by (WizardAtPycamp .init )
174176
175- await notify_scheduled_slots_to_wizard (update , context , pycamp , wizard , wizard_agenda )
177+ try :
178+ await notify_scheduled_slots_to_wizard (update , context , pycamp , wizard , wizard_agenda )
179+ logger .debug ("Notified wizard schedule to {}" .format (wizard .username ))
180+ except BadRequest :
181+ logger .warn ("Coulnd't notify its wizzard schedule to {}" .format (wizard .username ))
176182
177183
178184def persist_wizards_schedule_in_db (pycamp ):
@@ -190,7 +196,6 @@ def persist_wizards_schedule_in_db(pycamp):
190196 init = start ,
191197 end = end
192198 )
193- logger .debug ("From {} to {} the wizard is {}" .format (start , end , wizard .username ))
194199
195200
196201@admin_needed
@@ -201,6 +206,8 @@ async def schedule_wizards(update, context):
201206 logger .info ("Deleted wizards schedule ({} records)" .format (n ))
202207
203208 persist_wizards_schedule_in_db (pycamp )
209+ logger .info ("Wizards schedule persisted in the DB." )
210+
204211
205212 await notify_schedule_to_wizards (update , context , pycamp )
206213
@@ -215,7 +222,6 @@ async def schedule_wizards(update, context):
215222 )
216223
217224
218-
219225def format_wizards_schedule (agenda ):
220226 """Aux function to render the wizards schedule as a friendly message."""
221227 per_day = defaultdict (list )
@@ -265,7 +271,7 @@ async def show_wizards_schedule(update, context):
265271 if not show_all :
266272 agenda = agenda .where (WizardAtPycamp .end > datetime .now ())
267273 agenda = agenda .order_by (WizardAtPycamp .init )
268-
274+
269275 msg = format_wizards_schedule (agenda )
270276
271277 await context .bot .send_message (
0 commit comments