def roombooking_end_notifications(): if not config.ENABLE_ROOMBOOKING: logger.info('Notifications not sent because room booking is disabled') return if not rb_settings.get('end_notifications_enabled'): logger.info( 'Notifications not sent because they are globally disabled') return defaults = { 'default': rb_settings.get('end_notification_daily'), 'weekly': rb_settings.get('end_notification_weekly'), 'monthly': rb_settings.get('end_notification_monthly') } room_columns = { 'default': Room.end_notification_daily, 'weekly': Room.end_notification_weekly, 'monthly': Room.end_notification_monthly } cte = (db.session.query( Reservation.id, Reservation.repeat_frequency).add_columns( db.func.max(ReservationOccurrence.start_dt).label( 'last_valid_end_dt')).join(Reservation.room).join( Reservation.occurrences).filter( ReservationOccurrence.is_valid, ReservationOccurrence.end_dt >= datetime.now(), Reservation.is_accepted, Reservation.repeat_frequency != RepeatFrequency.NEVER, Room.end_notifications_enabled, ~Reservation.end_notification_sent, ~Room.is_deleted).group_by(Reservation.id).cte()) reservations = (Reservation.query.options(noload('created_by_user')).join( cte, cte.c.id == Reservation.id).join(Reservation.room).filter( _make_occurrence_date_filter(cte.c.last_valid_end_dt, defaults, room_columns, cte.c.repeat_frequency)).order_by( 'booked_for_id', 'start_dt', 'room_id').all()) for user, user_reservations in groupby(reservations, key=attrgetter('booked_for_user')): user_reservations = list(user_reservations) notify_about_finishing_bookings(user, list(user_reservations)) for user_reservation in user_reservations: user_reservation.end_notification_sent = True db.session.commit()
def roombooking_end_notifications(): if not config.ENABLE_ROOMBOOKING: logger.info('Notifications not sent because room booking is disabled') return if not rb_settings.get('end_notifications_enabled'): logger.info( 'Notifications not sent because they are globally disabled') return defaults = { 'default': rb_settings.get('end_notification_daily'), 'weekly': rb_settings.get('end_notification_weekly'), 'monthly': rb_settings.get('end_notification_monthly') } room_columns = { 'default': Room.end_notification_daily, 'weekly': Room.end_notification_weekly, 'monthly': Room.end_notification_monthly } reservations = (Reservation.query.join(Reservation.room).filter( ~Room.is_deleted, Room.end_notifications_enabled, Reservation.is_accepted, Reservation.end_dt >= datetime.now(), Reservation.repeat_frequency != RepeatFrequency.NEVER, ~Reservation.end_notification_sent, _make_occurrence_date_filter(Reservation.end_dt, defaults, room_columns)).order_by( Reservation.booked_for_id, Reservation.start_dt, Room.id).all()) for user, user_reservations in groupby(reservations, key=attrgetter('booked_for_user')): user_reservations = list(user_reservations) notify_about_finishing_bookings(user, list(user_reservations)) for user_reservation in user_reservations: user_reservation.end_notification_sent = True db.session.commit()