From 8a524a4e55d59ddb030d9513ab0546095d94e8d6 Mon Sep 17 00:00:00 2001 From: Davte Date: Thu, 23 May 2019 18:39:15 +0200 Subject: [PATCH] Added a method to create views in bot's database Given a list of dictionaries with `name` and `query` elements, overwrite or create a view. --- davtelepot/__init__.py | 2 +- davtelepot/custombot.py | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/davtelepot/__init__.py b/davtelepot/__init__.py index ae13201..82cea4f 100644 --- a/davtelepot/__init__.py +++ b/davtelepot/__init__.py @@ -7,7 +7,7 @@ __author__ = "Davide Testa" __email__ = "davte@libero.it" __credits__ = "Marco Origlia" __license__ = "GNU General Public License v3.0" -__version__ = "1.5.0" +__version__ = "1.5.1" __maintainer__ = "Davide Testa" __contact__ = "t.me/davte" diff --git a/davtelepot/custombot.py b/davtelepot/custombot.py index 9b47b66..bfdb290 100644 --- a/davtelepot/custombot.py +++ b/davtelepot/custombot.py @@ -2305,3 +2305,28 @@ class Bot(telepot.aio.Bot, Gettable): else: logging.info("Invalid name, please try again.") return 65 # Keep running, making user select another bot + + def create_views(self, views, overwrite=False): + """Take a list of `views` and add them to bot database. + + Each element of this list should have + - a `name` field + - a `query field` + """ + with self.db as db: + for view in views: + try: + if overwrite: + db.query( + f""" + DROP VIEW IF EXISTS {view['name']} + """ + ) + db.query( + f""" + CREATE VIEW IF NOT EXISTS {view['name']} + AS {view['query']} + """ + ) + except Exception as e: + logging.error(f"{e}")