def upload(
     yd,
     log,
     photo,
     local_path,
     chat_title,
 ):
     uploaded = False
     with open(local_path, "rb") as f:
         if not Chat.is_exists(photo.chat_id):
             try:
                 Chat.save_to_db(photo.chat_id, chat_title)
             except BaseException as e:
                 log.error('{0}'.format(e))
         yd_path = photo.get_yd_path(yd)
         if not yd.exists(yd_path):
             start = time.time()
             yd.upload(f, yd_path)
             end = time.time()
             log.info("YD uploaded: {0} ({1}s)".format(
                 yd_path, str(end - start)))
             if not Photo.is_exists(photo.chat_id, local_path):
                 db.session.add(photo)
                 db.session.commit()
                 log.info("DB added: " + yd_path)
             uploaded = True
     os.remove(local_path)
     return uploaded
 def test_models(self):
     Chat.save_to_db(1, "test1")
     ch = Chat.get_chat(1)
     self.assertEqual(1, ch.id)
     self.assertEqual("test1", ch.name)
     o = ch.options.all()
     self.assertTrue(len(o) == 2)
     ch.add_option("test_key", "test_val")
     o = ch.options.all()
     self.assertTrue(len(o) == 3)
     self.assertEqual("test_key", o[2].key)
     self.assertEqual("test_val", o[2].value)
     self.assertIsNotNone(o[2].chat)
     self.assertIsNotNone(ChatOption.get_val(ch.id, "test_key"))
     self.assertIsNone(ChatOption.get_val(ch.id, "test_val"))
     with self.assertRaises(Exception):
         ch.add_option("test_key", "exception")
 def init_chat(message):
     with app.app_context():
         if not is_initialized(message):
             if not Chat.is_exists(message.chat.id):
                 try:
                     ch = Chat.save_to_db(message.chat.id, message.chat.title)
                 except BaseException as e:
                     self.l.error('{0}'.format(e))
             ch.get_yd_folder(self.y)
 def init_bot_options(self):
     with self.app.app_context():
         ch = Chat.get_chat(1)
         if ch is None:
             Chat.save_to_db(1, "BotOptions")