def set_callback(update: Update, context: CallbackContext, chat_info: dict): if update.message.text not in settings.LANGUAGES_NAME: update.message.reply_text(text="🧐") return SettingsSteps.language else: locale = settings.LANGUAGES_NAME[update.message.text] db_session = Session() db_session.query(Chat).filter_by(id=update.message.chat_id).update( {"locale": locale}) transaction.commit() _ = get_translations(locale) text_to = _("*%(language)s* is your language now.") % { "language": LOCALE_NAME[locale] } update.message.reply_text(parse_mode=ParseMode.MARKDOWN, text=text_to) main_menu(update, chat_info, _) return SettingsSteps.main
def notification_auto_disable(pair: list) -> None: db_session = Session() notifications = (db_session.query(Notification).filter_by( is_active=true(), from_currency_id=pair[0], to_currency_id=pair[1]).all()) for n in notifications: _ = get_translations(n.chat.locale) send_notification.delay( n.chat_id, _("Your notification has been disabled, due to one of the currencies" " %(from_currency)s %(to_currency)s has been deactivated.") % { "from_currency": n.from_currency.code, "to_currency": n.to_currency.code, }, ) db_session.query(Notification).filter_by(is_active=true(), from_currency_id=pair[0], to_currency_id=pair[1]).update( {"is_active": false()}) transaction.commit()
def wrapper(update: Update, context: CallbackContext, *args, **kwargs): language_code = kwargs["chat_info"]["locale"] kwargs["_"] = get_translations(language_code) return func(update, context, *args, **kwargs)
def test_1_exists(self): self.assertTrue(get_translations("en"))
def test_3_not_exists(self): self.assertTrue(get_translations("qq-qqqq-qq"))
def test_3_exists(self): self.assertTrue(get_translations("zh-hans-sg"))
def test_2_4_exists(self): self.assertTrue(get_translations("zh-hans"))
def notification_checker() -> None: db_session = Session() pairs = (db_session.query( Notification.from_currency_id, Notification.to_currency_id).filter_by(is_active=true()).group_by( Notification.from_currency_id, Notification.to_currency_id).all()) for pair in pairs: from_currency = db_session.query(Currency).get(pair[0]) to_currency = db_session.query(Currency).get(pair[1]) if not from_currency.is_active or not to_currency.is_active: logging.info( "Disable notifications because currency %s %s is not active anymore", from_currency.code, to_currency.code, ) notification_auto_disable(pair) continue pr = PriceRequest( amount=None, currency=from_currency.code, to_currency=to_currency.code, parser_name="Notification", ) prr = convert(pr) notifications = (db_session.query(Notification).filter_by( is_active=true()).filter_by(from_currency_id=pair[0], to_currency_id=pair[1]).all()) for n in notifications: if is_triggered(n.trigger_clause, n.trigger_value, n.last_rate, prr.rate): # replace rate_open from daily to rate when was created or last triggered notification # TODO: PriceRequestResult -> dataclass, inside diff and etc nprr = PriceRequestResult( price_request=prr.price_request, exchanges=prr.exchanges, rate=prr.rate, rate_open=n.last_rate, last_trade_at=prr.last_trade_at, low24h=prr.low24h, high24h=prr.high24h, ) text_to = NotifyFormatPriceRequestResult(nprr, n.chat.locale).get() if n.trigger_clause in [ NotifyTriggerClauseEnum.less, NotifyTriggerClauseEnum.more, ]: n.is_active = False _ = get_translations(n.chat.locale) text_to += "\n" text_to += _("_One-time reminder. Set up a new reminder._") send_notification.delay(n.chat_id, text_to) n.last_rate = prr.rate transaction.commit()