def delete(self, chat_id, token_id, counter_id): try: token = Metrika_tokens.get(Metrika_tokens.id == token_id) try: counter = Metrika.select().where((Metrika.counter_id == counter_id) & (Metrika.chat_id == chat_id)).get() counter.delete_instance() counter_name = CConfig.CACHE['metrika_%s_%s' % (token_id, counter_id)] send_to_chat("Готово! Сайт <%s> отключен от модуля." % counter_name, chat_id) except Metrika.DoesNotExist: send_to_chat("Сайт не найден.", chat_id) except Metrika_tokens.DoesNotExist: send_to_chat("Ошибка токена <%s>." % token_id, chat_id)
def add(self, chat_id, token_id, counter_id): # TODO: Params as hash id in cache try: token = Metrika_tokens.get(Metrika_tokens.id == token_id) try: counter = Metrika.select().where((Metrika.counter_id == counter_id) & (Metrika.chat_id == chat_id)).get() send_to_chat("Счетчик <%s> уже прикреплен к данному чату." % counter.counter_name, chat_id) except Metrika.DoesNotExist: counter_name = CConfig.CACHE['metrika_%s_%s' % (token_id, counter_id)] metrika = Metrika.create(oauth_token=token.token, counter_id=counter_id, chat_id=chat_id, counter_name=counter_name, client_id=CConfig.MODULES['metrika']['ID']) metrika.save() send_to_chat("Готово! Сайт <%s> успешно подключен." % counter_name, chat_id) except Metrika_tokens.DoesNotExist: send_to_chat("Ошибка токена <%s>." % token_id, chat_id)
def metrika_stop(self, chat_id): try: token = Metrika_tokens.get(Metrika_tokens.chat_id == chat_id) counters = Metrika.select().where(Metrika.chat_id == chat_id) if len(counters): buttons = [] buttons_row = [] for counter in counters: buttons_row.append( {'text': counter.counter_name, 'callback_data': "/metrika_%s%s#%s" % ("stop", token.id, counter.counter_id)}) CConfig.CACHE['metrika_%s_%s' % (token.id, counter.counter_id)] = counter.counter_name if len(buttons_row) == 2: buttons.append(buttons_row[:]) buttons_row = [] if len(buttons_row): buttons.append(buttons_row[:]) send_object_to_chat("Выберите сайт, который хотите отключить.\n", json.dumps({ 'inline_keyboard': buttons }), chat_id) else: send_to_chat("Подключенных счетчиков не найдено.", chat_id) except Metrika_tokens.DoesNotExist: send_to_chat("Токен не найден.", chat_id)