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.
This commit is contained in:
Davte 2019-05-23 18:39:15 +02:00
parent a2e1b0c11a
commit 8a524a4e55
2 changed files with 26 additions and 1 deletions

View File

@ -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"

View File

@ -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}")