def handle_user_option_addition(bot, update, session, user, text, poll, chat): """Handle the addition of options from and arbitrary user.""" if not poll.allow_new_options: user.current_poll = None user.expected_input = None chat.send_message(i18n.t("creation.not_allowed", locale=user.locale)) added_options = add_options(session, poll, text) if len(added_options) > 0: # Reset user user.current_poll = None user.expected_input = None # Send message text = i18n.t("creation.option.multiple_added", locale=user.locale) + "\n" for option in added_options: text += f"\n*{option}*" chat.send_message(text, parse_mode="markdown") # Update all polls poll.init_votes_for_new_options(session) session.commit() update_poll_messages(session, bot, poll) else: chat.send_message(i18n.t("creation.option.no_new", locale=user.locale))
def handle_user_option_addition(bot, update, session, user, text, poll, chat): """Handle the addition of options from and arbitrary user.""" if not poll.allow_new_options: user.current_poll = None user.expected_input = None chat.send_message(i18n.t('creation.not_allowed', locale=user.locale)) added_options = add_options(poll, text) if len(added_options) > 0: # Reset user user.current_poll = None user.expected_input = None # Send message text = i18n.t('creation.option.multiple_added', locale=user.locale) + '\n' for option in added_options: text += f'\n*{option}*' chat.send_message(text, parse_mode='markdown') # Upate all polls update_poll_messages(session, bot, poll) else: chat.send_message(i18n.t('creation.option.no_new', locale=user.locale))
def owner_pick_date_option(session, context, poll, datepicker_context): """Owner adds or removes a date option.""" picked_date = date.fromisoformat(context.data[2]) # Check if we already have this date as option existing_option = poll.get_date_option(picked_date) # If that's the case, delete it if existing_option is not None: session.delete(existing_option) session.commit() message = i18n.t("callback.date_removed", locale=poll.locale, date=picked_date.isoformat()) else: added_options = add_options(session, poll, context.data[2], is_date=True) message = i18n.t("callback.date_picked", locale=poll.locale, date=picked_date.isoformat()) context.query.answer(message) update_datepicker(context, poll, datepicker_context, picked_date.replace(day=1)) if poll.created: update_poll_messages(session, context.bot, poll)
def handle_create_options(bot, update, session, user, text, poll, chat): """Add options to the poll.""" # Multiple options can be sent at once separated by newline # Strip them and ignore empty lines added_options = add_options(session, poll, text) if len(added_options) == 0: return i18n.t("creation.option.no_new", locale=user.locale) next_option(chat, poll, added_options)
def pick_external_date(session, context, poll): """Add or remove a date option during creation.""" picked_date = date.fromisoformat(context.data[2]) # Check if we already have this date as option existing_option = poll.get_date_option(picked_date) # If that's the case, delete it if existing_option is not None: return i18n.t("callback.date_already_picked", locale=poll.locale) add_options(session, poll, context.data[2], is_date=True) message = i18n.t("callback.date_picked", locale=poll.locale, date=picked_date.isoformat()) context.query.answer(message) update_datepicker(context, poll, DatepickerContext.external_add_option, picked_date.replace(day=1)) update_poll_messages(session, context.bot, poll)
def add_date(session, context, poll): """Add a date from the datepicker to the poll.""" option = poll.current_date.isoformat() added_options = add_options(poll, option, is_date=True) if len(added_options) == 0: context.query.answer(i18n.t('callback.date_already_picked', locale=poll.locale)) else: update_datepicker(context, poll) context.query.answer(i18n.t('callback.date_picked', locale=poll.locale, date=poll.current_date.isoformat()))
def add_date(session, context, poll): """Add a date from the datepicker to the poll.""" option = poll.current_date.isoformat() added_options = add_options(poll, option, is_date=True) if len(added_options) == 0: return i18n.t('callback.date_already_picked', locale=poll.locale) else: update_datepicker(context, poll) return i18n.t('callback.date_picked', locale=poll.locale, date=poll.current_date.isoformat()) if poll.created: update_poll_messages(session, context.bot, poll)
def handle_new_option(bot, update, session, user, text, poll, chat): """Add a new option after poll creation.""" added_options = add_options(poll, text) if len(added_options) > 0: text = i18n.t('creation.option.multiple_added', locale=user.locale) + '\n' for option in added_options: text += f'\n*{option}*' chat.send_message(text, parse_mode='markdown') poll.init_votes_for_new_options(session) else: chat.send_message(i18n.t('creation.option.no_new', locale=user.locale)) # Reset expected input user.current_poll = None user.expected_input = None text = get_settings_text(poll) keyboard = get_settings_keyboard(poll) message = chat.send_message( text, parse_mode='markdown', reply_markup=keyboard, ) # Delete old references references = session.query(Reference) \ .filter(Reference.poll == poll) \ .filter(Reference.admin_user_id == chat.id) \ .all() for reference in references: try: bot.delete_message(chat.id, reference.admin_message_id) except: pass session.delete(reference) # Create new reference reference = Reference(poll, admin_user=user, admin_message_id=message.message_id) session.add(reference) session.commit() update_poll_messages(session, bot, poll)
def handle_new_option(bot, update, session, user, text, poll, chat): """Add a new option after poll creation.""" added_options = add_options(session, poll, text) if len(added_options) > 0: text = i18n.t("creation.option.multiple_added", locale=user.locale) + "\n" for option in added_options: text += f"\n*{option}*" chat.send_message(text, parse_mode="markdown") poll.init_votes_for_new_options(session) else: chat.send_message(i18n.t("creation.option.no_new", locale=user.locale)) # Reset expected input user.current_poll = None user.expected_input = None text = get_settings_text(poll) keyboard = get_settings_keyboard(poll) message = chat.send_message( text, parse_mode="markdown", reply_markup=keyboard, ) remove_old_references(session, bot, poll, user) # Create new reference reference = Reference(poll, ReferenceType.admin.name, user=user, message_id=message.message_id) session.add(reference) session.commit() update_poll_messages(session, bot, poll, message.message_id, user)