def test_format_event_real_html(self): component = Component() component.add('summary', 'Встреча ML-клуба') component.add('location', 'ул. Таубе, 5, Омск, Омская обл., Россия, 644037') component.add( 'description', '10 февраля в 11:00 пройдет 5-я встреча <a href="https://vk.com/mlomsk">ML клуба</a> в офисе <a href="https://vk.com/7bits">7bits</a>, Таубе 5. Регистрация на встречу: <a href="https://vk.com/away.php?to=http%3A%2F%2Fmlomsk.1der.link%2Fmeetup%2Fsignup&post=-141957789_74&cc_key=" target="_blank">mlomsk.1der.link/meetup/signup</a>.<br><br>В этот раз у нас будет 2 доклада:' ) timezone = pytz.timezone('Asia/Omsk') component.add( 'dtstart', datetime.datetime(2018, 2, 10, 11, 0, 0, tzinfo=timezone)) event = Event.from_vevent(component, timezone) user_config = UserConfig.new(Config('calbot.cfg.sample'), 'TEST') user_config.language = 'ru_RU.UTF-8' result = format_event(user_config, event) self.assertEqual( 'Встреча ML-клуба\nСуббота, 10 февраля 2018, 11:00 Asia/Omsk\nул. Таубе, 5, Омск, Омская обл., Россия, 644037\n10 февраля в 11:00 пройдет 5-я встреча ML клуба (https://vk.com/mlomsk) в офисе 7bits (https://vk.com/7bits), Таубе 5. Регистрация на встречу: mlomsk.1der.link/meetup/signup.\n\nВ этот раз у нас будет 2 доклада:', result)
def set_lang(bot, update, config): message = update.message user_id = str(message.chat_id) user_config = config.load_user(user_id) new_lang = message.text.strip() old_lang = user_config.language try: normalized_locale = normalize_locale(new_lang) user_config.set_language(normalized_locale) try: sample = format_event(user_config, sample_event) message.reply_text('Language is updated to %s\nSample event:' % normalized_locale) message.reply_text(sample) return END except locale.Error as e: if old_lang: user_config.set_language(old_lang) logger.warning('Unsupported language "%s" for user %s', new_lang, user_id, exc_info=True) message.reply_text('Unsupported language:\n%s' % e) message.reply_text('Try again or /cancel') return SETTING except Exception as e: logger.warning('Failed to update language to "%s" for user %s', new_lang, user_id, exc_info=True) try: message.reply_text('Failed to update language:\n%s' % e) message.reply_text('Try again or /cancel') except Exception: logger.error('Failed to send reply to user %s', user_id, exc_info=True) return SETTING
def test_format_date_only_event(self): timezone = pytz.UTC component = _get_component() component.add('dtstart', datetime.date(2016, 6, 23)) event = Event.from_vevent(component, timezone, datetime.time(10, 0)) user_config = UserConfig.new(Config('calbot.cfg.sample'), 'TEST') result = format_event(user_config, event) self.assertEqual( 'summary\nThursday, 23 June 2016\nlocation\ndescription', result)
def get_lang(bot, update, config): message = update.message user_id = str(message.chat_id) user_config = config.load_user(user_id) message.reply_text('Current language is %s\nSample event:' % user_config.language) message.reply_text(format_event(user_config, sample_event)) message.reply_text('Type another language name to set or /cancel') return SETTING
def test_format_event_blanks(self): component = Component() component.add('dtstart', datetime.datetime(2018, 2, 3, 13, 3, 4, tzinfo=pytz.UTC)) event = Event.from_vevent(component, pytz.UTC) user_config = UserConfig.new(Config('calbot.cfg.sample'), 'TEST') user_config.language = 'ru_RU.UTF-8' result = format_event(user_config, event) self.assertEqual( 'None\nСуббота, 03 февраля 2018, 13:03 UTC\nNone\nNone', result)
def test_format_event(self): component = _get_component() component.add( 'dtstart', datetime.datetime(2016, 6, 23, 19, 50, 35, tzinfo=pytz.UTC)) event = Event.from_vevent(component, pytz.UTC) user_config = UserConfig.new(Config('calbot.cfg.sample'), 'TEST') result = format_event(user_config, event) self.assertEqual( 'summary\nThursday, 23 June 2016, 19:50 UTC\nlocation\ndescription', result)
def get_format(bot, update, config): message = update.message user_id = str(message.chat_id) user_config = config.load_user(user_id) message.reply_text('Current format:') message.reply_text(user_config.format) message.reply_text('Sample event:') message.reply_text(format_event(user_config, sample_event)) message.reply_text('Type a new format string to set or /cancel') return SETTING
def test_format_event_ru(self): component = _get_component() component.add( 'dtstart', datetime.datetime(2016, 6, 23, 19, 50, 35, tzinfo=pytz.UTC)) event = Event.from_vevent(component, pytz.UTC) user_config = UserConfig.new(Config('calbot.cfg.sample'), 'TEST') user_config.language = 'ru_RU.UTF-8' result = format_event(user_config, event) self.assertEqual( 'summary\nЧетверг, 23 июня 2016, 19:50 UTC\nlocation\ndescription', result)
def send_event(bot, config, event): """ Sends the event notification to the channel :param bot: Bot instance :param config: CalendarConfig instance :param event: Event instance, read from ical :return: None """ logger.info('Sending event %s "%s" to %s', event.id, event.title, config.channel_id) bot.sendMessage(chat_id=config.channel_id, text=format_event(config, event))
def test_format_event_html(self): component = Component() component.add('summary', '<b>summary</b>') component.add('location', '<i>location</i>') component.add( 'description', '<b>description</b><br><br> <a href="link.html">link</a>') component.add('dtstart', datetime.datetime(2018, 2, 3, 13, 3, 4, tzinfo=pytz.UTC)) event = Event.from_vevent(component, pytz.UTC) user_config = UserConfig.new(Config('calbot.cfg.sample'), 'TEST') user_config.language = 'ru_RU.UTF-8' result = format_event(user_config, event) self.assertEqual( 'summary\nСуббота, 03 февраля 2018, 13:03 UTC\nlocation\ndescription\n\n link (link.html)', result)
def set_format(bot, update, config): message = update.message or update.edited_message user_id = str(message.chat_id) user_config = config.load_user(user_id) new_format = message.text.strip() try: user_config.set_format(new_format) message.reply_text('Format is updated.\nSample event:') message.reply_text(format_event(user_config, sample_event)) return END except Exception as e: logger.warning('Failed to update format for user %s', user_id, exc_info=True) try: message.reply_text('Failed to update format:\n%s' % e) message.reply_text('Try again or /cancel') except Exception: logger.error('Failed to send reply to user %s', user_id, exc_info=True) return SETTING